嘿我正在使用7 x86窗口。我想添加两个16位数字。
当我添加3+3
时,其答案是正确的,但是当我添加7+7
时,它无效。我想添加两个数字,如75+75
,答案应为150。
它的程序请告诉我。 Thanx提前
.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
答案 0 :(得分:2)
以下是在8086上添加2个16位数字的代码:
.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
这里的诀窍在于使用'aaa'命令解压缩数字。
答案 1 :(得分:1)
使用INT 21h Fn 02
,您只能获得一个字符。要接收更多字符,您必须创建一个棘手的循环。但是DOS中还有另一个功能:INT 21h Fn 0Ah
。要转换大于一位数的数字,您需要两个转换例程 - 在您的教科书中必须详细说明。看看我的例子:
.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