8086汇编简易计算器结果显示与运算执行错误排查求助
8086汇编计算器程序的显示与运算错误修复
问题描述
编写的8086汇编简易计算器程序,预期实现用户选择四则运算、输入两次最多5位整数并显示结果的功能,但存在以下问题:
- 输入第二个数字后,
Enter 2nd Number:提示被覆盖 - 显示异常内容(例如输入21时,显示为
The result is: 0: 21) - 运算结果错误
问题根源分析
- 数字转换逻辑错误:
readNumber过程中,mul cx的计算顺序错误。当cx初始为0时,第一次执行mul cx会将当前读取的数字(al)清零,导致第一个数字丢失,最终num2被错误赋值,引发运算结果异常。 - 输入后未处理换行:使用
int 21h 0Ah读取字符串后,用户按下的回车(0Dh)未被处理,残留的控制字符会干扰后续的输出流程,导致提示被覆盖。
修复后的完整代码
.model small .stack 100h .data greeting db 'WELCOME TO YOUR CALCULATOR', 0Dh, 0Ah, '$' menu db '1 - ADDITION', 0Dh, 0Ah, '2 - SUBTRACTION', 0Dh, 0Ah, '3 - MULTIPLICATION', 0Dh, 0Ah, '4 - DIVISION', 0Dh, 0Ah, '$' choicePrompt db 'Enter your choice: $' prompt1 db 'Enter 1st Number: $' prompt2 db 'Enter 2nd Number: $' resultMsg db 'The result is: $' buffer db 6, ?, 5 dup(0) ; Buffer to store input (max 5 digits) userChoice db ? num1 dw 0 num2 dw 0 result dw 0 .code main proc mov ax, @data mov ds, ax ; Print the greeting and menu lea dx, greeting mov ah, 09h int 21h lea dx, menu mov ah, 09h int 21h ; Print choice prompt lea dx, choicePrompt mov ah, 09h int 21h ; Read user's choice mov ah, 01h int 21h sub al, '0' mov userChoice, al call getch ; Eat the enter key ; Print newline call printNewline ; Print prompt1 and read first number lea dx, prompt1 mov ah, 09h int 21h call readNumber mov num1, ax call printNewline ; Print prompt2 and read second number lea dx, prompt2 mov ah, 09h int 21h call readNumber mov num2, ax ; Perform the chosen operation mov ax, num1 mov bx, num2 cmp userChoice, 1 je additionOp cmp userChoice, 2 je subtractionOp cmp userChoice, 3 je multiplicationOp cmp userChoice, 4 je divisionOp jmp endProgram additionOp: add ax, bx mov result, ax ; Store the result jmp displayResult subtractionOp: sub ax, bx mov result, ax ; Store the result jmp displayResult multiplicationOp: mul bx mov result, ax ; Store the result jmp displayResult divisionOp: xor dx, dx div bx mov result, ax ; Store the result jmp displayResult displayResult: ; Print the result message lea dx, resultMsg mov ah, 09h int 21h ; Print the actual result mov ax, result call printNumber ; Print a newline call printNewline ret endProgram: mov ah, 4Ch int 21h main endp getch proc mov ah, 07h int 21h ret getch endp readNumber proc lea dx, buffer mov ah, 0Ah int 21h ; Handle remaining enter key after input call printNewline lea si, buffer + 2 mov cx, 0 convertLoop: lodsb cmp al, 0Dh je endConvert sub al, '0' ; Correct number conversion logic: cx = cx*10 + current digit mov ax, cx mov bx, 10 mul bx add ax, al mov cx, ax jmp convertLoop endConvert: mov ax, cx ret readNumber endp printNewline proc mov ah, 02h mov dl, 0Dh int 21h mov dl, 0Ah int 21h ret printNewline endp printNumber proc mov bx, 10 xor cx, cx convertLoop2: xor dx, dx div bx add dl, '0' push dx inc cx cmp ax, 0 jne convertLoop2 printLoop: pop dx mov ah, 02h int 21h loop printLoop ret printNumber endp end main
关键修改点说明
修复数字转换逻辑:
在readNumber的convertLoop中,调整计算顺序为累计值 = 累计值*10 + 当前数字,解决了初始cx=0时第一个数字被清零的问题,确保输入的数字能正确转换为数值。添加输入后换行处理:
在readNumber中调用printNewline,处理int 21h 0Ah读取输入后残留的回车字符,避免干扰后续的提示输出,解决提示被覆盖的问题。验证运算逻辑:
四则运算的核心逻辑本身正确,修复数字输入问题后,运算结果即可正常计算和显示。
内容的提问来源于stack exchange,提问作者Rusty Dalit
相关产品推荐
相关产品推荐

