mips1

Tuesday, June 21, 2016

Mips Factorial calculation code

.data
     msg1: .asciiz "Enter the integer number you wish\n"
     msg2: .asciiz "The factorial is: "
     .text
     .globl main
     main:
# Printing Message1:
  la $a0, msg1                   #load address of msg1
  li $v0, 4                      # print message1
  syscall

# READING INTEGER FROM USER FOR MESSAGE -1
li $v0, 5 # read the integer from the user
  syscall
  move $s0, $v0,                    #   move from $v0 t the $t0
  addi $sp, $sp -12          #make room for $ra and $fp on the stack
  sw $ra, 8($sp)                  #push $ra
  sw $s0, 0($sp)                  #push $s0
# NUMBER TO FUNCTION CALL
jal factorial # do multiply, and result in $v0

lw $t0, 4($sp) #restore (pop) $to
lw $ra, 8($sp) #restore (pop) $ra
addi $sp, $sp, 12 # adjust $sp

# Printing Message2:
  la $a0, msg2                       #load address of msg2 into $a0
  li $v0, 4   # printing the string from the message 2
  syscall
  move $a0, $t0 #moving the value from $t0 to the $a0
  li $v0, 1 # this means that it is printing the integer number value
  syscall

li $v0, 10   # to print out the integer
syscall
factorial:
lw $s0, 0($sp)
beq $s0, 0, finishTheProgram
addi $s0, $s0, -1
 # calling factorial funtion
  addi $sp, $sp, -12     # making the space
  sw $s0, 0($sp) # pushing into $s0
  sw $ra, 8($sp) #pushing into $ra
 jal factorial

  lw $s1, 4($sp) #loading word into the $s1
  lw $ra, 8($sp) # loading word into $ra
  addiu $sp, $sp, 12
  lw $s0, 0($sp) #pushing inside the $s0
  mul $s2, $s1, $s0 #multiplying $s1 and $s0 and puting inside the $s2
  sw $s2, 4($sp) #pushing into the $s2
  jr $ra



finishTheProgram:
  li $s0, 1
  sw $s0, 4($sp)
  jr $ra








 
   

No comments:

Post a Comment