在 Assembly 中比较底片和积累

Comparing negatives and accumulating in Assembly

我无法弄清楚如何在 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

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

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

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