The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: scolby33 on August 02, 2009, 04:13:50 AM

Title: Obvious mov question
Post by: scolby33 on August 02, 2009, 04:13:50 AM
I cannot figure this out. How can I move the contents of a variable to a register or the contents of a register to a variable. I tried using addr (only valid in invoke) and offset (don't know what's wrong with that). The following code produces Error Invalid Instruction Operands for the mov's at assembly. What am I doing wrong?

include \masm32\include\masm32rt.inc

.data
    myVar   db "Test",0
    yourVar db "Test2",0

.code
start:

invoke AllocConsole   

invoke StdOut,addr myVar
mov eax,yourVar
mov myVar,eax
invoke StdOut,addr myVar

inkey

invoke FreeConsole
invoke ExitProcess,0

end start
Title: Re: Obvious mov question
Post by: hutch-- on August 02, 2009, 04:39:03 AM
Try "OFFSET MyVar".
Title: Re: Obvious mov question
Post by: dedndave on August 02, 2009, 04:44:05 AM
myVar is defined with "db" (define byte)
eax is a dword register

if you wanted to get a variable into eax

someDword dd ?

        mov     eax,someDword
        mov     someDword,eax

you are trying to display the strings, so you want the pointer
to get the address of someDword

        mov     eax,offset someDword
        mov     eax,addr someDword

for what you are trying to do, you could just use the masm32 "print" macro

myVar   db "Test",0

        print   addr myVar

if you like, you can add a carriage return/line feed

        print   addr myVar,13,10

this program demonstrates how to use offset, addr, as well as AttachConsole
it does not use print, however - that function has been replaced by a pair of routines
http://www.masm32.com/board/index.php?action=dlattach;topic=11927.0;id=6515
Title: Re: Obvious mov question
Post by: scolby33 on August 02, 2009, 03:37:58 PM
Since a byte is smaller than a dword, shouldn't it fit in the register? Can I zero-extend the front, or would it be simpler to just dd instead of db?
Title: Re: Obvious mov question
Post by: Slugsnack on August 02, 2009, 03:45:45 PM
you are defining more than 1 byte though aren't you ? in fact "test", 0 is 5 bytes. 4 for each character and 1 for the null terminator
Title: Re: Obvious mov question
Post by: dedndave on August 02, 2009, 04:01:29 PM
well, yes, you may zero-extend it
but to display it, you want the address, which is a 32-bit value

        movzx   eax,SomeByte

or

        xor     eax,eax
        mov     al,SomeByte

to sign-extend it use

        movsx   eax,SomeByte
Title: Re: Obvious mov question
Post by: rags on August 02, 2009, 04:14:34 PM
Scolby,
The point Dave is try to make is that the StdOut function expects a POINTER to a string as an argument, not the ACTUAL data itself.
A pointer just 'points' to a place in memory where something resides, so a pointer to the myVar variable is just myVar's address.
Besides this that Dave showed you:
Quote
       mov     eax,offset someDword
       mov     eax,addr someDword

you could use this to get the pointer into a register:

      lea eax, someDword

I hope this helps to clear things up for you.
             Rags