EEL3801 Lab Manual PDF
EEL3801 Lab Manual PDF
EEL 3801
Computer Organization and Design
Laboratory Manual
1
Laboratory Schedule
Tasks Technical Topics Submission
Instruction execution flow, Assembly
Week 1 programming, Addressing MARS
1. Introduction to MARS 1. Y
installation procedure and MIPS
Week2 Assembly Instructions
Week 3 1. Introduction to Project 1: Practice Problems and How to use registers and characters
1. Y
Week 4 Code for Project 1 in MIPS Assembly
1. Project 1 (Part A Code): Write an assembly
Week 5 program, which uses loops to perform 1. Y
Mathematical operations and loops
multiplication.
in Assembly
1. Project 1 (Part B Code): Write an assembly
Week 6 1. Y
program, which uses loops to perform division
1. Project 1 (Report)
Week 7 How to work with strings and 1. Y
2. Introduction to Project 2: Practice Problems and
characters in MIPS Assembly 2. Y
Week 8 Code for Project 2
1. Project 2 (Part A Code): Write a code which
Week 9 finds how many times a character is used in a 1. Y
How to work with strings,
given statement
characters, and loops in MIPS
1. Project 2 (Part B Code): Write a code which finds
Assembly
Week 10 how many times a word is used in a given 1. Y
statement
1. Project 2 (Report)
Week 11 How to use functions and 1. Y
2. Introduction to Project 3: Practice Problems and
procedures in MIPS assembly 2. Y
Week 12 Code for Project 3
Floating-point calculations in MIPS
1.Project 3 (Part A Code): Calculate a
using functions.
Week 13 mathematical expression using loops and 1. Y
Increasing efficiency considering to
functions
Dynamic/Static Instruction Count.
Matrix Multiplication in MIPS
1. Project 3 (Part B Code): Multiply two 3x3
Week 14 assembly using functions. 1. Y
Matrices using loops and functions
Cache utilization observation.
*The topics and schedule may be adjusted as necessary to maximize learning outcomes.
2
WEEK 1 and WEEK 2: Introduction to MARS Software
MARS, the MIPS Assembly and Runtime Simulator, will assemble and simulate the execution of MIPS
assembly language programs. It can be used either from a command line or through its integrated
development environment (IDE). MARS is written in Java and requires at least Release 1.5 of the J2SE
Java Runtime Environment (JRE) to work. It is distributed as an executable JAR file.
Download MARS from following link and double click the icon for Mars.jar and MARS environment will
open:
http://courses.missouristate.edu/kenvollmar/mars/download.htm
Mac User:
Download and install JRE through below link:
http://www.java.com/en/download/index.jsp
Download MARS from following link and double click the icon for Mars.jar and MARS environment will
open:
http://courses.missouristate.edu/kenvollmar/mars/download.htm
.data
out_string: .asciiz "\nHello, World!\n"
.text
li $v0, 4
la $a0, out_string
syscall
li $v0, 10
syscall
3
Opening an existing program
To open an existing program, select "File = > Open" from the Mars menu. Enter the name of the program
file and click the Open button.
The Text Segment window displays the instructions. Each line contains 5 columns:
• Column 1 displays a checkbox for setting breakpoints.
• Column 2 displays the address of an instruction in hexadecimal.
• Column 3 displays the machine encoding of the instruction in hex.
• Column 4 is a mnemonic description of the machine instruction.
• Column 5 contains the assembly source that corresponds to the instruction.
4
Running the program
Once you have removed any syntax errors you can run your program. The Run menu and the toolbar contain
the follow execution options:
• The Run Speed Slider allows you to run the program at full speed or
slow it down so you can watch the execution.
• Reset resets the program to its initial state, so that you can execute again from the beginning using
the initial variable values.
• Pause suspends execution at the currently executing instruction when you are running your
program.
• Backstep "unexecutes" the last instruction when you are paused or stepping.
You can also set a breakpoint at any instruction by clicking the checkbox in front of the instruction in the
text segment pane. During execution you can see which instruction is being executed (highlighted in
yellow), which register was last modified (highlighted in green) and which variable was last changed
(highlighted in blue). It's usually only possible to see the highlighting when you are stepping or running at
less than full speed. Figure 3 shows the environment of “HelloWorld” program after completion.
1. Execute display is indicated by the highlighted tab.
2. Assembly code is displayed with its address, machine code, assembly code, and corresponding line from
the source code file.
3. The values stored in Memory are directly editable.
4. The window onto the Memory display is controlled in several ways: previous/next arrows and a menu of
common locations (e.g., top of stack).
5. The numeric base used for the display of data values and addresses (memory and registers) is selectable
between decimal and hexadecimal.
6. The values stored in Registers are directly editable.
7. Breakpoints are set by a checkbox for each assembly instruction. These checkboxes are always displayed
and available.
8. Selectable speed of execution allows the user to “watch the action” instead of the assembly program
finishing directly.
9. MARS messages are displayed on the MARS Messages tab of the message area at the bottom of the
screen. Runtime console input and output is handled in the Run I/O tab.
5
Fig 3. The environment of MARS after executing “HelloWorld” program
Closing a program
Select "File => Close" to close the current program. Always close the program before removing the program
disk or exiting Mars. Exit Mars with "File => Exit.
6
WEEK 3 and WEEK 4: Introduction to Project 1
The Format of a MIPS Assembly Program
The components of a MIPS program are as follows:
Comments: Comments start with a # sign. Everything from the # to the end of the line is a comment.
Labels: Labels are user defined names, assigned to instructions and variables. A label is an address that
starts with a letter, followed by letters and/or digits, and ends with a colon (:).
Variables: Variables are defined at the beginning of the program. The directive .data starts the variable
section.
Code: The code comes after the variables. There are two directives for the code. The .globl directive
specifies the external name of the function. For now, that name will be main. Later we will discuss how to
create additional functions. This is followed by the .text directive, which starts the code section. The
label main: comes right after the .text directive to indicate where execution should begin.
The layout of a MIPS program is as follows:
The .data and .text segment identifiers are required, but the main: label technically is optional.
Program Layout in the Memory: To execute a MIPS program memory must be allocated. The MIPS
computer can address 4 Gigabytes of memory, from address 0x00000000 to 0xffffffff. User
memory is limited to locations below 0x7fffffff. In the below figure the layout of the memory allocated
to a MIPS program is shown.
7
The purpose of the various memory segments:
• The user level code is stored in the text segment.
• Static data (data know at compile time) use by the user program is stored in the data segment.
• Dynamic data (data allocated during runtime) by the user program is stored in the heap.
• The stack is used by the user program to store temporary data during for example subroutine
calls.
• Kernel level code (exception and interrupt handlers) are stored in the kernel text segment.
• Static data used by the kernel is stored in the kernel data segment.
• Memory mapped registers for IO devices are stored in the memory mapped IO segment.
Character constants are enclosed in single quotes, for example 'a', 'Q', '4', '&'
Numeric constants are written in base 10, with an optional leading sign, e.g. 5, -17
String constants are enclosed in double quotes, for example "this is a string"
1.1 Commenting
Before we start to write the executable instructions of program, however, we'll need to write a comment
that describes what the program is supposed to do. In the MIPS assembly language, any text between a
pound sign # and the subsequent newline is interpreted as a comment.
1) Program Header: this is the overall description of the program or project, which appears at the front of
the code listing.
2) Code Block: comments identifying a block of code inside the program such as a loop or a subroutine.
3) Individual Instruction: one comment for each assembly language instruction is required.
Comments are vital for assembly language programs because they are notoriously difficult to read unless
they are properly documented. Therefore, we start by writing the following Program Header:
Even though this program doesn't do anything yet, anyone reading our program will at least know what this
program is supposed to do, and who to blame if it doesn't work. We are not finished commenting this
program, but we've done all that we can do until we know a little more about how the program will work.
8
with MIPS you will need to spend some time searching the lists of instructions for the right ones to use in
your program. Documentation for the MIPS instruction set can be found in the textbook and in MARS as
well as MIPS developers' manuals.
Luckily, as we look through the list of arithmetic instructions, we notice the addi instruction, which adds
two numbers together. The addi operation takes three operands:
1. A register that will be used to store the result of the addition. For our program, this will be $t0.
2. A register which contains the first number to be added. We select $t1 for this purpose and make note of
this in the comments. Therefore, we're going to have to place a value of 1 into $t1 before we can use
the addi instruction.
3. A 16-bit constant. In this case, since 2 is a constant that easily fits in 16 bits, we can just use 2 as the
third operand of addi .
We now know how we can add the numbers, but we have to figure out how to get the value 1 into
register $t1. To do this, we can use the li (load immediate value) instruction, which loads a 16-bit
constant into a register. the li $t1, 1 is a pseudo-instruction for addi $t1, $zero, 1 Therefore,
we arrive at the following sequence of instructions:
9
In order to run the assembly program, the assembler will generate the object code information tables. This
is done by reading and using directives, associate arbitrary names for labels or symbols with memory
locations, produce machine language, and create an object file. Note that pseudo-instructions are
instructions that assembler understands but not in machine language. In other words, to be able to perform
more complicated tasks we need to employ assembler macros called pseudo-instructions. For example, li
$t1, 1 is a pseudo-instruction for addi $t1, $zero, 1.
When a label appears alone on a line, it refers to the following memory location. Therefore, we could also
write this with the label main on its own line. This is often much better style, since it allows the use of
long, descriptive labels without disrupting the indentation of the program. It also leaves plenty of space on
the line for the programmer to write a comment describing what the label is used for, which is very
important since even relatively short assembly language programs may have a large number of labels.
Note that the MARS assembler does not permit the names of instructions to be used as labels. Therefore, a
label named add is not allowed, since there is an instruction of the same name. (Of course, since the
instruction names are all very short and fairly general, they don't make very descriptive label names
anyway.) Giving the main label its own line (and its own comment) results in the following program:
1.3.2 Syscalls
10
The end of a program is specifically defined. Similar to C, where the exit function can be called in order to
halt the execution of a program, one way to halt a MIPS program is with something analogous to calling
exit in C. Unlike C, however, if you forget to "call exit" your program will not gracefully exit when it
reaches the end of the main function. Instead, in actual practice it may blunder on through memory,
interpreting whatever it finds as instructions to execute. Generally speaking, this means that if you are
lucky, your program will terminate immediately; if you are unlucky, it will do something random and then
crash. The way to tell MARS that it should stop executing your program, and also to do a number of other
useful things, is with a special instruction called a syscall.
The syscall instruction suspends the execution of your program and transfers control to the operating
system. The operating system then looks at the contents of value register $v0 to determine what it is that
your program is asking it to do. Note that MARS syscalls don't actually transfer control to the operating
system. Instead, they transfer control to a very simple simulated operating system that is part of the MARS
program. In this case, what we want is for the operating system to do whatever is necessary to exit our
program. Looking into the syscall functions available in MARS, we see that this is done by placing the
value 10 (the number for the exit syscall) into $v0 before executing the syscall instruction. As before,
we can use the li instruction to do this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Your Exercise:
1- a) Write a program which sums two numbers specified by the user at runtime
We'll write a program named add2.asm that computes the sum of two numbers specified by the user at
runtime and displays the result on the screen.
The algorithm that this program will follow is:
1. Read the two numbers from the user. We'll need two registers to hold these two numbers. We can
use $t0 and $t1 for this.
a. Get first number from user, put into $t0.
i. load syscall read_int into $v0,
ii. perform the syscall,
iii. move the number read into $t0.
b. Get second number from user, put into $t1
i. load syscall read_int into $v0,
ii. perform the syscall,
11
iii. move the number read into $t1.
2. Compute the sum. We'll need a register to hold the result of this addition. We can use $t2 for this.
3. Print the sum.
4. Exit. We already know how to do this, using syscall.
1-b) Modify the above program to show sum of two numbers specified by the user at runtime in
Hexadecimal.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Implementing Loops
Introduction
We'll translate a while-loop, then a for-loop. You can do the do-while loop as an exercise (and you should!).
Here's a generic while-loop:
while ( <cond> )
{ <while-body> }
12
L1: if ( <cond> )
{ <while-body>
goto L1 ; }
while ( i < k )
{ k++ ;
i=i*2; }
L1: if ( i < k )
{ k++ ;
i=i*2;
goto L1 ; }
This can be straightforward to convert to MIPS. Below is some pseudocode whereby $t1 stores i, $t2
stores j, and $t3 stores k. Thus, before executing in MARS you will need to substitute some real MIPS
registers that you've learned instead of $t1, $t2, and $t3; also recall that EXIT is a label that you will
need to provide. So, give it a try!
We used the pseudo-instruction bge for convenience. Also, rather than use the multiply instruction
(which requires using two new registers, HI and LO), we multiplied by 2 by adding the value to itself.
Translating for-loops
To translate a for-loop, we'll only go part way, and translate it to if-statements and goto's. Your challenge
is to do the rest on your own. Here's a generic for-loop:
<init>L1:
if (<inverse cond>) then goto L2:
<for-body>
<update index>
goto L1 ;
L2:
13
On your own, or in the next lab, you can layer the same rules for translating for-loops, and realize that the
<for-body> may itself contain a for-loop, as nested loops do not need to be submitted in your
portfolio today.
Summary
Translating loops is fairly straightforward, because you can translate them to if-statements with goto's.
Congratulations, you've reduced the problem of translating loops to translating if-statements. Note that in
assembly, sometimes we have to use the reverse logic.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Your Exercise:
2-a) Write a program which increments from 0 to 15 and display the results in Decimal on
the console.
2-b) Modify program above to increment from 0 to 15 and display results in Hexadecimal on
the console.
14
WEEK 5: Project 1 Part A
You are tasked to calculate a specific algebraic expansion, i.e. compute the value of f and g for the
expression:
𝒈 = (𝑨𝑩𝟐 + 𝑪𝟐 𝑫𝟑 )
without using any native multiplication instructions, subroutines, and function calls. More formally, write
MIPS assembly code that accepts four positive integers A, B, C, and D as input parameters. The code shall
execute in MARS to prompt the user to enter four positive integers represented in decimal, each separated
by the Enter key. The program shall calculate f = (A4-4B3+3C2-2D) and g = (AB2+C2D3) using your own
self-written multiplication routine. The program will then output f and g in decimal and binary, using syscall
for each output.
Note: To receive credit, no multiplication, no division, and no shift instructions shall be used. Namely, do
not use any of {mul, mul.d, mul.s, mulo, mulou, mult, multu, mulu, div, divu, rem, sll, sllv, sra, srav, srl,
srlv}. The goal to compose your own division technique. In addition, use of a loop is required for credit to
realize the multiplication code. Do not use Macros, Subroutines, or Functions in this project.
The submitted program shall provide outputs in exactly the sequence and format shown above. To receive
full credit, no additional superfluous outputs shall occur.
15
WEEK 6: Project 1 Part B
You are tasked to use the same positive integers from Part A to also compute:
h = f/g;
i = (f+g) MOD h_quotient;
More formally, write MIPS code to output the result of above expression of h and i without using any built-
in MIPS/MARS instructions for multiplication or division. The values already entered for Part A for a, b,
c, and d shall be used. Output the value of h and i in {quotient with remainder} in a format as separate
decimal integers. Indicate the denominator for the remainder.
To receive credit, no multiplication, no division, and no shift instructions shall be used. Namely, do not use
any of {mul, mul.d, mul.s, mulo, mulou, mult, multu, mulu, div, divu, rem, sll, sllv, sra, srav, srl, srlv}. The
goal is to compose your own division technique. In addition, use of a loop is required for credit to realize
the division code. It is part of the project points to design a way to realize division using a loop. Do not use
of Macro, Subroutines, or Functions in this project.
You can refer to the definition of division and how division works. For example, given a positive integer
X, and a positive integer Y where X>Y then the division X/Y is computed such that unique integers Q and
R satisify X=( Y * Q + R) where 0 ≤ R < Y. The value Q is called the quotient and R is called the remainder.
Some examples are:
{X = 7, Y = 2} then 7 = 2 * 3 + 1 so Q=3 and R=1
{X = 8, Y = 4} then 8 = 4 * 2 + 0 so Q=2 and R=0
{X = 13, Y = 5} then 13 = 5 * 2 + 3 so Q=2 and R=3
The submitted program shall provide outputs in exactly the sequence and format shown above. To receive
full credit, no additional superfluous outputs shall occur.
16
WEEK 7 and WEEK 8: Introduction to Project 2
1) Write a C-prototype code which counts number of punctuation in the below sentence:
"This is a test. 1 2 3 4 5 !!"
Note: You are allowed to use ispunct() function. Punctuation list is provided in the image below:
17
str: .asciiz "abcde"
.text
# Load address of string into a0
|-------------------|
|Put your code here |
|-------------------|
strlen:
li $t0, 0 # initialize the count to zero
loop:
lb $t1, 0($a0) # load the next character into t1
beqz $t1, exit # check for the null character
|-------------------|
|Put your code here |
|-------------------|
exit:
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall
The lb instruction on line four may be unfamiliar, but it works exactly the same way as the load word
instruction with which you are familiar. Rather than loading an entire word, the lbu instruction loads a
single byte (the size of a C char).
18
lb $t2, 0($t0) # We do as always, get the first byte pointed by the
address
beqz $t2, end # if is equal to zero, the string is terminated
#if (character >= 'A'
|-------------------|
|Put your code here |
|-------------------|
upperCaseTest2:
# && character <= 'Z')
|-------------------|
|Put your code here |
|-------------------|
continue:
# Continue the iteration
addi $t0, $t0, 1 # Increment the address
j toLowerCase
isUpperCase:
# add 32, so it goes lower case
|-------------------|
|Put your code here |
|-------------------|
sb $t2, 0($t0) # store it in the string
j continue # continue iteration as always
end:
li $v0, 4 # Print the string
la $a0, string
syscall
li $v0, 4 # A nice newline
la $a0, newline
syscall
# We are done, exit the program
li $v0, 10
syscall
ASCII TABLE:
https://2.bp.blogspot.com/-
nMNrhxZPNUY/UvlF8DZQnZI/AAAAAAAAAJ8/Y3PNgaOVScs/s1600/asciifull.gif
19
WEEK 9: Project 2 Part A
The purpose of this project is to increase your understanding of data, address, memory contents, and strings.
You will be expected to apply selected MIPS assembly language instructions, assembler directives, and
system calls sufficient to handle string manipulation tasks. You are tasked to find the number of selected
letters present in a string, specifically the number of the occurrences of the letters K, N, I, G, H, T, and S
within an input sentence, and then output the result using the format shown below.
Sample Inputs for Part A is:
coming in winners of three in a row, including back-to-back triumphs on the road, the knights
returned to orlando hungry to finish out the regular season home schedule on a high note.
Sample Outputs for Part A is:
K: ----------- 3
N: ----------- 16
I: ------------ 11
G: ----------- 6
H: ---------- 12
T: ----------- 12
S: ----------- 7
K: ###
N: ################
I: ###########
G: ######
H: ############
T: ############
S: #######
The submitted program shall provide outputs in exactly the sequence and format shown above. To receive
full credit, no additional superfluous outputs shall occur.
20
WEEK 10: Project 2 Part B
The purpose of this project is to increase your understanding of data, address, memory contents, and strings.
You will be expected to apply selected MIPS assembly language instructions, assembler directives, and
system calls sufficient enough to handle string manipulation tasks. You are tasked to develop a program
that finds how many times a word is used in a given statement. To test your program, you should hardcode
the below sample statement in your code and ask the user to input two different words, which are “UCF”
and “KNIGHTS.” Your program should not be case sensitive, it should correctly find the words regardless
of how the user inputs the words, and your code should work for any words with less than 10 characters.
Sample Statement:
UCF, its athletic program, and the university's alumni and sports fans are sometimes jointly referred
to as the UCF Nation, and are represented by the mascot Knightro. The Knight was chosen as the
university mascot in 1970 by student election. The Knights of Pegasus was a submission put forth by
students, staff, and faculty, who wished to replace UCF's original mascot, the Citronaut, which was
a mix between an orange and an astronaut. The Knights were also chosen over Vincent the Vulture,
which was a popular unofficial mascot among students at the time. In 1994, Knightro debuted as the
Knights official athletic mascot.
Sample Output:
Please input first word: Knight (or KnIGhT, knight, …)
Please input second word: UCF (or ucf, UcF, …)
KNIGHT: 6
UCF: 3
KNIGHT: ######
UCF: ###
The submitted program shall provide outputs in exactly the sequence and format shown above. To receive
full credit, no additional superfluous outputs shall occur.
21
WEEK 11 and WEEK 12: Introduction to Project 3
Subroutines in MIPS
Determines the minimum of two integers
Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions,
and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware
designers include mechanisms to help programmers. In a high-level language like C or Java, most of the
details of subroutine calling are hidden from the programmer.
MIPS has special registers to send information to and from a subroutine. The registers $a0, $a1, $a2, $a3 are
used to pass arguments (or parameters) into the subroutine. The registers $v0 and $v1 are used to pass
arguments (or parameters) back from the subroutine.
The stack (and stack pointer register $sp) is used for a variety of things when using subroutines. The stack is
used to pass additional parameters to and from subroutines. It is also used to hold temporary values in
memory that a subroutine may need. Most importantly, it is used to save the current state so the subroutine
can return back to the caller once it has completed. This includes the frame pointer ($fp), the return address
register ($ra), and the caller-saved registers ($s0-$s7).
Imagine you would like a program that reads two integers from the user, determine the smaller of the two,
then prints the minimum value. One way to do this would be to have a subroutine that takes two arguments
and returns the smaller of the two. The program shown below illustrates one way to do this.
## Your Name
## min_btw_2num.asm-- takes two numbers A and B
## Compare A and B
## Print out the smaller one
## Registers used:
## You can define by yourself!
.data
p1: .asciiz "Please enter the 1st integer: "
p2: .asciiz "Please enter the 2nd integer: "
.text
main:
# Get numbers from user
li $v0, 4 # Load 4=print_string into $v0
la $a0, p1 # Load address of first prompt into $a0
syscall # Output the prompt via syscall
22
syscall # Read an integer via syscall
add $s1, $v0, $zero # Copy from $v0 to $s1
# Compute minimum
add $a0,$s0,$0 # Put argument ($s0) in $a0
add $a1,$s1,$0 # Put argument ($s1) in $a1
jal minimum # Call minimum function, result in $v0
# Output results
add $a0, $v0, $zero # Load sum of input numbers into $a0
li $v0, 1 # Load 1=print_int into $v0
syscall # Output the prompt via syscall
# Exit
li $v0, 10 # exit
syscall
Here are some important things to note about the above program. The $ra is the return address register
which stores the address where a subroutine should return to when it is completed; it is set automatically
by the jal instruction to be the address of the instruction following the jal instruction. In the subroutine,
the jr instruction sets the program counter to be the address located in the $ra register.
𝑁−1
1
𝜇 = ∑ 𝑎𝑖
𝑁
𝑖=0
Write and assembly program that reads two lists of floating point numbers A and B, and displays the
measures given above on the simulator’s console. The program’s specifications are given below:
• Each input vector should be of size 10, i.e., N=10
• The program should use PROCEDURES to compute the mean. Sample input array:
A = [0.11 0.34 1.23 5.34 0.76 0.65 0.34 0.12 0.87 0.56]
B = [7.89 6.87 9.89 7.12 6.23 8.76 8.21 7.32 7.32 8.22]
23
The program’s output should be similar to the following:
The Mean of A = 1.03
The Mean of B = 7.78
.data
A: .float 0.11 0.34 1.23 5.34 0.76 0.65 0.34 0.12 0.87 0.56
B: .float 7.89 6.87 9.89 7.12 6.23 8.76 8.21 7.32 7.32 8.22
N: .float 10
meanA: .asciiz "\nThe Mean of A ="
meanB: .asciiz "\nThe Mean of B ="
.text
24
li $v0,4 # Load 4=print_string into $v0
la $a0, meanB # Load address of second string into $a0
syscall # Output the string via syscall
#Exit
li $v0,10
syscall
#mean function
mean:
|------------------|
|Put your code here|
|------------------|
return:
|------------------|
|Put your code here|
|------------------|
25
WEEK 13: Project 3 Part A
You are tasked to calculate a specific algebraic expansion, i.e. compute the value of f and g for the
expression:
f = (0.1×A4) - (0.2×B3) + (0.3×C2) - (0.4×D)
g = (0.1×AB2) + (0.2×C2D3)
Write MIPS assembly code that accepts four positive integers A, B, C, and D as input parameters. The code
shall execute in MARS to prompt the user to enter four positive integers represented in decimal, each
separated by the Enter key. The program shall calculate f and g using your own self-written multiplication
routine. The program will then output f and g as floating-point numbers, using syscall for each output. You
are tasked to increase efficiency with respect to Dynamic and Static Instruction Count. For this purposed
you can use the Instruction Counter tool or Instruction Statistics tool in the MARS software.
Note: To receive credit, no multiplication, no division, and no shift instructions shall be used. Namely, none
of {mul, mul.d, mul.s, mulo, mulou, mult, multu, mulu, div, divu, rem, sll, sllv, sra, srav, srl, srlv} or else
a zero score will result. Thus, it is necessary to compose your own multiplication technique. In addition,
use of Subroutines is required for credit to realize the multiplication code.
Sample output for Part A is:
Enter 4 integers for A, B, C, D respectively:
15
9
21
3
f = 5047.8
g = 2502.9
The submitted program shall provide outputs in exactly the sequence and format shown above. To receive
full credit, no additional superfluous outputs shall occur.
26
WEEK 14: Project 3 Part B
Project Description:
In this project you will be using MARS simulator to run your assembly language program and data cache
hit/miss analysis.
Consider the following segment of a C code for multiplication of two matrices A and B of size (NxN) each:
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
temp = 0;
for (int k=0;k<N;k++) {
temp = temp + A[i][k]*B[k][j];
}
C[i][j] = temp;
}
}
Thus, matrix C is the product of matrix A and matrix B. For example, for N=3:
Write an assembly program for matrix multiplication assuming all elements are integers and the matrices
are stored in main memory. Initially, you may hard code the input matrices A and B of size 3x3 each in
your program. You should store them in row-major order. Observe the cache performance (e.g., hit- rate,
miss- rate etc.) for your code and try to increase the hit-rate. For this task you can use the Cache Simulator
tool of the MARS software.
27
References and Resources
A. Software Download
[A.1] Java for Windows- Download and install the Java software for Windows through below link:
http://javadl.sun.com/webapps/download/AutoDL?BundleId=76860
[A.2] Java for Mac- Download and install the Java software for Mac through below link:
http://www.java.com/en/download/index.jsp
B. Tutorials
[B.1] P. Sanderson, and K. Vollmar, “An assembly language IDE to engage students of all levels: tutorial
presentation,” Journal of Computing Sciences in Colleges 22.4, pp. 122-122, 2007.
[B.2] Intro to MARS Video- You can watch the below tutorial video for basic information regarding the
MARS software:
http://www.youtube.com/watch?v=z3ltaJ5UU5I
[B.3] Introduction to MIPS Assembly Programming- The below document by Oregon State University
provides an introduction to MIPS Assembly programming in MARS:
http://web.engr.oregonstate.edu/~walkiner/cs271-wi13/slides/04-IntroAssembly.pdf
[B.5] MIPS Memory Layout: Operating Systems I and Operating Systems and Process-Oriented
Programming Courses at the Department of Information Technology, Uppsala University, Spring 2018.
http://www.it.uu.se/education/course/homepage/os/vt18/module-0/mips-and-mars/mips-memory-layout/
28
C. ASCII Table
29