Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. ```section .text
  2. global _start
  3.  
  4. _start:
  5. push 1000 ; we need to sum 1 + 2 + ... + 1000
  6. call totalsum ; Run the total sum function
  7. add esp,4 ; Scrubs the parameter that was pushed onto the stack
  8. mov ebx,eax ; totalsum returns the answer in %eax, so we send to ebx
  9. mov eax,1 ; exit
  10. int 080h ; Function definition
  11.  
  12. global totalsum:function
  13. totalsum:
  14. push ebp ; a function in nasm has to restore esb always.
  15. mov ebp,esp ; We use esp to not move the stack pointer
  16. mov eax, [ebp+8] ; First arg to %eax @4 we have the return adress and @8 we have the arg
  17. cmp eax,1 ; Base of our loop call, with the value 1. If one, then end.
  18. je end ;
  19. dec eax ; otherwise, decrease the val by 1
  20. push eax ; push to totalsum
  21. call totalsum ; call totalsum
  22. mov ebx, [ebp+8] ; %eax has the return value, so use that as the 'sum number'
  23. add eax,ebx ; add that by the result of the last call to totalsum (in %eax)
  24.  
  25. end:
  26. mov esp,ebp ; We restore esp to where the function started
  27. pop ebp
  28. ret```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement