Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is resb 2 equivalent to resw 1?

In this code:

    global _start
section .bss
    v1 resw 1
    v2 resb 2    ;array of 2 chars 
section .text
_start:
    mov word[v1],500
    mov word[v2],500

    mov rax,60
    mov rdi,0
    syscall

Is this code correct or not?
Does word[v1] work the same way as word[v2]?

I'm using NASM on x86-64 Ubuntu.

like image 668
srilakshmikanthanp Avatar asked Oct 21 '25 17:10

srilakshmikanthanp


1 Answers

Yes, they both do reserve the same amount of memory (2 bytes), so you can safely access them using mov word[...], ....

Note that this is roughly equal to writing uint8_t v1[2] vs. uint16_t v2[1] in C. But NASM doesn't enforce types so every memory reference is like using C memcpy() to do strict-aliasing-safe loads or stores that transfer N bytes without caring about the C type of the memory.

I strongly recommend to actually use resw, if you plan to use the allocated data as a word or an array of words, to avoid confusion and potential bugs.

like image 180
janw Avatar answered Oct 23 '25 08:10

janw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!