tblznbits
tblznbits

Reputation: 6778

How do I use mov si, word[ecx] in my x86 assembly code with MASM?

I have a set of instructions in x86 assembly code that looks like the following:

[0x401240]
mov edx, str.HelloWorld
mov eax, ecx
push esi
   |
   |
   v
[0x401248]
mov si, word [eax]
cmp si, word [edx]
jne 0x40126e

Where ecx is the string that I pass into the program once it's running. I'm completely new to assembly, so I'm not entirely sure what's going on here, but I think the line mov si, word [eax] is saying "take two bytes from eax and set si equal to that. In order to test this, I wanted to write my own little script to print out to the console the results of this operation. So using tutorials from the internet, I put this together:

.386
.model flat, stdcall
option casemap :none

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

.data
    message db "Hello world!", 0

.code

main:
    mov si, word [message]
    invoke StdOut, si
    invoke ExitProcess, 0
end main

Unfortunately, running this via \masm32\bin\ml /c /Zd /coff test.asm results in test.asm(16): error A2009: syntax error in expression. How can I remedy this and test my hypothesis?

Upvotes: 1

Views: 1576

Answers (1)

rkhb
rkhb

Reputation: 14409

mov si, word [message] is NASM syntax. The equivalent in MASM is

mov si, word ptr [message]

Almost all procedures for 32-bit Windows need DWORDs as argument(s). SI is a WORD. Change

invoke StdOut, si

to

invoke StdOut, esi

The MASM32 procedure StdOut needs a pointer to a null terminated string. With MOV you get the value, not the pointer Use LEA to get the pointer. Change

mov si, word ptr [message]

to

lea esi, [message]

Alternatively you can load the pointer immediately:

mov esi, OFFSET message

The answer to your underlying problem is: mov si, word [eax] loads the WORD that is pointed by EAX into the register SI which is the lower part of ESI. The script to test should look like:

INCLUDE \masm32\include\masm32rt.inc

.data
    message db "Hello world!", 0

.code

main:
    xor esi, esi
    mov eax, OFFSET message
    mov si, WORD PTR [eax]

    printf ("0x%x",esi)

    invoke ExitProcess, 0
end main

Upvotes: 2

Related Questions