Code Snippets

  

Assembly Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,957 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,000 people online right now. Registration is fast and FREE... Join Now!





Factorial by Recursive Procedure Calls

Conveys the general idea of how to make recursive calls in MIPS assembly. This particular procedure calculates n!

Submitted By: Dark_Nexus
Actions:
Rating:
Views: 290

Language: Assembly

Last Modified: August 29, 2008

Snippet


  1. # The same function in C would look something like this
  2. # int fact(int n)
  3. # {
  4. #      if (n < 1)
  5. #           return (1);
  6. #      else
  7. #           return (n * fact(n - 1));
  8. # }
  9.  
  10.  
  11.  
  12. .data
  13.  
  14. input_request: .asciiz "Input the first operand: "
  15. output1: .asciiz "The result is "
  16. newline: .asciiz "\n"
  17.  
  18. .align 2
  19.  
  20. .globl main
  21.  
  22. .text
  23.  
  24. main:
  25.         # request input from user
  26.         la $a0, input_request
  27.         li $v0, 4
  28.         syscall
  29.        
  30.         # retrieve input from user
  31.         li $v0, 5
  32.         syscall
  33.        
  34.         # store user input as first operand
  35.         move $a0, $v0
  36.        
  37.         # calculate factorial
  38.         jal fact
  39.        
  40.         # store result
  41.         move $t0, $v0
  42.        
  43.         # display result
  44.         la $a0, output1
  45.         li $v0, 4
  46.         syscall
  47.        
  48.         move $a0, $t0
  49.         li $v0, 1
  50.         syscall
  51.        
  52.         la $a0, newline
  53.         li $v0, 4
  54.         syscall
  55.        
  56.         # exit program
  57.         li $v0, 10
  58.         syscall       
  59.        
  60. fact:
  61.         # make room on stack for $ra and $a0
  62.         move $fp, $sp
  63.         addi $sp, $sp, -8
  64.        
  65.         # store both those registers on the stack
  66.         sw $a0, 0($fp)
  67.         sw $ra, -4($fp)
  68.        
  69.         # recursive condition
  70.         bgtz $a0, fact_L1
  71.         li $v0, 1
  72.         j fact_return
  73.        
  74.         # recursive calls
  75.         fact_L1:
  76.                 addi $a0, $a0, -1
  77.                 jal fact
  78.                 addi $fp, $sp, 8
  79.                 lw $a0, 0($fp)
  80.                 mult $a0, $v0
  81.                 mflo $v0
  82.                 j fact_return       
  83.                
  84.         # return sequence
  85.         fact_return:
  86.                 # restore $ra and $a0 registers
  87.                 lw $ra, -4($fp)
  88.                 lw $a0, 0($fp)
  89.  
  90.                 move $sp, $fp               
  91.                
  92.                 jr $ra       
  93.  
  94.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month