Advertisement
Guest User

Untitled

a guest
Dec 4th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern  printf
  2.  
  3. SECTION .data
  4.  
  5.     p1_fmt: db  0xA,`\nOutput: %d\n`, 0xA, 0
  6.  
  7. SECTION .text
  8.  
  9.     global main
  10.  
  11. main:
  12. ;-------------------------------------------------------------------------------------------------------------------
  13. fibonacci:
  14.     push    ebp     ;Build Stack Frame(?)
  15.     mov ebp, esp
  16.     sub esp, 16
  17.    
  18.     mov eax, [ebp+8];   ;what is this, and what does it do?
  19.     cmp eax, 2
  20.     jae recur
  21.  
  22.     xor edx, edx    ;Set contents of edx to zero, why?
  23.     jmp done
  24. ;-------------------------------------------------------------------------------------------------------------------
  25. recur:
  26.     sub eax, 2
  27.     push    eax     ; compute fib(n-2)
  28.     call    fibonacci
  29.     mov [ebp-8], eax    ; save returned value in 8-byte local variable...
  30.     mov [ebp-4], edx    ; ...in Little-Endian byte order.
  31.  
  32.     mov eax, [ebp+8]    ; get argument again
  33.     dec eax
  34.     push    eax     ; compute fib(n-1)
  35.     call    fibonacci
  36.     mov [ebp-16], eax   ; save returned value in 8-byte local variable...
  37.     mov [ebp-12], edx   ; ...in Little-Endian byte order.
  38.  
  39.     mov eax, [ebp-8]
  40.     mov edx, [ebp-4]    ; retrieve 1st computed value
  41.     add eax, [ebp-16]
  42.     adc edx, [ebp-12]   ; add 2nd computed value
  43. ;-------------------------------------------------------------------------------------------------------------------
  44. done:
  45.     pop edx
  46.     push    edx
  47.     push    p1_fmt      ;push address of format string
  48.     call    printf      ;Call C function
  49.     add esp, 12     ;adjust ESP to remove parameter
  50.  
  51.     mov esp, ebp
  52.     pop ebp
  53.     ret         ;return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement