Created
          December 20, 2013 14:50 
        
      - 
      
- 
        Save vittorioromeo/8055772 to your computer and use it in GitHub Desktop. 
    Fibonacci in veeAsm
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | //!ssvasm | |
| $require_registers(4); | |
| $define(R0, 0); | |
| $define(R1, 1); | |
| $define(R2, 2); | |
| $define(ROutput, 3); | |
| // _______________________________ | |
| // FN_MAIN function | |
| // * entrypoint | |
| // * returns in ROutput | |
| // _______________________________ | |
| $label(FN_MAIN); | |
| // Compute the 10th fibonacci number | |
| // Load constants | |
| loadIntCVToR(R0, 10); | |
| // Save registers | |
| pushRVToS(R0); | |
| pushRVToS(R1); | |
| // Push args | |
| pushRVToS(R0); | |
| // Call func | |
| callPI(FN_FIB); | |
| // Get return value | |
| moveRVToR(ROutput, R0); | |
| // Pop args | |
| popSV(); | |
| // Restore registers | |
| popSVToR(R1); | |
| popSVToR(R0); | |
| // Push output to stack | |
| pushRVToS(ROutput); | |
| halt(); | |
| // _______________________________ | |
| // FN_FIB function | |
| // * needs 1 int argument | |
| // * uses R0, R1 | |
| // * returns in R0 | |
| // _______________________________ | |
| $label(FN_FIB); | |
| // Get arg from stack | |
| moveSBOVToR(R0, 2); | |
| // Check if arg is < 2 (put compare result in R1) | |
| compareIntRVIntCVToR(R1, R0, 2); | |
| // Return arg if arg < 2 | |
| goToPIIfCompareRVSmaller(FN_FIB_RET_ARG, R1); | |
| // Else return fib(arg - 1) + fib(arg - 2) | |
| // Calculate fib(arg - 1) | |
| // Save registers | |
| pushRVToS(R0); | |
| // Push args | |
| pushIntCVToS(1); | |
| pushRVToS(R0); | |
| subtractInt2SVs(); | |
| // Call func | |
| callPI(FN_FIB); | |
| // Get return value | |
| // Return value is in R0, move it to R2 | |
| moveRVToR(R2, R0); | |
| // Pop args | |
| popSV(); | |
| // Restore registers | |
| popSVToR(R0); | |
| // Push fib(arg - 1) on stack | |
| pushRVToS(R2); | |
| // Calculate fib(arg - 2) | |
| // Save registers | |
| pushRVToS(R0); | |
| // Push args | |
| pushIntCVToS(2); | |
| pushRVToS(R0); | |
| subtractInt2SVs(); | |
| // Call func | |
| callPI(FN_FIB); | |
| // Get return value | |
| // Return value is in R0, move it to R2 | |
| moveRVToR(R2, R0); | |
| // Pop args | |
| popSV(); | |
| // Restore registers | |
| popSVToR(R0); | |
| // Push fib(arg - 2) on stack | |
| pushRVToS(R2); | |
| // Return fib(arg - 1) + fib(arg + 1) | |
| addInt2SVs(); | |
| popSVToR(R0); | |
| returnPI(); | |
| $label(FN_FIB_RET_ARG); | |
| returnPI(); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment