1

I am trying to write a program that prints an integer, the integer is stored in rax register, I don't know why it only prints the first digit of the integer, in this case it's 3. The program divides rax by 10 and stores the remainder+48 in the reserved memory to display it later. we repeat this as long as rax is different than 0.

code:

section .bss
    integer resb 100

section .text

    global _start
_start:
    mov     rax, 321
    call    _printRAX

    mov     rax, 60
    mov     rdi, 0
    syscall

_printRAX:
    mov     rbx, 10
    mov     rcx, integer
    mov     [rcx], rbx

_loop1:
    inc     rcx
    mov     rdx, 0
    div     rbx
    add     rdx, 48
    mov     [rcx], dl

    cmp     rax, 0
    jne     _loop1

_loop2:
    mov     rax, 1
    mov     rdi, 1
    mov     rsi, rcx
    mov     rdx, 1
    syscall

    dec     rcx
    cmp     rcx, integer
    jge     _loop2

    ret

output:

3

Try It Online

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

0 Answers0