how-to-do-addition-using-loop-in-assembly-language

let say we want to add 3 numbers like 1, 2, 3 using loop in assembly language;

it will work like, 1+2+3 = 6
0+1 = 1
1+2 = 3
3+3 = 6

code:

.model small

.stack 100h

.data 

msg db 'sum: $'

.code

main proc

    mov ax,@data

    mov ds,ax  

    

    mov bl,0  ;al=sum

    mov dl,1  ;bl = i 

    mov cx,3  ;repeat loop 3 times

    

    loop_start:

    add bl,dl   ;sum+=i

    inc dl ; i++ 

    loop loop_start  ;loop until cx = 0

   

    

    ;print message

    mov ah,9

    lea dx,msg

    int 21h

    

    ;print result

    mov ah,2 

    add bl,48 ;convert to ascii for display

    mov dl,bl

    int 21h

    exit:

    mov ah,4ch

    int 21h

    main endp

end main


resutl: 

sum = 6






    

No comments

Theme images by hdoddema. Powered by Blogger.