News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Beginner: Simple Adding code.. Inconsistent results??

Started by rob1218, October 26, 2011, 12:21:02 AM

Previous topic - Next topic

rob1218

This is the first code I have ever written in assembly so excuse my lack of knowledge..  I am writing a simple program to take 2 numbers as input and output the sum of these numbers..  This is what I have so far.


.386


include \masm32\include\masm32rt.inc



.code
start:
mov eax, sval(input("First Number: "))
add eax, sval(input("Second Number: "))
push eax
inkey str$(eax), 13, 10, "Press Enter"
exit

end start


For some reason the answer is right sometimes.. and sometimes it is wrong...
Ex:
I put 40 and 30 and it produces 60

I put 20 and 10 and it produces 20

I put 30 and 30 and it produces 60

It seems to be wrong more often than right..  Any help with this would be greatly appreciated.. I've been trying to research the issue for hours looking over previous posts here with no success :/


Also I am assembling with this command "\masm32\bin\ml /c /Zd /coff program.asm" and linking with this "\masm32\bin\Link SUBSYSTEM:CONSOLE program.obj"
Is this correct? i learned this from another book and not sure if it is appropriate for  this situation.. Please let me know if there is an easier or more proper way of doing this..  Thanks for your time and help!



Gunner

Try this:

include \masm32\include\masm32rt.inc



.code
start:
mov eax, sval(input("First Number: "))
mov ecx, sval(input("Second Number: "))
add eax, ecx

inkey str$(eax), 13, 10, "Press Enter"
exit

end start
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

hutch--

Gunner's comment is correct, what is happening is you are overwriting the EAX register with the following "input" macro.

The solution would be to use 2 memory variables instead of just the registers.


.data?
  var1 dd ?
  var2 dd ?
.code]

mov var1, sval(input("First Number: "))
mov var2, sval(input("Second Number: "))

mov eax, var1,
add eax, var2

print ustr$(eax),13,10

inkey




Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

the EAX, ECX, and EDX registers may be trashed in macros or calls
some macros are written so that they only ovewrite EAX - you have to look at the code to know
however, it is easier to just know those 3 may be overwritten

rob1218

Thanks so much for the fast response!! worked perfectly!!
So is this method using a macro from the masm32rt.inc library??

Can anyone point me in the right direction as to how i can manually get the input and convert it to a number and than add it??

Just for learning purposes i feel as if i am kind of cheating or skipping important learning steps by writing the program in this fashion and not knowing really what its doing?

I assume "sval" is doing a lot of the dirty work for me.. does that make sense?

Thanks!!

hutch--

Rob,

I know where you are coming from but its a difficult task to take on while you are learning. Writing a run time library takes a bit of practice so while you are learning, simply use the macros and the functions they call and you will learn a lot faster. When you have a bit more experience under your belt you can start writing the runtime functions you need.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

it is really educational to see what code the masm32 package macros and functions generate

most of the macros are in the file Masm32\Macros\Macros.asm
UcMacros.asm has macros used for UNICODE
PoMacros.asm has macros for use with PoAsm

all of the Masm32 library functions are in seperate files in the Masm32\M32Lib folder

so - lets have a look at this specific case....

if you look in Macros.asm, use the Find function to search for "sval"
     sval MACRO lpstring       ; string to signed 32 bit integer
       invoke atol, reparg(lpstring)
       EXITM <eax>
     ENDM

you can see that the macro calls the "atol" function and returns the result from EAX

so, to see what the "atol" function looks like, look in the M32Lib folder, atol.asm
you will find the code for that function, written by Hutch or one of his friends   :P

rob1218

Perfect! Exactly what i was looking for.  Thanks for all the responses guy's its been a great help