Assembly Array and loop -


i've problem in assembly language want make loop sum element of array.
suppose array contains 10,20,30,40,50,60,70,80,90,100 have sum elements of array loop... how can this?


i'm trying this:

.model small .stack 100h .data w   dw 10,20,30,40,50,60,70,80,90,100 .code  main proc mov ax, @data mov ds, ax  xor ax, ax xor bx, bx mov cx, 10  addnos: add ax, w [bx] add bx, 2 loop addnos   ;this display mov dx, ax mov ah,2 int 21h  mov ah, 4ch int 21h main endp end main  


wrong in display print ascii (&).

edit: updated answer since code in question has been changed:

int 21h / ah=2 prints single character (note integer 1 , character '1' different values).
sum of elements in array 550, requires 3 characters print. way solve write routine converts value 550 string "550", , use int 21h / ah=9 print string. how you'd go doing has been asked several times before on stackoverflow; see e.g. this question , answers it.


this answer original question

for future questions, note "but wrong" terrible problem description. should explain precisely in way code isn't behaving way intended.

that said, there number of problems code:

here you're initializing cx first value in x. actually, since elements in x bytes (because used db) , cx word (two bytes) you'll cx = 301h (which 769 in decimal):

mov cx, x 

here you're moving first element of x bx on , over, instead of doing addition. , again, x contains bytes while bx word register.

top:    mov bx, [x] 

the loop instruction decrements cx 1 , jumps given label if cx != 0. incrementing cx before loop you're creating infinite loop. also, cmp useless (and i'm not sure why you're comparing against 7 since x has 5 elements):

inc cx cmp cx, 7 loop top 

this work values in range 0-9. if sum >=10 require multiple characters. see e.g. this answer example of how convert multi-digit number string can printed. also, you're writing word-sized register byte variable:

add bx, '0' mov [sum], bx 

here i'm bit lost @ you're trying do. if wanted write single character stdout should use int 21h / ah = 2 / dl = character. note mov ax,4 sets ah=0 , al=4. also, should end program int 21h / ax = 4c00h:

display_:     mov dx,1     mov cx, sum     mov bx, 1     mov ax,4     int 21h     mov ax, 1     int 21h 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -