masm - Comparing negatives and accumulating in Assembly -
i'm having trouble figuring out how compare inputs negatives in masm. positive input integers, use cmp
doesn't seem working me in program.
i'm missing 1 or 2 lines in calcloop
think, because program ends right after user inputs positive value, after inputting negative values. program should add , average values entered, 1 should negatives between [-100,-1]. when input -100 or -1, these should allowed not.
any appreciated i'm still trying figure out of basics of assembly. thanks!
code:
title program3 (program3.asm) include irvine32.inc lengthlimit = 20 .data intro byte "program 3: accumulator ", 13,10 byte "what name: ",0 prompt1 byte "oh hello, ",0 enternumprompt byte "enter numbers between -100 , -1.",13,10 byte "enter non-negative now: ",13,10,0 prompt2 byte "enter #: ",0 prompt3 byte "okay, ",0 prompt4 byte " numbers.",0 addprompt byte "the sum of numbers entered is, ",0 roundprompt byte "the rounded average is: " errorprompt byte "whoops! numbers less or equal -1, please",13,10,0 nameinputlimit byte 24 dup(0) seeya byte "thanks using 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 line or 2 here 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
your instructions correctly chosen, program flow needed fixing.
just clarity, these relevant comparison instructions.
jg - greather than? (signed numbers e.g. -128 127) jl - less than? (signed numbers e.g. -128 127) ja - above? (unsigned numbers e.g. 0 255) jb - below? (unsigned numbers e.g. 0 255)
below fixed version of main loop:
... getloop: mov edx, offset prompt2 call writestring call readint ; !!!todo!!! add abort condition jumping 'complete' here! cmp eax, -100 jl errorloop ; 'jg errorloop' - better goto error if less -100 cmp eax, -1 jg errorloop ; 'jl complete' - error on value greater -1 add result, eax ; more not necessary jmp getloop errorloop: mov edx, offset errorprompt call writestring jmp getloop
Comments
Post a Comment