sh-5.2$ cat main.s
.section .text
# long long qtoa(char *ptr, long long value);
qtoa:
movq %rsi, %rax
movq %rdi, %rsi
movq $10, %rcx
1: xorq %rdx, %rdx
divq %rcx
addq $'0', %rdx
movb %dl, (%rdi)
incq %rdi
cmpq $0, %rax
jne 1b
movq %rdi, %rax
subq %rsi, %rax
decq %rdi
1: movb (%rdi), %dh
movb (%rsi), %dl
movb %dh, (%rsi)
movb %dl, (%rdi)
incq %rsi
decq %rdi
cmpq %rsi, %rdi
ja 1b
retq
.global _start
_start:
subq $1024, %rsp
movq $dbcd, %rdi
movq $4, %rcx
2: pushq %rcx
pushq %rdi
xorq %rdx, %rdx
movq $4, %rcx
1: movb (%rdi), %al
shrb $4, %al
movb $10, %ah
mulb %ah
movb (%rdi), %ah
andb $0x0f, %ah
addb %ah, %al
movzbq %al, %rax
addq %rax, %rdx
incq %rdi
loopq 1b
#movq %rsp, %rdi
leaq 16(%rsp), %rdi
movq %rdx, %rsi
callq qtoa
leaq 16(%rsp), %rdx
movb $'\n', (%rdx, %rax)
incq %rax
movq %rax, %rcx
movq $1, %rax # sys_write
movq $1, %rdi # stdout
movq %rdx, %rsi # buf
movq %rcx, %rdx # count
syscall
popq %rdi
popq %rcx
addq $4, %rdi
loopq 2b
1: movq $60, %rax # sys_exit
xorq %rdi, %rdi # error_code
syscall
jmp 1b
.section .rodata
dbcd:
.byte (1 << 4) + 1, (2 << 4) + 3, (2 << 4) + 5, (3 << 4) + 1
.byte (1 << 4) + 0, (2 << 4) + 0, (3 << 4) + 0, (4 << 4) + 0
.byte (4 << 4) + 5, (4 << 4) + 5, (4 << 4) + 5, (4 << 4) + 5
.byte (8 << 4) + 8, (9 << 4) + 9, (7 << 4) + 7, (6 << 4) + 6
sh-5.2$ as -g -o main.o main.s
sh-5.2$ ld -o main main.o
sh-5.2$ ./main
90
100
180
330
sh-5.2$