More efficient way to output an integer in pure assembly

后端 未结 1 1148
感情败类
感情败类 2020-12-21 09:13

I\'m looking to output an integer using pure assembly. I\'m using nasm on a 64-bit linux machine. At the moment I\'m looking for a way to output integers to debug a compiler

相关标签:
1条回答
  • 2020-12-21 10:12

    Here are some possible changes to the routine:

    _printi:
        pushf
        push    rax
        push    rbx
        push    rcx
        push    rdx
    
        mov rax, [rsp+48]
        mov rcx, 4
        mov rbx, 10 ; --moved outside the loop
    .start:
        dec rcx
        xor rdx, rdx
        div rbx
        add rdx, 48
        mov [var+rcx], dl
        cmp rax, 0
        jne .start
    
        ; mov rax, [var] -- not used
        ; push    rax -- not used
        call    _printc
        ; pop rax -- not used
    
        pop rdx
        pop rcx
        pop rbx
        pop rax
        popf
        ret
    

    I also noted some limitations in the algorithm. If the number is larger than 9999, the code will continue to put digits outside of the allocated space, overwriting some other data. The routine is not fully reusable, i.e. if you print 123, then 9 it will come out as 129.

    0 讨论(0)
提交回复
热议问题