0

I'm trying to sum an array using this code

I don't know what register to use in order to sum the array.

I tried to use ax,bx,cx,dx but really I don't know what to do anymore

.MODEL SMALL
DATA SEGMENT
arr DB 0,0,0,0,0,0,0,0,0,0
DATA ENDS   
 
CODE SEGMENT
ASSUME CS:CODE,DS:DATA      
 
start:
MOV AX,DATA
MOV DS,AX     
MOV AX,0
MOV BX, OFFSET arr
mov cx, 9

input:
mov ah,1
int 21h
mov [bx],al
inc bx    
     
loop input

mov bx, OFFSET arr
mov cx,9    
mov ah,2
mov dl,10
int 21h
mov dl,13
int 21h
                  
mov ax,[bx]
inc bx
         
cal:
add ax,[bx] 
inc bx


loop cal

mov ah,2
mov dl,al
int 21h


MOV AH,4Ch
INT 21h
CODE ENDS
END start
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Or_Melamed
  • 19
  • 4
  • Are you using x86 in 16-bit mode? What assembler (NASM, TASM, MASM, emu8086, GNU assembler, etc.)? Oh, you did have an attempt, you just edited it out for some reason. – Peter Cordes Dec 09 '22 at 16:01
  • Im sorry I dont really know how to use stack overflow, Im using emu8086, x86 – Or_Melamed Dec 09 '22 at 16:11
  • You did your newline in the wrong order, it's 13 followed by 10. Other than that, you seem to have the right idea. I think the mistake you made is that you're using the wrong register size for your data set. Try changing `ax` to `al` in both `mov ax,[bx]` and `add ax,[bx]`. The way things are written now, you're loading and adding two bytes at a time instead of one. And since you're only incrementing once, half of your work is getting destroyed and replaced with half of the next number each time you loop. – puppydrum64 Dec 09 '22 at 16:15
  • The question is: what size answer do you want -- 16 bit or 8 bit? An 8 bit answer would match the array element size, though risk overflow sooner than a 16 bit answer. Either way, the dereference needs to be in 8 bit, then the addition in 8 bits, or convert to 16 bits and then add in 16 bits. – Erik Eidt Dec 10 '22 at 11:13
  • Ignore what I said about the 13,10 in the wrong order, it shouldn't really matter, since those two operations are independent (13 moves the text cursor back to column 0 and 10 moves it to the next row, neither of which get in the other's way.) I'm just used to seeing 13 before 10 so it caught me off guard without thinking about it. The rest of my comment, however, was accurate. – puppydrum64 Dec 21 '22 at 15:11

0 Answers0