Search code examples
assemblyx86mov

mov [reg], reg Not Working


I can't seem to figure this out in x86. The program keeps crashing and my book says I can use [reg] to insert into memory so long as reg holds a location in memory:

.data
Counter BYTE 4;
Prompt BYTE "Enter an Integer: ", 0
UserInput DWORD 10 DUP(?)

.code
PromptUser PROC
     mov edx, OFFSET Prompt
     call WriteString
     ret
PromptUser ENDP

ReadUserInput PROC
     mov edx, OFFSET UserInput
     add edx, DWORD PTR Counter
     call ReadInt
     mov [edx], eax //ISSUE IS HERE

Even if I move the movs after the call to ReadInt, it still doesn't work.


Solution

  • This is not going to end well:

    Counter BYTE 4
    add edx, DWORD PTR Counter
    

    You've only got a byte, but you're treating it as if it was a dword. You'll end up adding not only the value of Counter, but the first 3 bytes of the Prompt string as well.

    The simplest solution is to make Counter a dword. If you absolutely have to keep it a byte you can do something like:

    movzx eax, BYTE PTR Counter
    add edx,eax