在 x86 程序集中读取大于 9 的整数
Reading integers > 9 in x86 assembly
我想从 x86
程序集中的输入中读取 integers
,但是当 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:
我希望我的程序在 startcalc
标签开头的 ax
寄存器中存储正确的数字。
在这个程序中我应该做什么,我应该改变什么?
为什么你在 AX
中得到错误答案?
只要用户按下回车键,您就会停止聚合整数的过程。因此,当用户按下回车键时,存储在 AX
中的聚合整数将被 010D
替换。 01
因为它是您用于从键盘获取整数的 INT 21h 服务(它将 AH
的内容替换为 01
)。而 0D
因为它是 enter 键的十六进制值(它将 AL
的内容替换为 0D
)。所以 AX
寄存器的值不再存储结果(聚合整数)。
如何解决?
I want my program to store the correct number in ax register at the start of startcalc label.
只需在 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 和 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 之间。
我想从 x86
程序集中的输入中读取 integers
,但是当 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:
我希望我的程序在 startcalc
标签开头的 ax
寄存器中存储正确的数字。
在这个程序中我应该做什么,我应该改变什么?
为什么你在 AX
中得到错误答案?
只要用户按下回车键,您就会停止聚合整数的过程。因此,当用户按下回车键时,存储在 AX
中的聚合整数将被 010D
替换。 01
因为它是您用于从键盘获取整数的 INT 21h 服务(它将 AH
的内容替换为 01
)。而 0D
因为它是 enter 键的十六进制值(它将 AL
的内容替换为 0D
)。所以 AX
寄存器的值不再存储结果(聚合整数)。
如何解决?
I want my program to store the correct number in ax register at the start of startcalc label.
只需在 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 和 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 之间。