0

This is my code with commented explanations:

SECTION .data           ; Section containing initialised data

    EatMsg: db "Eat at Joe's!",10
    EatLen: equ $-EatMsg    

SECTION .bss            ; Section containing uninitialized data 

SECTION .text           ; Section containing code
global  _start          ; Linker needs this to find the entry point!    
_start:
nop                     ; This no-op keeps gdb happy...
    mov rax,1           ; Code for Sys_write call
    mov rdi, 1          ; Specify File Descriptor 1: Standard Output
    mov rsi, EatMsg     ; Pass offset of the message
    mov rdx, EatLen     ; Pass the length of the message
    mov R9,  [EatMsg]   ; move the adresse of Msg into R9
    syscall

mov rcx, 5 
DoMore: 
    mov rax, 1          ; Code for Sys_write call
    mov rdi, 1          ; Specify File Descriptor 1: Standard Output
    mov rsi, EatMsg     ; Pass offset of the message
    mov rdx, EatLen     ; Pass the length of the message
    dec rcx         
    jnz DoMore
    syscall             ; Make kernel call  


    mov rax, 1          ; Code for exit
    mov rdi, 0          ; Return a code of zero
    syscall             ; Make kernel call
zx485
  • 28,498
  • 28
  • 50
  • 59
Hasan Hawar
  • 23
  • 1
  • 9
  • 1
    So...what is your question? – zx485 Oct 20 '18 at 19:07
  • 2
    The [syscall instruction](http://www.felixcloutier.com/x86/SYSCALL.html) itself will overwrite RCX, and R11. Linux will also alter RAX with a return value. – Michael Petch Oct 20 '18 at 19:38
  • 1
    how to repeat the Msg 5 times? – Hasan Hawar Oct 20 '18 at 21:10
  • 2
    use a register other than RCX, RAX, or R11 so that it isn't destroyed by the syscall. Use RBX as a counter instead of RCX. – Michael Petch Oct 20 '18 at 21:22
  • 2
    You also ant to move the syscall that is right after `jnz DoMore` into the loop before the `dec` – Michael Petch Oct 21 '18 at 03:52
  • rax=1 / `syscall` is sys_write, not sys_exit. Your program presumably crashes. You want `mov eax, 231` for `sys_exit_group`, or `mov eax, 60` for `sys_exit`. Use `strace` to debug your system calls, and `gdb` to debug your code. – Peter Cordes Oct 22 '18 at 01:50
  • [Why do x86-64 Linux system calls modify RCX, and what does the value mean?](https://stackoverflow.com/q/47983371) would be a duplicate if `syscall` was inside the loop instead of after it. – Peter Cordes Oct 22 '18 at 01:52

0 Answers0