我是汇编代码新手,我正在尝试创建一个程序来添加用户的 2 个两位数值。但结果是ASCII符号,怎么才能显示十进制数呢?
.model small
.stack 100h
.data
digit1 db "enter first digit: $"
digit2 db 10,13,"enter 2nd digit: $"
number1 db 0
number2 db 0
first_number db 0
second_number db 0
result db 0
result_digit1 db 0
result_digit2 db 0
.code
main proc
;Prompt the user to enter the first number
mov ax,@data
mov ds,ax
lea dx, digit1
mov ah, 09h
int 21h
; Read the first digit of the first number into AL
mov ah, 01h`your text`
int 21h
sub al, 30h ; convert the ASCII code to its corresponding number
mov [number1], al
; Read the second digit of the first number into AL
mov ah, 01h
int 21h
sub al, 30h ; convert the ASCII code to its corresponding number
mov [number2], al
; Combine the digits to form the first number
mov al, [number1]
mov ah, [number2]
mov bx, 10
mul bx
add al, ah
mov [first_number], al
; Prompt the user to enter the second number
mov ah, 09h
mov dx, offset digit2
int 21h
; Read the first digit of the second number into AL
mov ah, 01h
int 21h
sub al, 30h ; convert the ASCII code to its corresponding number
mov [number1], al
; Read the second digit of the second number into AL
mov ah, 01h
int 21h
sub al, 30h ; convert the ASCII code to its corresponding number
mov [number2], al
; Combine the digits to form the second number
mov al, [number1]
mov ah, [number2]
mov bx, 10
mul bx
add al, ah
mov [second_number], al
; Add the two numbers
mov al, [first_number]
add al, [second_number]
mov [result], al
; Convert the result to ASCII code
add al, 30h
mov [result_digit1], al
mov al, [result]
div bx
mov [result_digit2], al
add al, 30h
; Print the result
mov ah, 02h
mov dl, [result_digit2]
int 21h
mov dl, [result_digit1]
int 21h
Main endp
End main
例如,当我输入 11 + 22 时,我期望的结果是 33,但我得到的答案是 ASCII 符号而不是整数。我怎样才能得到整数而不是 ASCII 符号
使用
mul bx
,您正在执行 字大小 乘法,将 AX 乘以 BX。您需要使用字节大小乘法将最高有效数字乘以10,然后添加最低有效数字:
mov al, 10
mul number1
add al, number2
mov [first_number], al
与second_number类似。
当输入 11 + 22 时,我期望的结果是 33,但我得到的答案是 ASCII 符号而不是整数。
您的转换代码添加 30h 值太快了!
第一步需要使用 byte-sized 除以 10 来分解 result 中的值:
mov al, [result] ; [0,99]
cbw ; Sets AH=0 because DIV BL not just divides AL, but rather the whole of AX
mov bl, 10
div bl ; Does AX / BL -> AL is tens, AH is ones
下一步将这些数字转换为文本字符:
add ax, '00' ; Same as `add ax, 3030h`
最后一步打印数字:
mov dx, ax ; -> DL is tens
mov ah, 02h
int 21h
mov dl, dh ; -> DL is ones
int 21h
; Print the result mov ah, 02h mov dl, [result_digit2] int 21h mov dl, [result_digit1] int 21h Main endp End main
最后两行不足以结束您的程序。您需要使用 DOS 程序终止功能之一:
mov ax, 4C00h ; DOS.TerminateWithReturncode
int 21h
Main endp
End Main