print_number.asm 808 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. %ifndef PRINT_NUMBER_ASM
  2. %define PRINT_NUMBER_ASM
  3. %include "syscalls.asm"
  4. section .bss align=8
  5. print_num_buf resb 64
  6. print_num_buf_end:
  7. section .text
  8. print_number:
  9. mov r8, 0b001
  10. call type_check
  11. ;; extract the number
  12. shr rax, 3
  13. ;; fill the buffer up with the decimal version of the number
  14. ;;; http://tptp.cc/mirrors/siyobik.info/instruction/CWD%252FCDQ%252FCQO.html
  15. mov rsi, print_num_buf_end
  16. mov rdi, 0
  17. mov rbx, 10
  18. ;; cqo
  19. .loop:
  20. mov rdx, 0
  21. idiv rbx ; rax=quotient, rdx=remainder
  22. dec rsi
  23. inc rdi
  24. add rdx, '0'
  25. mov byte [rsi],dl
  26. test rax, rax
  27. jnz .loop
  28. .done:
  29. mov rdx, rdi
  30. ;; print it
  31. mov rax, sys_write
  32. mov rdi, 1
  33. syscall
  34. ret
  35. %endif