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, but I want to use the same code for writing an OS, which is also the reason I don't simply use printf()
. After much searching and frustration I have come up with this code
SECTION .data
var: db " ",10,0
SECTION .text
global main
global _printc
global _printi
main:
mov rax, 90
push rax
call _printi
xor rbx, rbx
mov rax, 1
int 0x80
_printi:
pushf
push rax
push rbx
push rcx
push rdx
mov rax, [rsp+48]
mov rcx, 4
.start:
dec rcx
xor rdx, rdx
mov rbx, 10
div rbx
add rdx, 48
mov [var+rcx], dl
cmp rax, 0
jne .start
mov rax, [var]
push rax
call _printc
pop rax
pop rdx
pop rcx
pop rbx
pop rax
popf
ret
_printc:
push rax
push rbx
push rcx
push rdx
mov rax, [rsp+40]
mov [var], rax
mov rax, 4
mov rbx, 1
mov rcx, var
mov rdx, 4
int 0x80
pop rdx
pop rcx
pop rbx
pop rax
ret
Note that I'll be replacing 0x80 calls with BIOS calls when porting to OS development.
My question is how to optimize, or even prettify, this code further. My first thought would be to replace pushing all the registers individually, but there isn't any 64-bit pusha
instruction...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…