Search code examples
assemblymasmirvine32

Comparing negatives and accumulating in Assembly


I'm having trouble figuring out how to compare inputs to negatives in MASM. Usually for positive input integers, I would just use cmp but that doesn't seem to be working for me in this program.

I'm also missing one or two lines in my calcLoop I think, because the program just ends right after the user inputs the positive value, after inputting all the negative values. This program should just add up and average all the values entered, all but one should be negatives between [-100,-1]. Also when I input -100 or -1, these should be allowed but are not.

Any help would be greatly appreciated as I'm still trying to figure out some of the basics of Assembly. Thanks!

Code:

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

Solution

  • Your instructions are correctly chosen, but the program flow needed some fixing.

    Just for clarity, these are the relevant comparison instructions.

    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)
    

    Below is a fixed version of your main loop:

    ...
    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