Assembly 读取整数>;x86汇编中的9

Assembly 读取整数>;x86汇编中的9,assembly,x86,dos,real-mode,Assembly,X86,Dos,Real Mode,我想从x86程序集中的输入中读取整数,但是当整数大于9时,在读取时遇到一些问题 我尝试了以下代码(在internet上找到): 我希望我的程序在startcalc标签开始处的ax寄存器中存储正确的数字 在这个程序中我应该做什么?我应该更改什么?为什么在AX中得到错误的答案? 只要用户按enter键,就停止聚合整数的过程。因此,当用户点击enter键时,存储在AX中的聚合整数将被010D替换01因为它是INT 21h的服务,您使用它从键盘获取整数(它将把AH的内容替换为01)。和0D,因为它是en

我想从
x86
程序集中的输入中读取
整数
,但是当
整数
大于9时,在读取时遇到一些问题

我尝试了以下代码(在internet上找到):

我希望我的程序在
startcalc
标签开始处的
ax
寄存器中存储正确的数字


在这个程序中我应该做什么?我应该更改什么?

为什么在
AX
中得到错误的答案?

只要用户按enter键,就停止聚合整数的过程。因此,当用户点击enter键时,存储在
AX
中的聚合整数将被
010D
替换
01
因为它是INT 21h的服务,您使用它从键盘获取整数(它将把
AH
的内容替换为
01
)。和
0D
,因为它是enter键的十六进制值(将
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
  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
现在,您可以访问存储正确结果的
AX

如前所述,如果您想要更大的数字,您必须使用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
来存储结果,我会使用如下内容:

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
  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之间。

代码似乎在ax(lo)中存储了正确的金额,但ax(hi)中还有一个额外的1。为什么会这样?你认为可能是因为你做了
mov-ah,1
?我应该做什么来代替mov-ah,1?没有办法,因为中断需要它。您只需保存/恢复
ax
或使用其他寄存器即可。实际上,我不确定您希望得到的结果在哪里。在startcalc标签的开头,我希望输入保存在ax寄存器中。。。一切都是正确的,但例如,当我输入132时,在lo中我正确地有0D,但在hi中我有01。。。我应该改成00