Reputation: 1
I'm having some issues with my code, it utilized ReadInt, which then gets put into a variable which I call upon to do some basic math. The point of the program is to ask for 5 numbers, add the first two together, subtract the third, then add the fourth and fifth together, and give you the output. However I keep getting an error somewhere and im not sure where. For instance if I use the numbers 3, 10, 7, 6, 1 the answer should be 13, however my program outputs 14, which leads me to believe that its taking 7+6+1 instead of 3+10-7+6+1
.data
start BYTE "Please enter 5 numbers.", 0dh, 0ah,0
val1 DWORD ?
finish BYTE "The result of adding the first two numbers,subtracting the third", 0dh, 0ah, 0
finish2 BYTE " and adding the remaining two is ", 0dh, 0ah,0
msg BYTE 0dh,0ah,0
.code
main PROC
mov edx, OFFSET start
call WriteString
call ReadInt
mov val1,eax
call ReadInt
add eax, val1
mov val1, eax
call ReadInt
sub val1, eax
mov val1, eax
call ReadInt
add eax, val1
mov val1, eax
call ReadInt
add eax, val1
mov val1, eax
mov eax, val1
mov edx, OFFSET finish
call WriteString
mov edx, OFFSET finish2
call WriteString
mov edx, offset msg
call WriteString
invoke ExitProcess,0
main endp
end main
Upvotes: 0
Views: 56
Reputation: 58427
call ReadInt
sub val1, eax
mov val1, eax <-- this is wrong
Since you're subtracting directly from val1
you shouldn't move eax
to val1
afterwards (i.e. the mov
instruction should should be removed).
What's happening now is that you calculate 3 + 10 - 7 == 6. But then you overwrite val1
with 7, and then you continue by adding 6 and 1 to give you a total of 14.
Upvotes: 1