Computer Engineering Project Collection

-->

Saturday, February 14, 2009

MIPS - Factorial

Here is another MIPS problem :

Write a program to compute x ! (factorial)


The simplest way to implement this using MIPS assembly language is by using a loop that decrement the x number by one ($t1), and multiply it with the x ($t0). Then the decremented number ($t1) will be decremented again and multiplied again with the previously multiplied number ($t3). This is repeated until the multiplier ($t1) reach number one. The result then be stored in the memory. There is no special intention of me by not using the $t2 register, I just forget that I haven't use register $t2.

Here is the code:
.data 0x10010000
fact: .space 4
.text
.globl main

main: addu $s0, $ra, $0
lui $s1,0x1001
ori $t0,$0,12
ori $t4,$0,1
addi $t1,$t0,-1
mul $t3,$t1,$t0

loop: beq $t1,$t4,slese
addi $t1,$t1,-1
mul $t3,$t3,$t1
j loop

slese: sw $t3,0($s1)
addu $ra,$0,$s0
jr $ra

Labels: , , , , ,

Saturday, January 10, 2009

MIPS Assembly Introduction

This is an introduction program to get used into MIPS Assembly language. The codes compiled and simulated using PC SPIM ver 7.00. With this little program, we can see what happens in the registers and memory, debugging the code line per line, etc. It also has a console so that the input/output may be displayed.

The first simple program is to convert Farenheit temperature degree into Celcius temperature degree. The code is :

.data
enter: .asciiz "Enter temperature in farenheit: "
cel1: .asciiz "It's the same as "
cel2: .asciiz " celcius\n"
.text
.globl main
main: addu $s0, $ra, $0
li $v0,4
la $a0,enter
syscall
li $v0,5
syscall
add $t0,$0,$v0
sub $t0,$t0,32
li $t1,5
li $t2,9
mult $t0,$t1
mflo $t3
divu $t3,$t2
mflo $t4
li $v0,4
la $a0,cel1
syscall
add $a0,$0,$t4
li $v0,1
syscall
li $v0,4
la $a0,cel2
syscall
addu $ra, $0, $s0
jr $ra


it uses syscall to display some text on the console, as well as taking inputs from there. The parameters for syscall is regulated by register $v0.

The simulation result is like this :
(click to enlarge)

Labels: , ,