Hey I am using window 7 x86. I want to add two 16 bit numbers.
When I add 3+3
its answer is correct but when I add 7+7
it's not working. And I want to add two numbers like 75+75
its answer should be 150.
What is its procedure please tell me. Thanx in advance
.model small
.stack 100h
.data
num db 9 dup(0)
result dw 9 dup (0)
.code
main proc
mov ax,@data
mov ds,ax
mov ah, 1
int 21h ; get input from user
mov num, al ; store in the array
int 21h ;get 2nd number from user
mov num+1, al ;store in the array at num[1] index
mov al, num ;mov number into al
add dl, num+1 ;add num[1] in the num which is in dl
sub dl, 48 ; subract from assci so it become number 0 ~ 9
mov ah, 2 ; output
int 21h
mov ah, 4ch
int 21h
main endp
end main
Here is the code to add 2 16-bit numbers on 8086:
.model small
.data
a db "Enter the first number$"
b db "Enter the second number$"
c db "The sum is: $"
d db 00h
.code
start:
mov ax,@data
mov ds,ax
mov dx,offset a
mov ah,09h
int 21h
mov ah,01h
int 21h
mov bh,al
mov ah,01h
int 21h
mov bl,al
mov dx,offset b
mov ah,09h
int 21h
mov ah,01h
int 21h
mov ch,al
mov ah,01h
int 21h
mov cl,al
add al,bl
mov ah,00h
aaa
add bh,ah
add bh,ch
mov d,al
mov al,bh
mov ah,00h
aaa
mov bx,ax
add bx,3030h
mov dx,offset c
mov ah,09h
int 21h
mov dl,bh
mov ah,02h
int 21h
mov dl,bl
mov ah,02h
int 21h
mov dl,d
add dl,30h
mov ah,02h
int 21h
end start
The trick here lies in using 'aaa' command to unpack the digits.
With INT 21h Fn 02
you can get only one character. To receive more characters you must create a tricky loop. But there is another function in DOS: INT 21h Fn 0Ah
. For conversion of a number greater than one digit you need two conversion routines - surely detailed explained in your schoolbook. Take a look at my example:
.MODEL small
.386
.STACK 1000h
.data
num label
max db len
real db 0
buf db 6 dup(0) ; Input (5 digits) + CR
len = $-buf
db 'ENDE'
int1 dw 0
int2 dw 0
int3 dw 0
result db 6 dup ('$') ; Output (5 digits) + CR
.code
main PROC
mov ax,@data
mov ds,ax ; Init DS
mov es,ax ; Init ES for stosb
mov dx, OFFSET num
mov ah, 0Ah ; Input a string
int 21h
call dec2int
mov [int1], ax
mov dl, 0Ah ; Linefeed
mov ah, 02h ; Cooked Output one character
int 21h
mov dx, OFFSET num
mov ah, 0Ah ; Input a string
int 21h
call dec2int
mov [int2], ax
mov ax, [int1] ; first number
add ax, [int2] ; add with second number
mov [int3], ax ; Store result in [int3]
mov dl, 0Ah ; Linefeed
mov ah, 02h ; Cooked Output one character
int 21h
mov di, OFFSET result ; [ES:DI] = receives the result string
mov ax, [int3] ; AX = result from addition
call int2dec
mov dx, OFFSET result
mov ah, 09h ; Output until '$'
int 21h
mov ax, 4C00h ; Exit(0)
int 21h
main ENDP
dec2int PROC
xor ax, ax ; AX receives the result
mov si, OFFSET buf
movzx cx, byte ptr [real] ; Number of characters
test cx, cx ; Buffer empty?
jz _Ret ; yes: return with AX=0
_Loop: ; Repeat: AX = AX * 10 + DX
imul ax, 10
mov dl, byte ptr [si]
and dx, 000Fh ; Convert ASCII to integer
add ax, dx
inc si
loop _Loop
_Ret:
ret
dec2int ENDP
int2dec PROC
mov bx, 10 ; Base 10 -> divisor
xor cx, cx ; CX=0 (number of digits)
Loop_1:
xor dx, dx ; No DX for division
div bx ; AX = DX:AX / BX Remainder DX
push dx ; Push remainder for LIFO in Loop_2
add cl, 1 ; Equivalent to 'inc cl'
or ax, ax ; AX = 0?
jnz Loop_1 ; No: once more
Loop_2:
pop ax ; Get back pushed digits
or ax, 00110000b ; Conversion to ASCII
stosb ; Store only AL to [ES:DI] (DI is a pointer to a string)
loop Loop_2 ; Until there are no digits left
mov al, '$' ; Termination character for 'int 21h fn 09h'
stosb ; Store AL
ret
int2dec ENDP
END main
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.