Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```section .text
- global _start
- _start:
- push 1000 ; we need to sum 1 + 2 + ... + 1000
- call totalsum ; Run the total sum function
- add esp,4 ; Scrubs the parameter that was pushed onto the stack
- mov ebx,eax ; totalsum returns the answer in %eax, so we send to ebx
- mov eax,1 ; exit
- int 080h ; Function definition
- global totalsum:function
- totalsum:
- push ebp ; a function in nasm has to restore esb always.
- mov ebp,esp ; We use esp to not move the stack pointer
- mov eax, [ebp+8] ; First arg to %eax @4 we have the return adress and @8 we have the arg
- cmp eax,1 ; Base of our loop call, with the value 1. If one, then end.
- je end ;
- dec eax ; otherwise, decrease the val by 1
- push eax ; push to totalsum
- call totalsum ; call totalsum
- mov ebx, [ebp+8] ; %eax has the return value, so use that as the 'sum number'
- add eax,ebx ; add that by the result of the last call to totalsum (in %eax)
- end:
- mov esp,ebp ; We restore esp to where the function started
- pop ebp
- ret```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement