user3542317
user3542317

Reputation: 313

Variable changes its value mysteriously (to me at least)

Ok, I feel kind of awkward. I have this little assembler "program":

section .data
var dw 0x0

section .text
  global _start
  _start:
    nop
    cmp dword [var], 0x0
    mov eax, 1
    mov ebx, 0
    int 80h

Now, I initialize the variable "var" to 0x0. However, If I run this "program" through a debugger it says that the value of "[var]" at the line "cmp dword [var], 0x0" is actually 1835008. Now, why is that? I initialized it to 0 and I didn't do anything further to this variable, so it shouldn't change. Yet it has changed. Why? What is going on?

Upvotes: 0

Views: 147

Answers (1)

Daniel Kamil Kozar
Daniel Kamil Kozar

Reputation: 19266

dw, in NASM syntax, means "reserve word". A "word" in x86 architecture is 2 bytes, or 16 bits, mainly due to historical reasons. 32-bit data is referred to as "doublewords". In NASM syntax, dd can be used to "reserve doubleword".

After changing var dw 0x0 to var dd 0x0, the program works as expected. The value 1835008 (0x001c0000) apparently includes some other data that just happened to be located after var's starting address.

Upvotes: 3

Related Questions