1

I am learning x86_64 assembly and writing a simple code to print individual letters of a string. I know how to print the whole string but why is there no output when trying to display individual letters?

Code:

global _start

section .data
  msg db 'Hello!', 0x0a
  len equ $ - msg

section .text
  _start:
    mov eax, 4
    mov ebx, 0
  loop1:
    mov ecx, dword [msg+ebx] 
    mov edx, 1
    int 0x80
    cmp ebx, dword len
    jg exit
    add ebx, 1
    jmp loop1
  exit:
    mov eax, 1
    mov ebx, 0
    int 0x80
sh.3.ll
  • 815
  • 7
  • 17
  • 1
    You need to pass an address to write. See [man 2 write](http://man7.org/linux/man-pages/man2/write.2.html). You are loading a dword from memory instead. Change `mov ecx, dword [msg+ebx]` to `lea ecx, [msg+ebx]`. Also note that `ebx` is the file descriptor argument to `write` so it's a bad idea to use that for indexing. Furthermore, `eax` is used as return value so you need to reload it inside the loop. – Jester Oct 13 '19 at 18:40
  • First of all [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](//stackoverflow.com/q/46087730) - but if that was the problem, your code would have crashed from using a 32-bit addressing mode to load from `msg` instead of LEA to calculate a pointer. Or actually wouldn't have linked from using a 32-bit absolute address in a PIE. – Peter Cordes Oct 13 '19 at 18:41
  • Anyway, everything about this code is 32-bit: addressing and the 32-bit int 0x80 ABI. If you're trying to start with 64-bit code, this is not it. – Peter Cordes Oct 13 '19 at 18:49

0 Answers0