DarkoreXOR

Untitled

Jul 22nd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %include "io.inc"
  2.  
  3. global main
  4.  
  5. section .text
  6.  
  7. ;
  8. ; arg1 = n
  9. ;
  10. sum:
  11.     push ebp                ; save caller EBP
  12.     mov ebp, esp
  13.    
  14.     cmp dword [ebp + 8], 0  ; if (arg1 > 0)
  15.     jng .false              ; jump if not greater
  16.    
  17. .true____unused_label:      ; arg1 > 0
  18.     mov edx, [ebp + 8]      ; copy arg1 to newarg1
  19.     dec edx                 ; newarg1--
  20.     push edx                ; push newarg1
  21.     call sum                ; recursion call. Result will be in EAX
  22.     add esp, 4              ; drop pushed newarg1 after call
  23.     add eax, [ebp + 8]      ; result += arg1
  24.     jmp .end                ;
  25.    
  26. .false:                     ; arg1 <= 0
  27.     mov eax, 0              ; result = 0
  28.    
  29. .end:
  30.     ; mov esp, ebp          ; useless sh*t
  31.     pop ebp                 ; restore caller EBP
  32.     ret                     ; return back to caller, EAX already contains result
  33.  
  34. main:
  35.     push 5
  36.     call sum ; 5 + 4 + 3 + 2 + 1 + 0 = 15
  37.  
  38.     PRINT_DEC 4, eax
  39.  
  40.     ; system call exit()
  41.  
  42.     xor eax, eax
  43.     mov al, 1
  44.     xor ebx, ebx
  45.     int 0x80
Add Comment
Please, Sign In to add comment