News:

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

String>DWord and back question

Started by MickyD, February 24, 2008, 02:57:13 PM

Previous topic - Next topic

MickyD

Hello, first post for a spot of help! :)

I'm learning assembly, its a thing I've always wanted to do and thought sod it, I'll give it a go! :)

Basically, while learning C/C++ I found strings a bit of a bugger, but knuckled in and got over them, so I thought I'd do the same with assembly, so, while spending about an hour with MASM32 (Dont know which version) I've come up with this:-


.386
.model flat, stdcall
option casemap :none   ; case sensitive

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
         
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

Main   PROTO

.data
sVal1Prompt        db "Enter first number:",0
sVal2Prompt        db "Enter second number:",0
sShowResult        db "The values when added are ",0

dwVal1  DWORD ?
dwVal2  DWORD ?


.code

start:
invoke Main
invoke ExitProcess,0

Main proc

LOCAL sVal1[128]:BYTE
LOCAL sVal2[128]:BYTE
LOCAL result[128]:BYTE

invoke StdOut, ADDR sVal1Prompt
invoke StdIn, ADDR sVal1, LENGTHOF sVal1

invoke atodw, ADDR sVal1

mov dwVal1,eax

invoke StdOut, ADDR sVal2Prompt
invoke StdIn, ADDR sVal2, LENGTHOF sVal2

invoke atodw, ADDR sVal2
   
mov dwVal2,eax

mov eax, dwVal1
add eax, dwVal2

mov dwVal2,eax

invoke dwtoa, dwVal2 , ADDR result

invoke StdOut, ADDR sShowResult
invoke StdOut, ADDR [result]
   
invoke StdIn, ADDR sVal2, LENGTHOF sVal2

ret

Main endp

end start


I just want to take two values from the user and add them together, but for some strange reason, it throws up a number in the 1000's (Last one was 8860) when added together, even when I just put 10 and 20 as the values.

Can anyone explain where I've gone wrong?

Thanks

Tight_Coder_Ex

atodw expects a null terminated string, stdin however terminates with 13, 10.  Stdin also returns the length of the string in EAX so all you have to do is place a null @ your pointer + EAX - 2 and then call atodw

lea eax, sVal1
push eax
invoke StdIn, eax, LENGTHOF sVal1
pop edx
mov byte ptr [edx+eax-2], 0

MickyD

Thanks Tight_Coder for the speedy reply!  :U

That explains a lot :)