Search code examples
assemblyx86nasm

Trying to pass a value to reserved byte


I wanna pass "hello world" to the reserved "teste", but it just reads the first character of the string passed.

section .bss
    teste resb 1024

section .text
    global main

main:
    mov byte [teste], "hello world"

    mov eax, 4
    mov ebx, 1
    mov ecx, teste
    mov edx, 10
    int 0x80

exit_program:
    mov eax, 1      
    xor ebx, ebx
    int 0x80

I already tried changing from byte to qword, but it only add more 3 bytes and print "hell"


Solution

  • I already tried changing from byte to qword, but it only add more 3 bytes and print "hell"

    In 32-bit programming you can't use a 64-bit immediate. You can do this in just 3 dword moves:

    mov  ecx, teste
    mov  dword [ecx], "hell"
    mov  dword [ecx + 4], "o wo"
    mov  dword [ecx + 8], "rld "
    mov  eax, 4
    mov  ebx, 1
    mov  edx, 10
    int  0x80
    

    mov dword [ecx + 8], "rld" is valid too. NASM will then zero-pad to write a full dword.

    In order to display the whole message, use mov edx, 11 (10 is one short).

    It worked, but if I have a long string it won't be trivial doing that way... Isn't there a better way of doing that?

    If you had a long string it would already be in memory somewhere, wouldn't it? Then you just copy it to the .bss:

    mov  edi, teste
    mov  esi, message
    mov  ecx, length
    rep movsb
    
    ...
    
    message  db 'This is a long string to copy elsewhere'
    length   equ $ - message