【发布时间】:2016-02-02 22:51:04
【问题描述】:
我无法弄清楚如何在 MASM 中将输入与负数进行比较。通常对于正输入整数,我只会使用cmp,但这似乎对我在这个程序中不起作用。
我认为我的calcLoop 中也缺少一两行,因为在输入所有负值之后,程序在用户输入正值后立即结束。这个程序应该只是将所有输入的值相加并取平均值,除了一个之外,所有值都应该是 [-100,-1] 之间的负数。此外,当我输入 -100 或 -1 时,应该允许但不允许。
任何帮助都将不胜感激,因为我仍在尝试了解 Assembly 的一些基础知识。谢谢!
代码:
TITLE Program3 (program3.asm)
INCLUDE Irvine32.inc
LENGTHLIMIT = 20
.data
intro BYTE "Program 3: Accumulator by ", 13,10
BYTE "What is your name: ",0
prompt1 BYTE "Oh hello, ",0
enterNumPrompt BYTE "Enter numbers between -100 and -1.",13,10
BYTE "Enter a non-negative now: ",13,10,0
prompt2 BYTE "Enter #: ",0
prompt3 BYTE "Okay, ",0
prompt4 BYTE " numbers.",0
addPrompt BYTE "The sum of the numbers entered is, ",0
roundPrompt BYTE "The rounded average is: "
errorPrompt BYTE "Whoops! Only numbers less than or equal to -1, please",13,10,0
nameInputLimit BYTE 24 DUP(0)
seeya BYTE "Thanks for using the accumulator, "
seeyaName BYTE "!"
numEntered DWORD ?
amountOfNums DWORD ?
result DWORD ?
sumResult DWORD ?
avgResult DWORD ?
.code
main PROC
mov edx, OFFSET intro
call Crlf
call WriteString
mov edx, OFFSET nameInputLimit
mov ecx, SIZEOF nameInputLimit
call ReadString
mov edx, OFFSET prompt1
call WriteString
mov edx, OFFSET nameInputLimit
call WriteString
call Crlf
mov edx, 0000000h
mov edx, OFFSET enterNumPrompt
call WriteString
call Crlf
call Crlf
getLoop:
mov edx, OFFSET prompt2
call WriteString
call ReadInt
cmp eax, -100
jg errorLoop
cmp eax, -1
jl complete
jmp calcLoop
errorLoop:
mov edx, OFFSET errorPrompt
call WriteString
jmp getLoop
calcLoop: ;missing a line or two here I think
mov numEntered, eax
mov eax, result
add eax, numEntered
mov result, eax
jmp getLoop
complete:
call Crlf
mov numEntered, eax
goodbye:
mov edx, OFFSET seeya
call WriteString
exit
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
【问题讨论】:
-
cmp eax, -100; jg errorLoop- 如果值大于 -100,这应该会导致跳转到errorLoop- 这听起来不像你想要的。 -
是的,它应该允许 -100 作为输入,以及介于 -100 和 -1 之间的任何值,但不能超出该范围,所以如果我输入 -101 和 0,我认为它不会好好工作。问题是我认为我没有正确比较否定,也许我应该使用不同的语法。还是我的 calcLoop 中存在这个问题,正如我所说,我不知道添加所有内容时缺少什么,但这可能是唯一的问题?