Home > Enterprise >  x86 Asm copy value from register
x86 Asm copy value from register

Time:12-09

I have such code:

mov eax, 0
mov [left], eax
mov eax, [len]
dec eax
mov [right], eax

I need to get 0 in left and len - 1 in right. But somehow left gets 0 and after line gets len - 1 too. I try to use another register for right, but result same.

mov eax, 0
mov [left], eax
mov ebx, [len]
dec ebx
mov [right], ebx

So, it gives same result.

Full code:

section .data
    len dd 10
section .bss
    left  resd 0
    right resd 0
section .text
    global main
push rbp
mov rbp, rsp
    mov eax, 0
    mov [left], eax
    mov eax, [len]
    dec eax
    mov [right], eax

and at this line I already have a problem

CodePudding user response:

From the nasm manual:

RESB, RESW, RESD, RESQ, REST, RESO, RESY and RESZ are designed to be used in the BSS section of a module: they declare uninitialized storage space. Each takes a single operand, which is the number of bytes, words, doublewords or whatever to reserve.

So your

    left  resd 0
    right resd 0

reserves 0 dwords for left and right, i.e. no memory at all. Thus they end up at the same address, and you observe that writing to one also seems to write the other.

You probably wanted left resd 1, etc.

You may have been thinking of 0 as the value to initialize with, like dd, but the whole point of resd is that you don't get to specify the initial value; in the .bss section it's automatically zero.

  • Related