News:

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

Obvious mov question

Started by scolby33, August 02, 2009, 04:13:50 AM

Previous topic - Next topic

scolby33

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

hutch--

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

dedndave

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

scolby33

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?

Slugsnack

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

dedndave

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

rags

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

God made Man, but the monkey applied the glue -DEVO