我想从integers
汇编中的输入中读取x86
,但是当integer
大于9时会出现问题。
我尝试了以下代码(在互联网上找到):
.code
mov bh,0
mov bl,10
inputloop:
mov ah,1
int 21h
cmp al,13
jne convertion
jmp startcalc
convertion:
sub al,48
mov cl,al
mov al,bh
mul bl
add al,cl
mov bh,al
jmp inputloop
startcalc:
我希望我的程序在ax
标签开头的startcalc
寄存器中存储正确的数字。
我该怎么做以及我应该在这个程序中做些什么改变?
答案 0 :(得分:1)
为什么你在AX
得到错误答案?
每当用户点击输入密钥时,您就会停止聚合整数的过程。因此,当用户点击enter键时,AX
中存储的聚合整数将被010D
替换。 01
因为它是用于从键盘获取整数的INT 21h的服务(它将AH
的内容替换为01
)。并0D
因为它是输入键的十六进制值(将AL
的内容替换为0D
)。因此AX
寄存器的值不再存储结果(聚合整数)。
如何解决?
我希望我的程序在startcalc标签的开头将正确的数字存储在ax寄存器中。
只需在startcalc
标签的开头添加以下代码行,如下所示:
startcalc:
mov ah, 0 ; Get rid of 01 in AH
mov bl, 0 ; Get rid of 0A in BL
mov al, bh ; Store the result in AL
现在您可以访问存储正确结果的AX
。
正如Jester所说,如果你想要更大的数字,你将不得不使用16位寄存器,这可以通过以下方式完成:
16位数代码:
要允许您的程序接受16位数字,请执行以下操作:
替换它:
mov bh,0
mov bl,10
由此:
mov bx, 0
mov di,10 ; DI is a 16bit register that will store our divisor
这个: -
sub al,48
mov cl,al
mov al,bh
mul bl
add al,cl
mov bh,al
由此: -
sub al, 48
mov ah, 0
mov cx,ax ; using 16bit version of CX and AX (store AX into CX)
mov ax,bx ; store the previous value of BX into AX
mul di ; multiple the AX with DI
add ax,cx ; add the current integer (CX) with the multiplication result
mov bx,ax ; store AX into BX
然后最后: -
startcalc:
mov ah, 0
mov bl, 0
mov al, bh
由此: -
startcalc:
mov ax, bx
希望有所帮助。 :)
答案 1 :(得分:0)
哦!我的!该代码将输入限制在1字节以内的数字,因此在0到255之间。
使用bx
存储结果,我会使用以下内容:
mov bx, 0
inputloop:
mov ah, 1 ; getc()
int 21h
cmp al, 13 ; enter?
je startcalc
sub al, 48 ; character to number (assuming user only types '0' to '9')
mov ah, 0 ; clear ah
sll bx, 1 ; x2
mov cx, bx
sll bx, 2 ; x4 (so x8 total)
add bx, cx ; x2 + x8 = x10
add bx, ax
jmp inputloop
startcalc:
mov ax, bx ; if you want the result in ax instead
现在你的号码至少在0到65535之间。