assembly 如何添加值并获得十进制数结果而不是ASCII符号

zxlwwiss  于 2023-02-08  发布在  其他
关注(0)|答案(1)|浏览(119)

我是汇编代码的新手,我正在尝试创建一个程序来添加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符号

qrjkbowd

qrjkbowd1#

请先检查输入

使用mul bx,您将执行字长乘法,即将AX乘以BX。您需要使用字节大小乘法将最高有效位乘以10,然后将最低有效位加到它上面:

mov  al, 10
mul  number1
add  al, number2
mov  [first_number], al
  • second_number* 也是如此。

如何输出高达99的结果

当输入11 + 22时我期望的结果是33,但我得到的答案是ASCII符号而不是整数。
您的转换代码添加30 h值太早!
第一步需要使用字节大小的除以10来分解 result 中的值:

mov  al, [result]   ; [0,99]
cbw                 ; Will perform AX / BL
mov  bl, 10
div  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

相关问题