fork(3) download
  1. ;functions.asm file
  2. ;------------------------------------------
  3. ; void iprint(Integer number)
  4. ; Integer printing function (itoa)
  5.  
  6.  
  7. iprint:
  8. push eax ; preserve ax on the stack to be restored after function runs
  9. push ecx ; preserve cx on the stack to be restored after function runs
  10. push edx ; preserve dx on the stack to be restored after function runs
  11. push esi ; preserve si on the stack to be restored after function runs
  12. mov ecx, 0 ; counter of how many bytes we need to print in the end
  13.  
  14. divideLoop:
  15. inc ecx ; count each byte to print - number of characters
  16. mov edx, 0 ; empty dx
  17. mov esi, 10 ; mov 10 into si
  18. idiv esi ; divide ax by si
  19. add edx, 48 ; convert dx to it's ascii representation - dx holds the remainder after a divide instruction
  20. push edx ; push dx (string representation of an intger) onto the stack
  21. cmp eax, 0 ; can the integer be divided anymore?
  22. jnz divideLoop ; jump if not zero to the label divideLoop
  23.  
  24. printLoop:
  25. dec ecx ; count down each byte that we put on the stack
  26. mov eax, esp ; mov the stack pointer into ax for printing
  27. call sprint ; call our string print function
  28. pop eax ; remove last character from the stack to move spforward
  29. cmp ecx, 0 ; have we printed all bytes we pushed onto the stack?
  30. jnz printLoop ; jump is not zero to the label printLoop
  31.  
  32. pop esi ; restore si from the value we pushed onto the stack at the start
  33. pop edx ; restore dx from the value we pushed onto the stack at the start
  34. pop ecx ; restore cx from the value we pushed onto the stack at the start
  35. pop eax ; restore ax from the value we pushed onto the stack at the start
  36. ret
  37.  
  38.  
  39. ;------------------------------------------
  40. ; void iprintLF(Integer number)
  41. ; Integer printing function with linefeed (itoa)
  42.  
  43. iprintLF:
  44. call iprint ; call our integer printing function
  45. push eax ; push ax onto the stack to preserve it while we use the ax register in this function
  46. mov eax, 0Ah ; move 0Ah into ax - 0Ah is the ascii character for a linefeed
  47. push eax ; push the linefeed onto the stack so we can get the address
  48. mov eax, esp ; move the address of the current stack pointer into ax for sprint
  49. call sprint ; call our sprint function
  50. pop eax ; remove our linefeed character from the stack
  51. pop eax ; restore the original value of ax before our function was called
  52. ret
  53.  
  54.  
  55. ;------------------------------------------
  56. ; int slen(String message)
  57. ; String length calculation function
  58.  
  59. slen:
  60. push ebx
  61. mov ebx, eax
  62. nextchar:
  63. ; cmp ax, 0
  64. jz finished
  65. inc eax
  66. jmp nextchar
  67. finished:
  68. sub eax, ebx
  69. pop ebx
  70. ret
  71.  
  72.  
  73. ;------------------------------------------
  74. ; void sprint(String message)
  75. ; String printing function
  76.  
  77. sprint:
  78. push edx
  79. push ecx
  80. push ebx
  81. push eax ; the number
  82.  
  83.  
  84. ;call slen
  85. mov edx,1 ; length of string
  86. mov ecx,esp ; arg2, pointer to string
  87. mov ebx,1 ; write to screen
  88. mov eax,4 ; sysout
  89. int 0x80 ; call kernel interupt
  90.  
  91. pop eax
  92. pop ebx
  93. pop ecx
  94. pop edx
  95. ret
  96.  
  97.  
  98. ;------------------------------------------
  99. ; void sprintLF(String message)
  100. ; String printing with line feed function
  101.  
  102. sprintLF:
  103. call sprint
  104. push eax
  105. mov eax, 0AH
  106. push eax
  107. call sprint
  108. pop eax
  109. pop eax
  110. ret
  111.  
  112.  
  113. ;------------------------------------------
  114. ; void exit()
  115. ; Exit program and restore resources
  116.  
  117. quit:
  118. mov ebx, 0
  119. mov eax, 1
  120. int 80h
  121. ret
  122. ; %include 'functions.asm'
  123.  
  124. section .bss
  125.  
  126. number: resb 1
  127. custom_regs: resd 8
  128.  
  129. section .text
  130. global _start
  131. _start:
  132.  
  133.  
  134. ; Init to 0
  135. xor ecx, ecx
  136.  
  137. tenloop:
  138.  
  139. inc ecx ; Incriment
  140. mov eax,ecx ; Move CX to AX for comparison
  141. test eax, 1
  142. jne fail
  143. mov eax, ecx
  144. add eax,48 ; number + 48 = ascii number+index
  145. call sprintLF ; call your print function
  146. fail:
  147. cmp ecx,8 ; did the counter reach 8?
  148. jne tenloop
  149.  
  150. end:
  151. call quit
Success #stdin #stdout 0s 148KB
stdin
Standard input is empty
stdout
2
4
6
8