You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
1.4 KiB

5 years ago
  1. ; Collection of Assembly macros to make programming in assembly easier
  2. ; Include it with "%include 'macros.asm'
  3. section .data
  4. newline db 0xA,0x0
  5. ; Macro to calculate string length and print to stdout
  6. %macro printStr 1
  7. ;; Store previous data
  8. push rax
  9. push rbx
  10. push rcx
  11. push rdx
  12. push rdi
  13. push rsi
  14. ;; Move first arg to rax
  15. mov rax, %1
  16. ;; push rax to stack
  17. push rax
  18. ;; move 0 to rbx for loop counter
  19. mov rbx,0
  20. ;; counts letters
  21. %%printLoop:
  22. inc rax
  23. inc rbx
  24. mov cl,[rax]
  25. cmp cl,0
  26. jne %%printLoop
  27. ;; sys_write
  28. mov rax,1
  29. mov rdi,1
  30. pop rsi
  31. mov rdx,rbx
  32. syscall
  33. ;; pop values back to registers
  34. pop rsi
  35. pop rdi
  36. pop rdx
  37. pop rcx
  38. pop rbx
  39. pop rax
  40. %endmacro
  41. %macro printStrLF 1
  42. push rax
  43. mov rax,%1
  44. printStr rax
  45. printStr newline
  46. pop rax
  47. %endmacro
  48. %macro printInt 1
  49. push rax
  50. push rcx
  51. push rdx
  52. push rsi
  53. mov rax,%1
  54. mov rcx, 0
  55. %%divideLoop:
  56. inc rcx
  57. mov rdx, 0
  58. mov rsi, 10
  59. idiv rsi
  60. add rdx, 48
  61. push rdx
  62. cmp rax, 0
  63. jnz %%divideLoop
  64. %%printLoop:
  65. dec rcx
  66. mov rax, rsp
  67. printStr rax
  68. pop rax
  69. cmp rcx, 0
  70. jnz %%printLoop
  71. pop rsi
  72. pop rdx
  73. pop rcx
  74. pop rax
  75. %endmacro
  76. %macro printIntLF 1
  77. push rax
  78. mov rax, %1
  79. printInt rax
  80. printStr newline
  81. pop rax
  82. %endmacro
  83. %macro exit 1
  84. mov rax,60
  85. mov rdi,%1
  86. syscall
  87. %endmacro