0

I'm studying x86 Assembly with NASM,so I started by the basics. I created the code below and wanted to understand its output:

section .data
    msg db 'Testing', 0xA
    size db 8
    array db 21, 22, 17, 3

section .text
    global _start

_start:
    ; sys_WRITE the string
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, [size]
    int 0x80

    ; Exit - Return code is the first element of array
    mov eax, 1
    mov ebx, [array]
    int 0x80

This is the output:

Testing
.shstrtab.text.data
 %

I tried to define the size of the string in a Byte in the .data section, so it gives this output. I already figured out that it happens because, when I load a byte to the edx register, which is of size dword, some garbage stays in the remaining bits. (The solution is just to declare size as dd, but that's not what this is about).

But I wanted to understand, why it doesn't happen with the array? When I check with echo $?, the value is 21. Shouldn't it be garbage?

I'm trying to understand this topic in a elemental level, I'm aware of the solution, but I don't know why the above happens.

Can someone explain? Thank you!

(The % is probably because I'm using a theme of Oh-My-Zsh)

Stopfield
  • 1
  • 3
  • 1
    `mov edx, [size]` loads 4 bytes, including 3 bytes of the string as the high 24 bits of the 32-bit integer size. If you wanted to load a byte, use `movzx edx, byte [size]`. It does happen with the array, too, as you'd see with a debugger or with `strace ./a.out`, but Unix exit statuses are truncated to 8 bits. – Peter Cordes Aug 13 '23 at 05:00
  • See also https://stackoverflow.com/tags/x86/info including the section at the bottom on using debuggers. – Peter Cordes Aug 13 '23 at 05:12

0 Answers0