我如何修复我的代码中Msg3中的显示?

问题描述 投票:0回答:1

我不知道我应该在代码中更改什么

.型号小 .堆叠100小时

.数据 Msg1 DB '输入第一个数字:$' Msg2 DB 10, 13, '输入第二个数字:$' Msg3 DB 10, 13, '输入的数字是:$' PositiveMsg DB ' 是正数$' NegativeMsg DB ' 为负$' 逗号 DB ', $' 新行 DB 10, 13, '$' 输入缓冲区 DB 6 DUP('$')

.代码 主程序 移动斧头,@data mov ds, 斧头

; Display message asking for the first number
mov ah, 09h
lea dx, Msg1
int 21h

; Read the first character from the user
mov ah, 01h
int 21h
mov bl, al           ; Save the first character
cmp al, '-'          ; Check if the first character is '-'
je ReadFirstNegative
sub al, '0'          ; Convert ASCII character to its corresponding numeric value
jmp FirstNumberProcessed

先读负数: 移动啊,01h 21小时内 子阿尔,'0';将 ASCII 字符转换为其对应的数值 否定 ;对值求负使其变为负数

处理的第一个数字: 莫夫·bh,阿尔;将第一个数字的数值移至 BH

; Display message asking for the second number
mov ah, 09h
lea dx, Msg2
int 21h

; Read the second character from the user
mov ah, 01h
int 21h
mov bl, al           ; Save the first character
cmp al, '-'          ; Check if the first character is '-'
je ReadSecondNegative
sub al, '0'          ; Convert ASCII character to its corresponding numeric value
jmp SecondNumberProcessed

读取第二个负数: 移动啊,01h 21小时内 子阿尔,'0';将 ASCII 字符转换为其对应的数值 否定 ;对值求负使其变为负数

已处理的第二个数字: 莫夫·DH,艾尔;将第二个数字的数值移至DH

; Display message indicating the entered numbers
mov ah, 09h
lea dx, Msg3
int 21h

; Display the first entered number
mov al, bh           ; Move the numeric value of the first number to AL
add al, '0'          ; Convert numeric value to its corresponding ASCII character
mov dl, al
mov ah, 02h
int 21h

; Determine if the first number is positive or negative
mov ah, 09h
cmp bh, 0
jge FirstPositive
lea dx, NegativeMsg
int 21h
jmp CheckSecondNumber

第一个优点: lea dx, PositiveMsg 21小时内

检查第二个号码: ;显示逗号和空格 移动啊,09h lea dx, 逗号 21小时内

; Display the second entered number
mov al, dh           ; Move the numeric value of the second number to AL
add al, '0'          ; Convert numeric value to its corresponding ASCII character
mov dl, al
mov ah, 02h
int 21h

; Determine if the second number is positive or negative
mov ah, 09h
cmp dh, 0
jge SecondPositive
lea dx, NegativeMsg
int 21h
jmp ExitProgram

第二个优点: lea dx, PositiveMsg 21小时内

退出程序: ;退出程序 莫夫啊,4Ch 21小时内 主要终点

主线结束

here in Msg3 the output is wrong

它应该确定我将在Msg1或Msg2中输入什么数字。应该是“1为正,-2为负”

assembly dosbox
1个回答
0
投票

问题1:未打印正确的数字。

读取数字时,如果用户先输入“-”,则会对

al
after converting from ASCII 的值取反,并将其存储在
bh
/
dh
中。所以
bh
/
dh
持有正数或负数。

当您尝试打印它时,您只是将其转换为 ASCII,而无需查看符号。因此,-2 + '0' 不会生成 '2' (老实说,我很惊讶它不会生成 '.')。

您还忘记了在打印数字之前可能需要打印出“-”。

问题2:总是说积极的。

我相信这是因为

cmp bh, 0
。由于取反字节只是设置了高位的字节(例如 -2 = 11111110),因此
cmp
可能会错误地将其视为 254 - 因此它始终大于 0,并且
jge
始终会重定向。它“应该”是一个有符号减法,但这可能只能在完整的 ebx
edx
寄存器上正常工作。我的汇编程序有点生疏了。
我让你找到一种方法来测试最上面的部分,以确定它是否是负面的。您可能希望始终将正值保留在 

bh / dh

中,并使用其他方式来记住第一个和第二个数字是否为负数 - 这会简化其余的操作。

    

© www.soinside.com 2019 - 2024. All rights reserved.