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: , , , , ,

Happy Valentine!

This post is out of topic, but can't stand to say :

HAPPY VALENTINE !!


May the love always be with you all..

Thanks for visiting my blog, please leave a comment if you like the blog

Friday, February 13, 2009

MIPS - Prime Number

This is the last of the three MIPS problems. If you recall, the first one was Determinant of 2x2 matrix, the second one was Bit Counter, and this last one is about Prime Number.
This is the problem :

write a program that store the first 20 prime numbers into memory!

Well, the idea of checking prime number is, as you should already know, by dividing a number with another number less than itself. The program will check if there are any remainder, and will perform another division with lesser number if there aren't. If there aren't any remainder until the divisor reach number one, then the number is a prime number. If there are remainder within the division progress, then the program will check another number that is more than the previously checked number. It obviously the easiest method, but could take some time for a very large numbers.

This is the code :
.data 0x1001000
array: .space 80
.text
.globl main

main: addu $s0,$ra,$0
lui $s1,0x1001
addi $s2,$s1,80
addi $t0,$0,2
addi $t5,$0,1
sw $t0,0($s1)
addi $s1,$s1,4

loop1: addi $t0,$t0,1
or $t1,$t0,$0

loop2: addi $t1,$t1,-1
slt $t4,$t5,$t1
bne $t4,$0,skip
sw $t0,0($s1)
addi $s1,$s1,4
beq $s1,$s2,exit
j loop1
skip: div $t0,$t1
mfhi $t3
bne $t3,$0,loop2
j loop1

exit: addu $ra, $0, $s0
jr $ra

the program will write the first 20 prime numbers checked, and it will be stored in memory. As usual the simulation is performed using MIPS32 Simulator program called PC-SPIM.

Labels: , , , , ,

Sunday, February 01, 2009

MIPS - Bit counter

This is the second of the 3 problems of MIPS Assembly. The simulation, as before, using the MIPS32 Simulator called PC-SPIM. This is a free software that you can download and use for yourself. The problem basically is to count the number of bit '1' in a 32 bit integer number. This is the problem in detail :

Create a program that reads a 32 bit integer from memory and count the number of ones.
Again store the number of ones in memory.

The idea is to store the desired 32-bit integer number into the memory, load it into one of the registers, and then do some sort of 'manipulations'. The easiest is to do shift left logical (sll) on the number until the Least Significant Bit (LSB) become the Most Significant Bit (MSB), and the shift back that bit into LSB again using shift right logical. We use the shift right logical so that the more significant bits (that is the 'front' bits) filled with '0'.
Then we compare the resulting number with 0, if it's not the same as zero, then the counter adds 1. This process is repeated until all the bits checked.

Here is the code :

.data 0x10010000
.word 41210
.space 4
.text
.globl main
main: addu $s0,$ra,$0
lui $s1,0x1001
lw $s2, 0($s1)
addi $t0,$0,32
add $t1,$0,$0
loop: addi $t0,$t0,-1
beq $t0,$0,exit
sll $s3,$s2,$t0
srl $s3,$s3,31
beq $s3,$0,loop
addi $t1,$t1,1
j loop
exit: sw $t1,4($s1)
addu $ra,$0,$s0
jr $ra

Labels: , , , , ,

Saturday, January 31, 2009

MIPS - Determinant of 2x2 matrix

This is the first of 3 Problems I got regarding the MIPS. The problems are given to make the students get used to MIPS assembly language. The simulation is performed using MIPS32 Simulator called PC SPIM, which can be acquired freely at http://pages.cs.wisc.edu/~larus/spim.html

The first problem is :

Create a program to compute determinant of a 2x2 matrix. Remember how a a 2-D array is
stored in memory. Result is stored in memory. Use integer only operations and assume all
result are 32-bit.

The idea is to put the matrix into the memory in the following order : [a00,a01,a10,a11], then do a calculation of determinant. If the result is negative than it must be inverted (because the absolute mark).
Here is the resulting code:

.data 0x10010000
.word 2,1,3,4
.space 4
.text
.globl main

main: addu $s0,$ra,$0
lui $s1,0x1001
lw $t0, 0($s1)
lw $t1, 4($s1)
lw $t2, 8($s1)
lw $t3, 12($s1)
mult $t0,$t3
mflo $s2
mult $t1,$t2
mflo $s3
sub $s4,$s2,$s3
slt $t4,$s4,$0
beq $t4,$0,oke
nor $s4,$s4,$0
addi $s4,$s4,1
oke: sw $s4,16($s1)
addu $ra,$0,$s0
jr $ra

Labels: , , ,