user4180960
user4180960

Reputation:

Assembly: how to store data into variable

I wrote a simple program in assembly code (open console and loop input until user enters 5). I want to store each input in variable input (new input will overwrite old). This is my code:

format PE console
entry start

include 'win32a.inc'

;======================================
section '.data' data readable writeable
;======================================

input      db "", 0

;=======================================
section '.code' code readable executable
;=======================================

start:
        ccall   [getchar] ; Wait for input
        cmp     eax, "5"  ; Compare input with string
        je      exit      ; If it is equal, then exit
        jne     start     ; If not, wait for input again
exit:
        stdcall [ExitProcess], 0

;====================================
section '.idata' import data readable
;====================================

library kernel,'kernel32.dll',\
        msvcrt,'msvcrt.dll'

import  kernel,\
        ExitProcess,'ExitProcess'

import  msvcrt,\
        printf,'printf',\
        getchar,'_fgetchar'

I tried to to write

    ccall   [getchar] ; Wait for inout
    cmp     eax, "5"  ; Compare input with string

    mov     [input], eax ; This line is added

    je      exit      ; If it is equal, then exit
    jne     start     ; If not, wait for input again

but I got error Operand sizes do not match.. I have searched for this error, but I didn't find anything useful.

Upvotes: 0

Views: 7927

Answers (1)

Guffa
Guffa

Reputation: 700422

The eax register is a 32-bit (4 byte) register, but the data type of input is byte.

You need doubleword data to store a 32-bit value:

input      dd 0

Note: The data in eax is actually not a string. When you compare it to "5", the value "5" is converted to a 32-bit value, i.e. 0x00000005. Declaring input as a zero-length string not only made it the wrong type, it was also too small to hold a 4 byte value as it was only 1 byte large (the string terminator).

Upvotes: 1

Related Questions