Home > front end >  How do I print out two-digit integers in ASM, ( Linux NASM x86_64 )
How do I print out two-digit integers in ASM, ( Linux NASM x86_64 )

Time:12-31

This is how to print out a number, but how do I print out 2 digit numbers?

section .data
    num: db 9,10

section .text
global _start
_start:
    mov rax,[num]
    add rax,48
    mov [num],al
    mov rax,1
    mov rdi,1
    mov rsi,num
    mov rdx,2
    syscall

    mov rax,60
    mov rdi,0
    syscall

This simply prints out 9, but if I make num 12 it gives me a '<'. I believe it is printing out the ascii character for 60, which is '<'.

CodePudding user response:

mov rax,[num]

Because num just holds a byte, better not read this as a qword. Use the movzx eax, byte [num] instruction. You don't need the movzx rax, byte [num] instruction because all writing to a dword register already zeroes the high dword anyway.

but how do I print out 2 digit numbers?

Next code can do just that, printing numbers from the range [10,99].
Note that there's a placeholder right in front of the newline.

section .data
    num: db 12, 0, 10

section .text
global _start
_start:
    movzx eax, byte [num]  ; Value LT 100
    xor   edx, edx
    mov   ebx, 10
    div   ebx              ; Quotient in EAX, Remainder in EDX
    mov   ah, dl
    add   ax, '00'         ; Convert to ASCII
    mov   [num], ax        ; Stores 'tens' followed by 'ones'
    mov   rax, 1
    mov   rdi, 1
    mov   rsi, num
    mov   rdx, 3           ; 3 instead of 2
    syscall

For a general approach you could first study Displaying numbers with DOS. It explains the methodology, but of course you'll need to adapt the code to 64-bit.
Even better info is at https://stackoverflow.com/a/46301894.

CodePudding user response:

To print multiple digit integers in ASM, you will need to use the appropriate system call to write the digits to the screen one at a time.

Here is an example of how you can do this:

First, you will need to convert the integer to a string representation. You can do this using the itoa function, which is a function that converts an integer to a string.

Next, you can use the write system call to write the string to the screen one character at a time. The write system call takes three arguments: the file descriptor to write to (usually 1 for stdout), a pointer to the buffer containing the data to be written, and the number of bytes to write.

Here is some example code that demonstrates how to print out a multiple digit integer:

section .data
    buffer: times 11 db 0 ; buffer to hold the string representation of the integer

section .text
    global _start

_start:
    mov rax, 42 ; put the integer to be printed in RAX
    mov rdi, buffer ; put the address of the buffer in RDI
    mov rsi, 10 ; put the base (10 for decimal) in RSI
    call itoa ; convert the integer to a string

    mov rdx, 11 ; number of bytes to write
    mov rsi, buffer ; pointer to the data to be written
    mov rdi, 1 ; file descriptor (stdout)
    mov rax, 1 ; write system call number
    syscall ; write the string to stdout

    mov rax, 60 ; exit system call number
    xor rdi, rdi ; exit code (0 for success)
    syscall ; exit

Note that this code uses the itoa function, which is not a standard C library function. You will need to implement this function yourself or find an implementation that you can use.

  • Related