【问题标题】:Comparing negatives and accumulating in Assembly比较负数并在汇编中累积
【发布时间】: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 中存在这个问题,正如我所说,我不知道添加所有内容时缺少什么,但这可能是唯一的问题?

标签: assembly masm irvine32


【解决方案1】:

您的指令选择正确,但程序流程需要一些修正。

为了清楚起见,这些是相关的比较说明。

JG  - Greather than? (signed numbers e.g. -128 to 127)
JL  - Less than?     (signed numbers e.g. -128 to 127)
JA  - Above?         (unsigned numbers e.g. 0 to 255)
JB  - Below?         (unsigned numbers e.g. 0 to 255)

以下是主循环的固定版本:

...
getLoop:
mov edx, OFFSET prompt2
call WriteString
call ReadInt
; !!!TODO!!! Add an abort condition jumping to 'complete' here!
cmp eax, -100
jl errorloop         ; was 'jg errorLoop' - better goto error if less than -100   
cmp eax, -1
jg errorloop         ; was 'jl complete' - now error on value greater than -1
add result, eax      ; more is not necessary
jmp getLoop

errorLoop:    
mov edx, OFFSET errorPrompt
call WriteString
jmp getLoop

【讨论】:

  • 看起来只有一些问题需要修复,您确定calcLoop 中的代码正确吗?正如我现在看到的那样,只要输入名称,程序就会崩溃。程序应该在哪里调用 calcLoop 来实际将所有数字相加?不知道为什么会崩溃,因为您的说明似乎很好。感谢您的帮助!
  • 我修改的代码(几乎)不会崩溃,因为除了add result, eax 之外,没有任何指令会导致崩溃。所以它必须在其他地方。为什么不使用调试器? calcLoop 实际上是存在的:读取数字后检查范围,然后将 add result, eax 添加到总数 result
  • 好的,我发现它在这条线附近崩溃了mov edx, 0000000h。现在它允许我获取输入,但是当我输入一个正数时会吐出错误,即使那应该是输入的结尾并且它应该在那时计算结果。我猜这就是你建议TODO 跳转到complete 的地方,所以我会搞砸的。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多