Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
48
Task/Factorial/MIPS-Assembly/factorial-1.mips
Normal file
48
Task/Factorial/MIPS-Assembly/factorial-1.mips
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
##################################
|
||||
# Factorial; iterative #
|
||||
# By Keith Stellyes :) #
|
||||
# Targets Mars implementation #
|
||||
# August 24, 2016 #
|
||||
##################################
|
||||
|
||||
# This example reads an integer from user, stores in register a1
|
||||
# Then, it uses a0 as a multiplier and target, it is set to 1
|
||||
|
||||
# Pseudocode:
|
||||
# a0 = 1
|
||||
# a1 = read_int_from_user()
|
||||
# while(a1 > 1)
|
||||
# {
|
||||
# a0 = a0*a1
|
||||
# DECREMENT a1
|
||||
# }
|
||||
# print(a0)
|
||||
|
||||
.text ### PROGRAM BEGIN ###
|
||||
### GET INTEGER FROM USER ###
|
||||
li $v0, 5 #set syscall arg to READ_INTEGER
|
||||
syscall #make the syscall
|
||||
move $a1, $v0 #int from READ_INTEGER is returned in $v0, but we need $v0
|
||||
#this will be used as a counter
|
||||
|
||||
### SET $a1 TO INITAL VALUE OF 1 AS MULTIPLIER ###
|
||||
li $a0,1
|
||||
|
||||
### Multiply our multiplier, $a1 by our counter, $a0 then store in $a1 ###
|
||||
loop: ble $a1,1,exit # If the counter is greater than 1, go back to start
|
||||
mul $a0,$a0,$a1 #a1 = a1*a0
|
||||
|
||||
subi $a1,$a1,1 # Decrement counter
|
||||
|
||||
j loop # Go back to start
|
||||
|
||||
exit:
|
||||
### PRINT RESULT ###
|
||||
li $v0,1 #set syscall arg to PRINT_INTEGER
|
||||
#NOTE: syscall 1 (PRINT_INTEGER) takes a0 as its argument. Conveniently, that
|
||||
# is our result.
|
||||
syscall #make the syscall
|
||||
|
||||
#exit
|
||||
li $v0, 10 #set syscall arg to EXIT
|
||||
syscall #make the syscall
|
||||
42
Task/Factorial/MIPS-Assembly/factorial-2.mips
Normal file
42
Task/Factorial/MIPS-Assembly/factorial-2.mips
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#reference code
|
||||
#int factorialRec(int n){
|
||||
# if(n<2){return 1;}
|
||||
# else{ return n*factorial(n-1);}
|
||||
#}
|
||||
.data
|
||||
n: .word 5
|
||||
result: .word
|
||||
.text
|
||||
main:
|
||||
la $t0, n
|
||||
lw $a0, 0($t0)
|
||||
jal factorialRec
|
||||
la $t0, result
|
||||
sw $v0, 0($t0)
|
||||
addi $v0, $0, 10
|
||||
syscall
|
||||
|
||||
factorialRec:
|
||||
addi $sp, $sp, -8 #calling convention
|
||||
sw $a0, 0($sp)
|
||||
sw $ra, 4($sp)
|
||||
|
||||
addi $t0, $0, 2 #if (n < 2) do return 1
|
||||
slt $t0, $a0, $t0 #else return n*factorialRec(n-1)
|
||||
beqz $t0, anotherCall
|
||||
|
||||
lw $ra, 4($sp) #recursive anchor
|
||||
lw $a0, 0($sp)
|
||||
addi $sp, $sp, 8
|
||||
addi $v0, $0, 1
|
||||
jr $ra
|
||||
|
||||
anotherCall:
|
||||
addi $a0, $a0, -1
|
||||
jal factorialRec
|
||||
|
||||
lw $ra, 4($sp)
|
||||
lw $a0, 0($sp)
|
||||
addi $sp, $sp, 8
|
||||
mul $v0, $a0, $v0
|
||||
jr $ra
|
||||
Loading…
Add table
Add a link
Reference in a new issue