Search code examples
assemblyx86-64nasmmicro-optimization

printing a signed integer optimization


There's this unanswered question in the Igor Zhirkov's book Low-Level Programming :

"Try to rewrite print_int without calling print_uint, copying its code, or using jmp. You will only need one instruction and a careful code placement.

Read about co-routines.".

The supplied code for "print_int" and "print_uint":

print_uint:
    mov rax, rdi
    mov rdi, rsp
    push 0
    sub rsp, 16
    
    dec rdi
    mov r8, 10

.loop:
    xor rdx, rdx
    div r8
    or  dl, 0x30
    dec rdi       
    mov [rdi], dl
    test rax, rax
    jnz .loop 
   
    call print_string
    
    add rsp, 24
    ret

print_int:
    test rdi, rdi
    jns print_uint
    push rdi
    mov rdi, '-'
    call print_char
    pop rdi
    neg rdi
    jmp print_uint

print_char:
    push rdi
    mov rdi, rsp
    call print_string 
    pop rdi
    ret
print_string:
    push rdi
    call string_length
    pop rsi
    mov rdx, rax
    mov rax, 1
    mov rdi, 1
    syscall
    ret

What could be that special single instruction he's talking about ?


Solution

  • I am so sorry, but it is unfortunately an error.

    There is a pair of functions print_newline and print_char where print_newline can be expressed as one instruction if the control falls to print_char afterwards. I wrote a blog post about it. The basic idea is that printing a specific character i.e. the newline feed is like starting the "print any character" subroutine when its argument is assigned the code of the said character.

    print_newline:
       mov rdi, '\n'  ; first integer argument is in rdi
    print_char:
       ...
    

    As to print_int I am sure that on AMD64 you can not express it through one instruction and fall to print_uint.