Computer Architecture Lab
32-175-493-001
Lab 3 MIPS Program compute the N factorial
Overview:
This report will deal with the code needed to write a program in the MIPS assembly language. The program that will be demonstrated will compute the factorial of an integer. Most of the MIPS commands that will be used to solve this program will be fairly simple. Three subroutines will be needed to calculate the result, one, input from the user, two, integer math to solve for the "N" factorial, and finally, the program will display the result to the user on the screen. There will also be a comparison between the MIPS and the Motorola 68000 assemblers.
This program can be broken into three basic parts as stated above, input, calculation of the integer, and output of the answer. First the input will display a message asking the user to input an integer from the keyboard. This number will be used to calculate the final answer. The next routine must calculate the factorial of "N". An example that will be used will be to solve the "N" factorial for the number "5". The way to do this is to take 5*4*3*2*1 = 120. The final subroutine will display the answer to the user.
The N factorial program will be explained in detail so the actual body of the code will be necessary for this discussion. The preceding link will provide the MIPS code for this program. The program will be discussed sequentially for readability. The # sign indicates a "comment" will follow. The heading of this program uses the following comments to indicate what the program is and who wrote the assembly code. The .text and .globl means that the program execution will begin.
Main function: The main function controls the flow of the program. The main function provides subroutines and sometimes variables. Subroutines are used to make the programs easy to write, read, understand, debug, modify, and maintain.
Input subroutine: The input subroutine is the first thing that is executed in the program. The input subroutine is called by using the operator jal input. JAL is a procedure call that simply means jump and link to the address input. The input subroutine prompts the user to enter an integer. There are two system calls executed within the input subroutine. The $v0 register is used to execute input and output routines. To output an ASCII message you must use the system call for printing a string li $v0, 4. The four is used to display an ASCII message. The $a0 register must be loaded with the address of the ASCII message in order for the message to be displayed. The message ("Enter N: ") is displayed when this command is executed. To get input from the user you must use the system call for inputting a string li $v0, 5. The five is used to read an integer from the user. A syscall is placed after these two commands to actually perform these operations. The move command is relatively simple, it places the integer entered in by the user ($v0) into a temporary address $t0. The jr $ra returns back to the calling function main.
Nfact subroutine: The Nfact subroutine is the guts of the algorithm. The first thing the Nfact subroutine does is initializes a counter and a product. This is done by loading a temporary address $t1, $t2, with ones. The counter will indicate how many times the loop has been executed and the product will store the answer to the problem. Inside the loop mul command is executed first which is a simple multiplication operation (mul $t2,$t2,$t1). This will multiple the value of $t2 times the value of $t1 and store the result in $t2. The next step is (beq $t0,$t1,exit) which will compare the values $t0, and $t1 if they are equal it will exit the loop and if they are not equal it will execute the next statement. The next statement is the loop counter (addi $t1,$t1,1) which will increment $t1 by one every time it is executed. The final statement in the loop is the j loop statement, which will jump to the target address loop and execute the statements inside the loop again. The exit command will simply bring the program back to the calling function main.
Output subroutine: The output subroutine is much like the input subroutine. The only difference is that it displays the output of the answer to the N factorial. The only difference is the way you output the answer. This is done by loading the answer ($t2) into ($a0) as you can see in the program this is the only difference between the input subroutine the output subroutine.
Conclusion:
The conclusion of the paper will compare to different assembly languages. The two that will be compared are MIPS and the Motorola 68000 assembly languages. The MIPS architecture was designed using a few logical principles, "simplicity favors regularity", "smaller is faster", "good design means good compromises". The MIPS architecture is designed around the Reduced Instruction Set Architecture (RISC). This technology is based on that most microprocessors use only a small portion of their entire instruction set. There are many differences between these two assembly languages. The most noticeable difference between the two would be the number of instructions that both assemblers have to offer. The 68000 has over 70 different instructions which is used to make the programmers tasks of writing source code less tedious. While the MIPS instruction set is small and little more limited than other assembly languages. This does mean that MIPS assembly language is inferior to Motorola 68000 assembly language. The MIPS assembly language is designed around the motto "smaller is faster".
The instruction set of the Motorola 68000 and the MIPS architecture are hardly similar. They both have arithmetic operations. Loops are identical in both architectures. Subroutines in both architectures are represented in the same way. Where a jump to a label which would be at the beginning of each function. Some of the conditional branches take on the same idea.
There are so few similarities in these two architectures so it is hard to decide which one is "better" than the other architecture. It all comes down to a matter of taste really. If you are interested in a smaller faster language go with the MIPS architecture. If you are looking for a less tedious architecture use the Motorola 68000 architecture.