0

;I am trying to print the elements of an array on console in nasm ,but it is always printing only last two elements ,why is that: this is my code:

section .data
    msg db 1,2,3
    msg_len equ ($ - msg)
    separator db ' '    ; Separator character
    separator_len equ $ - separator
    temp db 0
section .text
    global _start
_start:
    mov rbx,msg_len
    mov rsi,0
again:
    xor rax,rax
    mov al, [msg + rsi] 
    add al,'0'
    mov [temp],al
    xor al,al

    ; Print the character
    mov rdi, 1             ; File descriptor (stdout)
    mov rdx, 1             ; Length of the string to print
    mov rcx,temp
    mov rax, 4             ; System call number for write
    int 0x80               ; Call the kernel
    inc rsi

    ; Print the separator
    mov rdi, 1             ; File descriptor (stdout)
    mov rdx, separator_len ; Length of the separator
    mov rcx, separator     ; Separator to print
    mov rax, 4             ; System call number for write
    int 0x80               ; Call the kernel

    dec rbx
    cmp rbx,0
    jne again

    mov rax, 1        ; System call number for exit
    xor rbx, rbx      ; Exit code 0
    int 0x80          ; Call the kernel

i want to print the elements of array but it is always printing only last two elements

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    Running it under `strace` shows the first two calls to `write` were `write(3, "1", 1) = -1 EBADF (Bad file descriptor)` and `write(3, " ", 1) = -1 EBADF (Bad file descriptor)`. But writing to file-descriptors 2 and 1 both work. – Peter Cordes May 24 '23 at 05:44
  • 1
    The 32-bit `int 0x80` ABI looks for its args in EBX, ECX, and EDX, in that order. It's the 64-bit `syscall` ABI (which you should be using, with 64-bit syscall numbers) that takes the first arg in RDI, but doesn't look at ECX. – Peter Cordes May 24 '23 at 05:45

0 Answers0