Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         .data
  2.         .text
  3.  
  4.         .globl _fibofac
  5. _fibofac:
  6.         pushl   %ebp
  7.         movl    %esp, %ebp
  8.        
  9.         movl    8(%ebp),%eax    # Moving the first parameter into eax
  10.         pushl   %eax            # First argument is pushed onto stack to be argument for fac
  11.         call    _fac
  12.         addl    $4, %esp        # Remove foo's argument from stack
  13.  
  14.        subl    $4, %esp
  15.        movl    %eax, -4(%ebp)  # Store result of fac somewhere else so %eax can be used by fib
  16.        
  17.        movl    12(%ebp),%eax
  18.        pushl   %eax
  19.        call    _fib
  20.        addl    $4, %esp        # Remove fib's argument from stack
  21.         addl     -4(%ebp), %eax
  22.         addl    $4, %esp
  23.  
  24.         popl    %ebp
  25.         ret
  26.  
  27. _fac:
  28.         pushl   %ebp
  29.         movl    %esp,%ebp
  30.  
  31.         cmpl    $4, 8(%ebp)     # Compare n to 4
  32.         jle     DONE            # Leave because we're only dealing with n>4
  33.  
  34.        movl    8(%ebp), %eax
  35.        subl    $2, %eax        # Subtract 2 because first argument for fibofac is (n-2)  
  36.        pushl   %eax            # Put n-2 in register and push on stack
  37.        addl    $1, %eax        # Add one to create another parameter for (n-1), or ((n-2)+1)
  38.        pushl   %eax            # Push (n-1) argument on stack
  39.        call    _fibofac
  40.        addl    $8, %esp        # Remove arguments from stack
  41.        
  42.        imul    8(%ebp), %eax   # Multiply n by result of fibofac, return that
  43.  
  44.        popl    %ebp
  45.        ret
  46.  
  47. _fib:
  48.        pushl   %ebp
  49.        movl    %esp,%ebp
  50.  
  51.        cmpl    $4, 8(%ebp)
  52.        jle     DONE
  53.  
  54.        movl    8(%ebp), %eax   # Put parameter n in register
  55.        subl    $4, %eax        # Subtract 4 from n to create (n-4) argument for 2nd fibofac call
  56.        pushl   %eax            # Push (n-4) onto stack
  57.        addl    $1, %eax        # ((n-4)+1) -> (n-3)
  58.        pushl   %eax
  59.        call    _fibofac
  60.        addl    $8, %esp
  61.  
  62.        subl    $4, %esp
  63.        movl    %eax, -4(%ebp)
  64. #
  65.        movl    8(%ebp), %eax
  66.        subl    $2, %eax
  67.        pushl   %eax
  68.        addl    $1, %eax
  69.        pushl   %eax
  70.        call    _fibofac
  71.        addl    $8, %esp
  72. #
  73.        addl    -4(%ebp),%eax
  74.        addl    $4, %esp
  75.  
  76.        popl    %ebp
  77.        ret
  78.  
  79.  
  80. DONE:
  81.        movl    $1, %eax
  82.        popl    %ebp
  83.        ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement