-5

How do I print the value of a register in 64bit on Linux using NASM?

I have looked on google but I could only find answer that did not work. Although I found one way to do it by messing around with godbolt.org. it is very large.

print:
    mov     r9, -3689348814741910323
    sub     rsp, 40
    mov     BYTE [rsp+31], 10
    lea     rcx, [rsp+30]
.L2:
    mov     rax, rdi
    lea     r8, [rsp+32]
    mul     r9
    mov     rax, rdi
    sub     r8, rcx
    shr     rdx, 3
    lea     rsi, [rdx+rdx*4]
    add     rsi, rsi
    sub     rax, rsi
    add     eax, 48
    mov     BYTE [rcx], al
    mov     rax, rdi
    mov     rdi, rdx
    mov     rdx, rcx
    sub     rcx, 1
    cmp     rax, 9
    ja      .L2
    lea     rax, [rsp+32]
    mov     edi, 1
    sub     rdx, rax
    xor     eax, eax
    lea     rsi, [rsp+32+rdx]
    mov     rdx, r8
    mov     rax, 1
    syscall
    add     rsp, 40
    ret

Note: Not allowed to use libc or any c/c++ Only NASM. Thanks in Advance.

  • 2
    Are you permitted to use the libc? In that case, the simplest way would be to call `printf`. If not, please let us know some details: do you want the value in decimal or hexadecimal? Unsigned or two's complement? – fuz May 29 '22 at 13:54
  • i can only use NASM no libc or any c/c++ , I want it in decimal, negative numbers supported – Darsh Cubing May 29 '22 at 16:24
  • Please tell us what kind of research you already did. Do you know how to print a single character or a string of characters from a buffer? Do you have any idea on how to convert a number into single digits? – Michael Karcher May 29 '22 at 19:42

0 Answers0