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