我的项目中有这段代码。它会从交易中取出所有金额并将其添加并显示您当天赚了多少钱。问题是它没有以十进制格式显示以及是否显示 3 位数字。
结论是我希望它以十进制格式显示 3 位数字。
show_amount:
mov dx, offset amount_earned
mov ah, 9
int 21h
mov ax, 00h
mov al, amount
AAM
mov ch, ah
mov cl, al
add ch, 48 ; Convert the first digit to ASCII decimal
add cl, 48 ; Convert the second digit to ASCII decimal
mov dl, ch
mov ah, 2
int 21h
mov dl, cl
mov ah, 2
int 21h
cmp dl, 48 ; Check if the number is less than 100
jl skip_zero
mov dl, 48 ; If it's 100 or more, set the first digit to 0
mov ah, 2
int 21h
skip_zero:
add dl, 48 ; Convert the number to ASCII decimal
mov ah, 2
int 21h
mov dl, 0 ; Move 0 to DL register for decimal conversion
mov ah, 2
int 21h
jmp start
我已经尝试了上面的代码,其中总交易量为 72+72=144,但它显示:
>40`
关于
aam
指令:参见EMU8086中的AAM指令。
将示例金额 144 加载到 AL 寄存器中(无需预先将 AX 清零),
aam
指令会将 AL 除以 10,将余数 (4) 存储在 AL 中,并将商 (14) 存储在 AH 中。对于余数,没有问题,因为它在 [0,9] 范围内,您可以添加 48 将其转换为十进制字符。但是,如果对大于 9 且超出范围的商应用相同的加法,则会得到无意义的“>”字符。要解决此任务,您需要执行
aam
两次。第二次你需要将它应用到你第一次得到的商。
mov al, amount ; 1st time
aam ; 144 / 10 -> quotient AH=14, remainder AL=4
add al, '0'
push ax ; (1)
mov al, ah ; 2nd time using quotient from 1st time
aam ; 14 / 10 -> quotient AH=1, remainder AL=4
add ax, '00'
xchg al, ah
mov dx, ax ; -> DL="1" DH="4"
mov ah, 02h ; DL="1"
int 21h
mov dl, dh ; -> DL="4"
int 21h
pop dx ; (1) -> DL="4"
int 21h