如何将用户输入的16位数字转换为十进制 [英] How to convert a 16 bit number entered by user to decimal
问题描述
我让用户输入一个 16 位数字.我想显示用户输入的数字.
这是我目前想到的.
如您所见,我已从输入中减去 30H 以将其转换为十进制.我应该在哪里添加 30H 以使其恢复到其原始 ASCII 值?
MOV AH,1H ;用户输入的第一个数字部分 1INT 21HSUB AL,30HMOV NUM1,ALMOV AH,1H ;第一个数字第 2 部分INT 21HSUB AL,30HMOV NUM2,ALMOV AH,1H ;第一个数字第 3 部分INT 21HSUB AL,30HMOV NUM3,ALMOV AH,1H ;第一个数字第 4 部分INT 21HSUB AL,30HMOV NUM4,AL异或啊,啊MOV AL,NUM1MOV DX,1000DMUL DX ;1*1000添加 AH,30H添加AL,30HMOV BX,AX异或啊,啊MOV AL,NUM2MOV DX,100DMUL DX ;2*100添加 BX,AX异或啊,啊MOV AL,NUM3MOV DX,0010DMUL DX ;3*10添加 AH,30H添加AL,30H添加 BX,AX异或 CH,CHMOV CL,NUM4添加 CH,30H添加 CL,30HADD BX,CX ;BX 现在有 16 位数字MOV FNUM1,BX ;最后的第一个 16 位数字打印LEA DX,MSG8 ;msg 输出MOV AH,9HINT 21H打印 FNUM1
例如:当我输入数字为 1234 时,我得到的输出为 F.你们能告诉我我做错了什么并帮助我吗?在这一点上,任何帮助将不胜感激.
首先要正确输入.
从输入中获得 4 位数字后,您需要按照公式将它们组合起来d1 * 1000 + d2 * 100 + d3 *10 + d4
在这个计算中,你不需要加上 48!
*1000 的乘法需要一个字大小的 mul
,但是 *100 和 *10 的乘法可以用一个字节大小的 mul
.
mov al, NUM1cbw ;->AH=0mov dx, 1000mul dx ;DX:AX 中的乘积 1*1000 但 DX=0mov bx, ax移动, 100mul NUM2 ;产品 2*100 in AX添加 bx, ax移动, 10mul NUM3 ;产品 3*10 in AX添加 bx, ax添加 bl, NUM4adc bh, 0 ;BX 现在有 16 位数字
上述更好的版本使用循环.只有将 NUMx 变量定义为内存中的连续字节时,此循环才有效!
NUM1 db 0NUM2 分贝 0NUM3 分贝 0NUM4 分贝 0...mov cx, 4 ;位数lea si, NUM1 ;第一位(最高有效位)的地址异或 bx, bx ;16 位结果更多的:imul bx, 10 ;BX = BX * 10lodsb ;NUM1然后NUM2然后NUM3然后NUM4体重添加 bx, ax十二月更多
然后输出到屏幕.
<块引用>我想显示用户输入的数字.
将存储在BX
(来自前面的步骤)中的16位数字移动到AX
寄存器,然后阅读我关于如何转换16位数字的说明在 AX
中转换为文本,以便它可以打印到屏幕上 用 DOS 显示数字.
I got the user to enter a 16 bit number. I want to display the number entered by the user.
This is what I've come up with so far.
As you can see, I have subtracted 30H from the input to convert it to decimal. Where should I add 30H back to get it back to its original ASCII value?
MOV AH,1H ;user input for first number part 1
INT 21H
SUB AL,30H
MOV NUM1,AL
MOV AH,1H ;1st number part 2
INT 21H
SUB AL,30H
MOV NUM2,AL
MOV AH,1H ;1st number part 3
INT 21H
SUB AL,30H
MOV NUM3,AL
MOV AH,1H ;1st number part 4
INT 21H
SUB AL,30H
MOV NUM4,AL
XOR AH,AH
MOV AL,NUM1
MOV DX,1000D
MUL DX ;1*1000
ADD AH,30H
ADD AL,30H
MOV BX,AX
XOR AH,AH
MOV AL,NUM2
MOV DX,100D
MUL DX ;2*100
ADD BX,AX
XOR AH,AH
MOV AL,NUM3
MOV DX,0010D
MUL DX ;3*10
ADD AH,30H
ADD AL,30H
ADD BX,AX
XOR CH,CH
MOV CL,NUM4
ADD CH,30H
ADD CL,30H
ADD BX,CX ;BX now has the 16 bit number
MOV FNUM1,BX ;final 1st 16 bit number
PRINTN
LEA DX,MSG8 ;msg for output
MOV AH,9H
INT 21H
PRINT FNUM1
For eg: when I enter the number as 1234, I get output as F. Can you guys let me know what I'm doing wrong and help me out? Any help would be appreciated at this point.
First get the input right.
Once you got the 4 digits from input you need to combine them adhering to the formula
d1 * 1000 + d2 * 100 + d3 *10 + d4
Nowhere in this calculation are you required to add in 48!
The multiplication *1000 needs a word-sized mul
, but multiplying *100 and *10 can do with a byte-sized mul
.
mov al, NUM1
cbw ; -> AH=0
mov dx, 1000
mul dx ; Product 1*1000 in DX:AX but with DX=0
mov bx, ax
mov al, 100
mul NUM2 ; Product 2*100 in AX
add bx, ax
mov al, 10
mul NUM3 ; Product 3*10 in AX
add bx, ax
add bl, NUM4
adc bh, 0 ; BX now has the 16 bit number
A nicer version of the above uses a loop. This loop can only work if you define your NUMx variables as consecutive bytes in memory!
NUM1 db 0
NUM2 db 0
NUM3 db 0
NUM4 db 0
...
mov cx, 4 ; Number of digits
lea si, NUM1 ; Address of 1st digit (most significant digit)
xor bx, bx ; The 16-bit result
More:
imul bx, 10 ; BX = BX * 10
lodsb ; NUM1 then NUM2 then NUM3 then NUM4
cbw
add bx, ax
dec cx
jnz More
Then output to the screen.
I want to display the number entered by the user.
Move the 16-bit number stored in BX
(from the previous steps) to the AX
register and then read my explanations on how to convert a 16-bit number in AX
into text so it can be printed to the screen at Displaying numbers with DOS.
这篇关于如何将用户输入的16位数字转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!