Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. SECTION .text
  2. global _start
  3.  
  4. _start:
  5. mov ecx, 0
  6. loop:
  7. inc ecx
  8. mov eax, ecx
  9. call iprint
  10. push eax ; push eax onto the stack to preserve it while we use the eax register in this function
  11. mov eax, 0Ah ; move 0Ah into eax - linebreak
  12. push eax ; push the linefeed onto the stack so we can get the address
  13. mov eax, esp ; move the address of the current stack pointer into eax for sprint
  14. call stringPrint
  15.  
  16. pop eax ; remove our linefeed character from the stack
  17. pop eax ; restore the original value of eax before our function was called
  18.  
  19. cmp ecx, 12 ; 12 is the hard-coded end of the loop
  20. jne loop
  21.  
  22. ;quit
  23. mov ebx, 0
  24. mov eax, 1
  25. int 80h
  26. ret
  27.  
  28.  
  29. ; -------SUBROUTINES-----------
  30.  
  31. iprint:
  32. push eax ; counter of how many bytes we need to print in the endpush eax ; preserve eax on the stack to be restored after function runs
  33. push ecx ; preserve ecx on the stack to be restored after function runs
  34. push edx ; preserve edx on the stack to be restored after function runs
  35. push esi ; preserve esi on the stack to be restored after function runs
  36. mov ecx, 0
  37.  
  38. divideLoop:
  39. inc ecx ; count each byte to print - number of characters
  40. mov edx, 0 ; empty edx
  41. mov esi, 10 ; mov 10 into esi
  42. idiv esi ; divide eax by esi
  43. add edx, 48 ; convert edx to it's ascii representation - edx holds the remainder after a divide instruction
  44. push edx ; push edx (string representation of an intger) onto the stack
  45. cmp eax, 0 ; can the integer be divided anymore?
  46. jnz divideLoop ; jump if not zero to the label divideLoop
  47.  
  48. printLoop:
  49. dec ecx ; count down each byte that we put on the stack
  50. mov eax, esp ; mov the stack pointer into eax for printing
  51. call stringPrint ; call our string print function
  52. pop eax ; remove last character from the stack to move esp forward
  53. cmp ecx, 0 ; have we printed all bytes we pushed onto the stack?
  54. jnz printLoop ; jump is not zero to the label printLoop
  55.  
  56. pop esi ; restore esi from the value we pushed onto the stack at the start
  57. pop edx ; restore edx from the value we pushed onto the stack at the start
  58. pop ecx ; restore ecx from the value we pushed onto the stack at the start
  59. pop eax ; restore eax from the value we pushed onto the stack at the start
  60. ret
  61.  
  62. slen:
  63. push ebx
  64. mov ebx, eax
  65.  
  66. nextchar:
  67. cmp byte [eax], 0
  68. jz finished
  69. inc eax
  70. jmp nextchar
  71.  
  72. finished:
  73. sub eax, ebx
  74. pop ebx
  75. ret
  76.  
  77. stringPrint:
  78. push edx
  79. push ecx
  80. push ebx
  81. push eax
  82. call slen
  83.  
  84. mov edx, eax
  85. pop eax
  86.  
  87. mov ecx, eax
  88. mov ebx, 1
  89. mov eax, 4
  90. int 80h
  91.  
  92. pop ebx
  93. pop ecx
  94. pop edx
  95. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement