我想编写一个程序,添加两个数字并以十进制形式显示结果,最初它看起来像一块蛋糕,但事实证明,它不是! 因为十进制中只有0-9个字符,当我们想要添加任何大于我们要执行某些数学的数字时,
这就是我所做的, 我想添加两个数字 35和39,
35 + 39 = 74
MOV BL,35H
MOV AL,39H
ADD AL,BL
DAA ;Decimal after Addition => the result of it would be 0074H
PUSH AX ;PRESERVE 0074H
; Separating the two numbers
AND AL,00001111B ; AL => 0000 0100
ADD AL,30H ; ; AL => 0004H + 30H = 4 of Decimal
POP AX ;AX = 74H => 01110100
ROR AL,1
ROR AL,1
ROR AL,1
ROR AL,1
AND AL,00001111B ;AL => 0000 0111
ADD Al,30H ;A: => 0007H + 30H = 7 of Decimal
MOV DL,AL
MOV AH,4CH ;Return Control to the DOS
INT 21H
我恢复了两个数字,但现在如何将结果显示为'74'???
此外,这种方法非常耗时,有没有更好更有效的方法呢?
答案 0 :(得分:2)
您可以将结果打印为单个字符(7为'7',4为'4')或将这些字符组合成字符串,将'$'字符附加到其上并打印该字符串(终端'$ '不会被打印出来。)
DOS具有打印单个字符(ah
= 2,dl
= ASCII字符代码)和$ -terminated字符串(ah
= 9,ds
:{的功能{1}} = $ -terminated string的地址。)
答案 1 :(得分:2)
MOV BL,35H
MOV AL,39H
ADD AL,BL
DAA # al = 74h = 0111.0100
XOR AH,AH # ah = 0 (just in case it wasn't)
# ax = 0000.0000.0111.0100
ROR AX,4 # ax = 0100.0000.0000.0111 = 4007h
SHR AH,4 # ax = 0000.0100.0000.0111 = 0407h
ADD AX,3030h # ax = 0011.0100.0011.0111 = 3437h = ASCII "74" (reversed due to little endian)
现在您只需将AX复制到缓冲区并打印即可。