MIPS Assembly Language Programming: Lesson #1
MIPS Assembly Language Programming: Lesson #1
MIPS Assembly Language Programming: Lesson #1
Programming
Bob Britton, Instructor
Lesson #1
Required Background
Preparation
Two semesters of programming experience
Benefits of Studying Assembly
Language Programming
Obtain Insights into writing more efficient code
• Machine Language
• Assembly Language
MIPS
MIPS
PowerPC
MIPS Computer Organization
• Datapath Diagram
• Control Logic
DataPath Diagram
Program Counter (PC)
Cache Memory
Instruction Register
Out ALU
Control
Rs
Address Logic
Rt
Rd
Data In 4
Register File
Number Value Name
Register
0 $zero
1 $at
File 3
5
$v1
$a0
$a1
from functions
Pass parameters
6 $a2
to functions
7 $a3
8 $t0
9 $t1
Caller Saved
10 $t2
Registers –
11 $t3
Use these registers
12 $t4
in functions
13 $t5
14 $t6
15 $t7
16 $s0
17 $s1 Callee-Saved
18 $s2 Registers –
19 $s3 Use these registers for values
20 $s4 that must be maintained
21 $s5 across function calls.
22 $s6
23 $s7
24 $t8
An Example MIPS Assembly
Language Program
Label Op-Code Dest. S1, S2 Comments
move $a0, $0 # $a0 = 0
li $t0, 99 # $t0 = 99
loop:
add $a0, $a0, $t0 # $a0 = $a0 + $t0
addi $t0, $t0, -1 # $t0 = $t0 - 1
bnez $t0, loop # if ($t0 != zero) branch to loop
li $v0, 1 # Print the value in $a0
syscall
li $v0, 10 # Terminate Program Run
syscall
Three Instruction Word Formats
• Register Format
Op-Code
Op-Code Rs
Rs Rt
Rt Rd
Rd Code
Code
6 5 5 5 6
• Immediate Format
Op-Code
Op-Code Rs
Rs Rt
Rt 16--Bit
16 Bit Immediate
ImmediateValue
Value
6 5 5 16
• Jump Format
Op-Code
Op-Code 26Bit
26 BitCurrent
CurrentSegment
SegmentAddress
Address
6 26
Main Memory Program Counter (PC)
4
Decode Instruction
lw or sw beqz
Read from Reg. File
Address = Rs + Offset
R-Type
If (Rs == 0 ) then
sw PC = PC + Offset
Memory[Address] = Rt
lw
Lesson #2
Exercises – Chapter 1
1.1 Explain the difference between a register and the ALU.
1.2 Explain the difference between Assembly Language and Machine Language.
1.3 Explain the difference between Cache Memory and the Register File.
1.4 Explain the difference between the Instruction Register and the Program Counter.
1.5 Explain the difference between a Buss and a control line.
1.6 Identify a kitchen appliance that contains a finite state machine.
1.7 If a 500 MHz machine takes one-clock cycle to fetch and execute an instruction, then
what is the instruction execution rate of the machine?
1.8 How many instructions could the above machine execute in one minute?
1.9 Let’s suppose we have a 40-year-old computer that has an instruction execution rate
of one thousand instructions per second. How long would it take in days, hours, and
minutes, to execute the same number of instructions that you derived for the 500 MHz
machine?
1.10 What is an algorithm?
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #3
Pseudocode
Using Pseudocode to Document a MIPS Assembly Language Program
When documenting an algorithm in a language
such as Pascal or C, programmers use descriptive
variable names such as: speed, volume, size,
count, amount, etc. After the program is compiled,
these variable names correspond to memory
locations. To efficiently execute code, a compiler
will attempt to keep the variables that are
referenced most often in processor registers
because access to a variable in a processor
register is much faster than access to memory.
MIPS has 32 processor registers. The names used
to reference these registers are defined in Figure
2.1 on page 4 in the textbook.
As an assembly language programmer you must take
maximum advantage of the processor registers.
For example, you may have a value in register $s0
corresponding to speed, a value in register $s1
corresponding to volume, a value in register $s2
corresponding to size, and a value in register $t3
corresponding to count.
On the next slide you will see a pseudocode description of the algorithm
and following that the corresponding assembly language program, where
processor register $t0 is used to accumulate the sum, and processor
register $v0 is used as a loop counter.
Next, load the program into SPIM. Run the program and experiment with
the different features of the MIPS simulator. ( For example: Single Step)
Read the help file for a description of how to use the simulator.
An Example MIPS Program
# Program #1 : (descriptive name) Programmer: YOUR NAME
# Due Date : Sep. 13, 2001 Course: CSCI 51A
# Last Modified: Sep. 12, 2001 Section: 2
#########################################################
# Functional Description: Find the sum of the integers from 1 to N where
# N is a value input from the keyboard.
#########################################################
# Algorithmic Description in Pseudocode:
# main: v0 << value read from the keyboard (syscall 4)
# if (v0 < = 0 ) stop
# t0 = 0; # t0 is used to accumulate the sum
# While (v0 >= 0) { t0 = t0 + v0; v0 = v0 - 1}
# Output to monitor syscall(1) << t0; goto main
##########################################################
# Register Usage: $t0 is used to accumulate the sum
# $v0 the loop counter, counts down to zero
##########################################################
.data
prompt: .asciiz "\n\n Please Input a value for N = "
result: .asciiz " The sum of the integers from 1 to N is "
bye: .asciiz "\n **** Adios Amigo - Have a good day **** "
.globl main
.text
main: li $v0, 4 # system call code for print_str
la $a0, prompt # load address of prompt into a0
syscall # print the prompt message
li $v0, 5 # system call code for read_int
syscall # reads a value of N into v0
blez $v0, done # if ( v0 < = 0 ) go to done
li $t0, 0 # clear $t0 to zero
loop: add $t0, $t0, $v0 # sum of integers in register $t0
addi $v0, $v0, -1 # summing in reverse order
bnez $v0, loop # branch to loop if $v0 is !=
zero
li $v0, 4 # system call code for print_str
la $a0, result # load address of message into $a0
syscall # print the string
li $v0, 1 # system call code for print_int
move $a0, $t0 # a0 = $t0
syscall # prints the value in register $a0
b main
done: li $v0, 4 # system call code for print_str
la $a0, bye # load address of msg. into $a0
syscall # print the string
li $a0, 0 # $a0 = 0
li $t0, 10 # Initialize loop counter to 10
loop:
add $a0, $a0, $t0
addi $t0, $t0, -1 # Decrement loop counter
bgtz $t0, loop # If ($t0 >0) Branch to loop
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #4
Translation of a “switch statement”
Pseudocode Description:
$s0 = 32;
top: cout << “Input a value from 1 to 3”
cin >> $v0
switch($v0)
{
case(1): { $s0 = $s0 << 1; break}
case(2): { $s0 = $s0 << 2; break}
case(3): { $s0 = $s0 << 3; break}
default: goto top ;
}
cout <<$s0
“switch statement” continued
.data
.align 2
jumptable: .word top, case1, case2, case3
prompt: .asciiz "\n\n Input a value N from 1 to 3: "
result: .asciiz " The value 32 shifted left by N bits is now = "
.text
main:
li $s0, 32
top:
li $v0, 4 # Code to print a string
la $a0, prompt
syscall
li $v0, 5 # Code to read an integer
syscall
bltz $v0, exit
beqz $v0, top # Default for less than one
li $t3, 3
bgt $v0, $t3, top # Default for greater than 3
la $a1, jumptable
sll $t0, $v0, 2 # Create a word offset
add $t1, $a1, $t0 # Form a pointer into jumptable
lw $t2, 0($t1) # Load an address from jumptable
jr $t2 # Go to specific case
“switch statement”
continued
case1:
sll $s0, $s0, 1
b done
case2:
sll $s0, $s0, 2
b done
case3:
sll $s0, $s0, 3
done:
li $v0, 4 # Code to print a string
la $a0, result
syscall
li $v0, 1 # Code to print a value
move $a0, $s0
syscall
bgez $s1, main
exit:
li $v0, 10
syscall
Exercises – Chapter 2
E1h:
E1i:
E1j:
Exercises – Chapter 2
(m) t0 = 2147483647 - 2147483648;
(n) s0 = -1 * s0;
(o) s1 = s1 * a0;
(p) s2 = srt(s02 + 56) / a3;
(q) s3 = s1 - s2 / s3;
(r) s4 = s4 * 8;
(s) s5 = * s5;
2.2 Analyze the assembly language code that you developed for each of the
above pseudocode expressions and calculate the number of clock cycles
required to fetch and execute the code corresponding to each expression. (Assume it
takes one clock cycle to fetch and execute every instruction except multiply and
divide, which require 32 clock cycles and 38 clock cycles respectively.)
2.3 Show how the following expression can be evaluated in MIPS assembly
language, without modifying the contents of the “s” registers:
$t0 = ( $s1 - $s0 / $s2) * $s4 ;
Exercises Continued
2.2 Analyze the assembly language code that you developed for each of the
above pseudocode expressions and calculate the number of clock cycles
required to fetch and execute the code corresponding to each expression.
(Assume it takes one clock cycle to fetch and execute every instruction
except multiply and divide, which require 32 clock cycles and 38 clock
cycles respectively.)
2.3 Show how the following expression can be evaluated in MIPS assembly
language, without modifying the contents of the “s” registers:
$t0 = ( $s1 - $s0 / $s2) * $s4 ;
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #5
MIPS
MIPS
Three Instruction Word Formats
• Register Format
Op-Code
Op-Code Rs
Rs Rt
Rt Rd
Rd Code
Code
6 5 5 5 6
• Immediate Format
Op-Code
Op-Code Rs
Rs Rt
Rt 16--Bit
16 Bit Immediate
ImmediateValue
Value
6 5 5 16
• Jump Format
Op-Code
Op-Code 26Bit
26 BitCurrent
CurrentSegment
SegmentAddress
Address
6 26
A Register Transfer Description
of the Control Logic
IR = Mem[PC]
PC = PC + 4
Decode Instruction
lw or sw beqz
Read from Reg. File
Address = Rs + Offset
R-Type
If (Rs == 0 ) then
sw PC = PC + Offset
Memory[Address] = Rt
lw
45 Remainder 101101
22 1
11 0
5 1
2 1
1 0
0 1
Practice - Convert 25 to Binary
Divide by 2 and record the remainder
25 Remainder
12
To represent binary values in the positive
and negative domains we use the
Two’s Complement Number System
Here is the polynomial expansion of a two’s
complement number 8-bit binary number N:
Scan the binary number from right to left leaving all least significant
zeros (0) and the first one (1) unchanged, and then complementing the
remaining digits to the left: 11100110
01000100 = 68
+ 00111100 = 60
10000000 = -128 Overflow Occurred
Overflow
0000
1111 0001
0
1110 -1 1 0010
-2 2
0011
1101 3
-3
1100 -4 4 0100
-5
1011 5
-6 0101
6
1010 -7 7
-8 0110
0111
1001
1000
Binary Arithmetic
in the Two’s Complement Number System
Here is a subtraction example where we assume
we are limited to 8 binary digits. To subtract in
binary we always add the two’s complement of the
subtrahend.
01000100 = 01000100 68
-00111100 = +11000100 60
00001000 = 00001000 = 8
The Rule for
Detection of Overflow
#################################################
Adding numbers of opposite signs, overflow is impossible.
When adding numbers of the same sign, if the result is not the
same as the operands then overflow occurred.
#################################################
Here is an example:
You are given the following two numbers in two’s complement representation.
Perform the binary subtraction and indicate if there is signed overflow.
______
Explain Why: 11101000 = -24
11101000 +11101101 = -19
-00010011 11010101 Correct
Result = -43
Sign Extension
The value –43 as an 8-bit binary number is: 11010101
The value –43 as an 32-bit binary number is:
11111111111111111111111111010101
In Hexadecimal –43 appears as: 0xFFFFFFD5
###############################################
Lesson #6
Chapter 2 Exercises Continued
2.3 Show how the following expression can be evaluated in MIPS assembly
language, without modifying the contents of the “s” registers:
$t0 = ( $s1 - $s0 / $s2) * $s4 ;
2.4 The datapath diagram for the MIPS architecture shown in figure 1.1
with only one memory module is referred to as a von Neumann architecture.
Most implementations of the MIPS architecture use a Harvard Architecture,
where there are two separate memory modules, one for instructions and
the the other for data. Draw such a datapath diagram.
2.5 Show how the following pseudocode expression can be efficiently evaluated
in MIPS assembly language, without modifying the contents of the “s”
registers:
$t0 = ( $s0 / 8 - 2 * $s1 + $s2 ;
Solution to Exercises 2.3 & 2.5
Clock Cycles
E2.3 div $s0, $s2 38
mflo $t0 1
sub $t0, $s1, $t0 1
mult $t0, $s4 32
mflo $t0 1
Total= 73
1111111100111100
0xFF88
-128
1111111111111010
0x0011
-25
3.19 You are given the following two numbers in two’s complement representation.
Perform the binary addition and indicate if there is signed overflow. __
Explain Why:
01101110
00011010
10001000 Yes overflow occurred – Sign of the result is different
from the operands
Exercises
3.20 You are given the following two numbers in two’s complement representation.
Perform the binary subtraction and indicate if there is signed overflow. ______
Explain Why:
11101000 11101000
-00010011 + 11101101
11010101 No Overflow
3.21 FF88
Sign extend the 8 bit hex number 0x88 to a 16 bit number. 0x_________
3.23 You are given the following two 8-bit binary numbers in the two’s complement number
system. What values do they represent in decimal?
-108
X = 10010100 = __________ 44
Y = 00101100 = __________
2 10 2 10
Lesson #7
SPIM - The MIPS Simulator,
Register Window
Text Window
Data Window
Message Window
Console
Text Segment
[0x00400020] 0x34020004 ori $2, $0, 4 ; 34: li $v0, 4
[0x00400024] 0x3c041001 lui $4, 4097 [prompt] ; 35: la $a0, prompt
[0x00400028] 0x0000000c syscall ; 36: syscall
[0x0040002c] 0x34020005 ori $2, $0, 5 ; 38: li $v0, 5
[0x00400030] 0x0000000c syscall ; 39: syscall
[0x00400034] 0x1840000d blez $2 52 [end-0x00400034] ; 41: blez $v0, end
[0x00400038] 0x34080000 ori $8, $0, 0 ; 42: li $t0, 0
[0x0040003c] 0x01024020 add $8, $8, $2 ; 44: add $t0, $t0, $v0
[0x00400040] 0x2042ffff addi $2, $2, -1 ; 45: addi $v0, $v0, -1
[0x00400044] 0x14403ffe bne $2, $0, -8 [loop-0x00400044]; 46: bnez $v0, loop
[0x00400048] 0x34020004 ori $2, $0, 4 ; 47: li $v0, 4
[0x0040004c] 0x3c011001 lui $1, 4097 [result] ; 48: la $a0, result
[0x00400050] 0x34240022 ori $4, $1, 34 [result]
[0x00400054] 0x0000000c syscall ; 49: syscall
Analyzing the Data Segment
.data
prompt: .asciiz “\n Please Input a value for N = ”
result: .asciiz “ The sum of the integers from 1 to N is ”
bye: .asciiz “ **** Adios Amigo – Have a good day ****”
a e l P e s
[0x10010000] 0x2020200a 0x61656c50 0x49206573 0x7475706e
[0x10010010] 0x76206120 0x65756c61 0x726f6620 0x3d204e20
[0x10010020] 0x20200020 0x65685420 0x6d757320 0x20666f20
[0x10010030] 0x20656874 0x65746e69 0x73726567 0x6f726620
[0x10010040] 0x2031206d 0x4e206f74 0x20736920 0x20200a00
Op-Code Rs Rt Imm
001101ssssstttttiiiiiiiiiiiiiiii
00110100000000100000000000001010
0x 3 4 0 2 0 0 0 A
Translating Assembly Language to Machine language
R-Type Instruction
Use the information in Appendix C to verify that 0x01024020
is the correct machine language encoding of the instruction
add $8, $8, $2 add $t0, $t0, $v0
In Appendix C we are shown how this instruction is encoded in
binary
add Rd, Rs, Rt # RF[Rd] = RF[Rs] + RF[Rt]
000000ssssstttttddddd00000100000
00000001000000100100000000100000
0x 0 1 0 2 4 0 2 0
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #8
Exercise 4.1
Translate the following assembly language instructions to their
corresponding machine language codes as they would be
represented in hexadecimal. (Hint – Refer to Appendix C and
Appendix D.)
Op-Code Rs Rt Offset
101000ssssstttttiiiiiiiiiiiiiiii
10100010110001000000000000000100
0xA 2 C 4 0 0 0 4
Translating Assembly Language Shift Instruction to Machine Language
Op-Code Rt Rd sa
00000000000tttttdddddvvvvv000010
00000000000101111011100100000010
0x0 0 1 7 B 9 0 2
PCSpim Translation
Lesson #9
An Example Exam Question
Write a program that will compute the sum of the even positive values,
minus the odd negative values in an array of words. Stop when a value
of zero is found in the array. For Example:
array: .word -29, -30, 75, 34, -2, 90, -11, 98, 1, 0, 76
2. You are given the following two numbers in two’s complement representation.
Perform the binary subtraction and indicate if there is signed overflow ? ______
Explain Why:
10101100
- 00001100 10101100
11110100
10100000
3. You are given the following two numbers in two’s complement representation.
Perform the binary addition and indicate if there is signed overflow ? ______
Explain Why:
10100100
+ 11101100
Example Exam Questions
Sign extend the 2-digit hex number 0x90 to a 4-digit hex number. 0x_______
sum: li $v0, 0
li $v1, 0 # Initialize v0 and v1 to zero
loop:
blez $a1, retzz # If (a1 <= 0) Branch to Return
addi $a1, $a1, -1 # Decrement loop count
lw $t0, 0($a0) # Get a value from the array
addi $a0, $a0, 4 # Increment array pointer to next word
bltz $t0, negg # If value is negative Branch to negg
add $v0, $v0, $t0 # Add to the positive sum
b loop # Branch around the next two instructions
negg:
add $v1, $v1, $t0 # Add to the negative sum
b loop # Branch to loop
retzz: jr $ra # Return
Classroom Exercises to Strengthen Your Mental Muscle
The class is presented with a challenging assembly language
programming exercise.
Groups review and compare each others code, looking for the
most efficient code in terms of the Figure of Merit (FM):
test:
Solution to Exam#1 - Pseudocode
###### Solution to Exam# 1 ########
# a0 = 0
# t0 = &M
# t1 = Mem(t0)
# t2 = Mem(t0 + 4)
# t3 = t1 & 1
# if (t3 == 0) t1 = t1 +1
# do {a0 = a0 + t1
# t1 = t1 + 2
# } while ( t1 <= t2)
# syscall(1) << a0
# exit
################################
MIPS Assembly Language Code
.data
M: .word 4
N: .word 10
.text
main:
li $a0, 0
la $t0, M
lw $t1, 0($t0)
lw $t2, 4($t0)
andi $t3, $t1, 1
bnez $t3, test
addi $t1, $t1, 1
b test
loop:
add $a0, $a0, $t1
addi $t1, $t1, 2
test:
ble $t1, $t2, loop
li $v0, 1
syscall
li $v0, 10
syscall
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #10
Exercise 5.2
Write an efficient segment of MIPS assembly
language code to transfer a block of 100 words
starting at memory location “SRC” to another area
of memory beginning at memory location “DEST”.
Exercise 5.2 (Pseudo Code)
$a1= &SRC; # “&” means “Address of”
$a2= &DEST;
for ($t0 = 100; $t0 > 0; $t0 =$t0 -1)
{$t1 = Mem($a1);
Mem($a2) = $t1;
$a1= $a1 + 4;
$a2= $a2 + 4;
}
Exercise 5.2 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2 Comments
.data
SRC: .space 400
DEST: .space 400
.globl main
.text
main:
la $a1, SRC # $a1 = &SRC
la $a2, DEST #$a2 = &DEST
li $t0, 100 #$t0 = 100
loop: lw $t1, 0($a1) #$t1= Mem($a1)
sw $t1, 0($a2) #Mem($a2) = $t1
addi $a1, $a1,4 #$a1 = $a1+4
addi $a2, $a2,4 #$a2 = $a2+4
addi $t0, $t0, -1 # $t0 = $t0 - 1
bgtz $t0, loop #Branch if $t0 > 0
li $v0, 10
syscall
Exercise 5.3
Write a MIPS function which accepts an integer
word in register $a0 and returns its absolute
value in $a0.
Lesson #11
Exercise 5.4
Write a function PENO (&X, N, SP, SN) that will
find the sum of the positive and negative values
in an array X of length “N”.
"X" the address of an array, passed through $a0.
"N" is the length of the array, passed through $a1.
The procedure should return two values:
(1) The sum of all the positive elements in
the array, passed back through $v0.
(2) The sum of all the negative elements in
the array, passed back through $v1.
Exercise 5.4 (Pseudo Code)
$v0 = 0;
$v1 = 0;
for ( ; $a1 > 0; $a1 = $a1-1)
{$t0 = Mem($a0);
$t1 = $t0 & 1;
$a0 = $a0 + 4;
if ($t0 > 0 & $t1 = 0) $v0 = $v0 +
$t0;
if ($t0 < 0 & $t1 != 0) $v1= $v1+
$t0;
}
return;
Exercise 5.4 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2 Comments
.globl SUM
.text
PENO:
li $v0, 0
li $v1, 0
LOOP:
lw $t0, 0($a0)
andi $t2, $t0, 1
addi $a0, $a0, 4
bltz $t0, NEG
bnez $t2, CHK
add $v0, $v0, $t0
b CHK
NEG:
beqz $t2, CHK
add $v1, $v1, $t0
CHK:
addi $a1, $a1, -1
bgtz $a1, LOOP
jr $ra
A Function that takes a Binary Value and prints the
equivalent Decimal Representation, Right Justified
.text
SUM:
addi $v0, $a0, 1 # $v0 = $a0 + 1
mult $v0, $a0 # $v0 = $v0 * $a0
mflo $v0
sra $v0, $v0, 1 # Shift right arithmetic
# is the quick way to
# divide by 2
jr $ra
The Main Program
.data
N: .word 9, 10, 32666, 32777, 654321
.text
main: li $s0, 5
la $s1, N
loop:
lw $a0, 0($s1)
addiu $s1, $s1, 4
jal SUM
move $a0, $v0
li $v0, 1
syscall
addi $s0, $s0, -1
bnez $s0, loop
li $v0, 10
syscall
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #12
Exercise 5.6
Write a function FIB(N, &array) to store the
First N elements of the Fibonacci sequence
into an array in memory. The value N is
passed in $a0, and the address of the array
is passed in register $a1.
The first few numbers of the Fibonacci sequence
are: 1, 1, 2, 3, 5, 8, 13, ............
Exercise 5.6 (Pseudo Code)
Mem($a1) = 1;
Mem($a1 + 4) = 1;
for ($a0 = $a0 - 2; $a0 > 0; $a0 = $a0-1)
{
Mem($a1+8) = Mem($a1) + Mem($a1+4);
$a1 = $a1 + 4;}
return;
Exercise 5.6 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2 Comments
fib: li $t0, 1
sw $t0, 0($a1)
sw $t0, 4($a1)
addi $a0, $a0, -2
loop:
lw $t0, 0($a1)
lw $t1, 4($a1)
add $t0, $t0, $t1
sw $t0, 8($a1)
addi $a1, $a1, 4
addi $a0, $a0, -1
bgtz $a0, loop
jr $ra
Exercise 5.7
Lesson #13
Functional Descriptions of Code Modules
A functional description will provide the information
anyone needs to know if they are searching for a function
that would be useful is solving some larger programming
assignment. The functional description only describes
what the function does, not how it is done.
The functional description must explain how arguments
are passed to the function and how results are returned.
The following is an example functional description:
Hexout($a0: value)
A 32-bit binary value is passed to the function in register $a0
and the hexadecimal equivalent is printed out right justified.
Exercise 5.9
Write a function to search through an array “X” of
“N” words to find the minimum and maximum
values. The address of the array will be passed to
the function using register $a0, and the number of
words in the array will be passed in register $a1.
The minimum and maximum values are returned
in registers $v0, & $v1.
Exercise 5.9 (Pseudo Code)
MaxMin ($a0: address, $a1: number of words)
$v0 = Mem($a0);
$v1 = $v0;
$a1 = $a1 - 1;
While ($a1 > 0)
{$a0 = $a0 + 4;
$t0 = Mem($a0);
if ($t0 < $v0) $v0 = $t0;
else if ($t0 > $v1) $v1 = $t0;
$a1= $a1 - 1;
}
return;
Exercise 5.9 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2 Comments
MaxMin:
lw $v0, 0($a0)
move $v1, $v0
addi $a1, $a1, -1
blez $a1, ret
loop:
addi $a0, $a0, 4
lw $t0, 0($a0)
bge $t0, $v0, next
move $v0, $t0
b chk
next:
ble $t0, $v1, chk
move $v1, $t0
chk:
addi $a1, $a1, -1
bgtz $a1, loop
ret:
jr $ra
Exercise 5.10
Write a function to find the sum of the main
diagonal elements in a two dimensional N by N
array of 32 bit words. The address of the array
and the size N are passed to the procedure in
registers $a0 and $a1 respectively.
Lesson #14
Exercise 5.12
Write a MIPS assembly language function that
accepts a binary number in register $a0 and
returns a value corresponding to the number of
one’s in the binary number.
Exercise 5.12(Pseudocode)
$v0 = 0;
while ($a0 = !0)
{
$t0 = $a0 & 1;
$a0 = $a0 >> 1;
$v0 = $v0 + $t0;
}
Return
Exercise 5.12 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2 Comments
.globl count
.text
count:
li $v0, 0
while:
andi $t0, $a0, 1
srl $a0, $a0, 1
add $v0, $v0, $t0
bnez $a0, while
jr $ra
Exercise 5.13
Translate the following pseudocode expression to MIPS assembly
language code. Include code to insure that there is no array
bounds violation when the store word (sw) instruction is executed.
Note that the array “zap” is an array containing 50 words, thus the
value in register $a0 must be in the range from 0 to 196. Include
code to insure that the value in register $a0 is a word address
offset into the array “zap.” If an array bounds violation is detected
or the value in register $a0 is not a word address offset then
branch to the label “Error.”
.data
zap: .space 200
.text
...
zap[$a0] = $s0
Exercise 5.13 (Pseudocode)
$t0 = $a0 & 3;
If ($t0 != 0 ) go to Error;
if ($a0 < 0) go to Error
if ($a0 > 196) go to Error
$t0 = &zap
$a0 = $a0 + $t0
Mem($a0) = $s0;
Exercise 5.13 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2 Comments
.data
zap: .space 200
.text
andi $t0, $a0, 3
bnez $t0, Error
bltz$a0, Error
li $t0, 196
bgt $a0, $t0, Error
la $t0, zap
add $a0, $a0, $t0
sw $s0, 0($a0)
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #15
Exercise 5.14
Write a function to search through an array “X” of
“N” words to find how many of the values are
evenly divisible by four. The address of the array
will be passed to the function using register $a0,
and the number of words in the array will be
passed in register $a1. Return the results in
register $v0.
Exercise 5.14 (Pseudocode)
$v0 = 0;
$t3 = 3;
For ( ; $a1 > 0; $a1= $a1- 1)
{ $t2 = Mem ($a0);
$a0 = $a0 + 4;
$t0 = $t2 & t3;
If ($t0 == 0 ) $v0 = $v0 + 1;
}
return
Exercise 5.14 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2
Comments
Div4:
li $v0, 0
li $t3, 3
b skip
loop:
lw $t2, 0($a0)
addi $a0, $a0, 4
and $t0, $t2, $t3
bnez$t0, skip
addi $v0, $v0, 1
skip:
addi $a1, $a1, -1
bgez$a1, loop
jr $ra
Chapter 6
Passing Arguments on the Stack
Program Counter (PC)
Cache Memory
Instruction Register
Out ALU
Control
Rs
Address Logic
Rt
Rd
Data In 4
Register File
The Stack is in Memory and Register $sp
Points to the Top of Stack
Chapter 6
Passing Arguments on the Stack
An Example of Jack calling Jill(A, B, C, D, E)
$sp
$a0
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #16
Exercise 6.1
MinMax (&X, N, Min, Max)
Lesson #17
Memory Layout
0x00000000
Reserved
0x00400000
Text Segment
0x10000000
Data Segment
Stack Segment
0x70000000
Operating System
0xFFFFFFFF
Exercise 6.2
Search(&X, N, V, L)
Write a function to sequentially search an array X of N
bytes for the relative location L of a value V.
The parameters &X, N, and V are passed to the
procedure on the stack, and the relative location L
(a number ranging from 1 to N) is returned on the
stack.
If the value V is not found the value -1 is returned for
L.
Exercise 6.2 (Pseudocode)
$t3= Mem(sp); # get &X
$t1= Mem($sp + 4); # get N
$t0= Mem($sp + 8); # get V
$t2=$t1;
for ($t2 = $t2 - 1; $t2 >= 0; $t2= $t2 - 1)
{ $t4 = mem($t3);
$t3=$t3 + 1;
if ( $t4 == $t0) go to found;
}
Mem(sp + 12) = -1;
go to exit;
found:
Mem(sp + 12) = $t1- $t2;
exit: return;
Exercise 6.2 (MIPS Assembly Language)
Label Op-Code Dest. S1, S2 Comments
.text
search:
lw $t3, 0($sp) # get &X
lw $t1, 4($sp) # get N
lw $t0, 8($sp) # get V
move $t2, $t1
addi $t2, $t2, -1 # t2 = N - 1
loop:
lbu $t4, 0($t3) # get a character
addiu $t3, $t3, 1 # increment pointer
beq $t4, $t0, found
addi $t2, $t2, -1 # decrement loop counter
bgez $t2, loop
li $t4, -1
sw $t4, 12($sp)
b exit
found:
sub $t1, $t1, $t2
sw $t1, 12($sp)
exit: jr $ra
Exercise 6.3
Scan(&X, N, U, L, D)
Write a function to scan an array 'X' of 'N' bytes counting how many
bytes are ASCII codes for:
a. upper case letters - U
b. lower case letters - L
c. decimal digits - D
Return the counts on the stack. The address of the array and the number of bytes N
will be passed to the function on the stack.
lw $t0, 8($sp)
lw $t1, 12($sp)
lw $t2, 16($sp)
addi $sp, $sp, 20 # Deallocate
----
Exercise 6.3 (Pseudocode)
Scan(&X:$t6, N:$t2, U:$t3, L:$t4, D:$t5)
$t6 = Mem(sp) # &X
$t2 = Mem(sp+4) # N
$t3=$t4=$t5=0;
For ( ; $t2> 0; $t2=$t2-1)
{
$t1 = mem($t6) # get a byte
$t6 = $t6 + 1
if ( $t1 >= 65 && $t1 <= 90 ) $t3 = $t3+1;
else if ( $t1 >= 97 && $t1 <= 122) $t4=$t4+1;
else if ( $t1 >= 48 && $t1 <= 57 ) $t5=$t5+1;
}
Mem(sp + 8 ) = $t3;
Mem(sp + 12 ) = $t4;
Mem(sp + 16 ) = $t5;
return;
Exercise 6.3 (Assembly Language Initialize)
Scan(&X:$t6, N:$t2, U:$t3, L:$t4, D:$t5)
Label Op-Code Dest. S1, S2 Comments
scan:
lw $t6, 0($sp) # Get &X
lw $t2, 4($sp) # Get Value N
li $t3, 0 # Count of Upper Case
li $t4, 0 # Count of Lower Case
li $t5, 0 # Count of Decimal Digits
blez $t2, done
li $t0, 48 # ASCII “0”
li $t9, 57 # ASCII “9”
li $t7, 97 # ASCII “a”
li $t8, 122 # ASCII “z”
addiu $sp, $sp, -8 # Allocate Temp Space
sw $s6, 0($sp) # Save s6
sw $s7, 4($sp) # Save s7
li $s6, 65 # ASCII “A”
li $s7, 90 # ASCII “Z”
Exercise 6.3 (Assembly Language Body)
Label Op-Code Dest. S1, S2 Comments
loop: lbu $t1, 0($t6)
addi $t6, $t6, 1
blt $t1, $s6, num # “A”
bgt $t1, $s7, lowc # “Z”
addi $t3, $t3, 1
b check
lowc:
blt $t1, $t7, check # “a”
bgt $t1, $t8, check # “z”
addi $t4, $t4, 1
b check
num:
blt $t1, $t0, check # “0”
bgt $t1, $t9, check # “9”
addi $t5, $t5, 1
check:
addi $t2, $t2, -1
bgtz $t2, loop
Exercise 6.3 (Assembly Language Continued)
Label Op-Code Dest. S1, S2 Comments
Lesson #18
Exercise 6.4
Hypotenuse(A, B, H)
This is an exercise in calling nested functions and passing
parameters on the stack.
jal hypotenuse
Lesson #19
Exercise 6.5
AVA (&X, &Y, &Z, N, status)
Write a function to perform an absolute value vector addition.
Use the stack to pass parameters. The parameters are:
the starting address of three different word arrays (vectors) : X, Y, Z,
and an integer value N specifying the size of the vectors.
Lesson #20
Exercise 6.6
Fibonacci (N, E)
Write an function to return the Nth element in the Fibonacci sequence.
A value N is passed to the function on the stack, and the Nth Fibonacci
number E is returned on the stack.
The address of the array and the value N will be passed to the function
on the stack.
Lesson #21
Reentrant Functions
See Appendix A
Read String has the same semantics as the Unix library routine fgets.
It reads up to n – 1 characters into a buffer and terminates the string
with a null byte. If fewer than n – 1 characters are on the current line,
Read String reads up to and including the newline and again
null-terminates the string
J u l y i s Ho t
Exercise 7.1 (Pseudocode)
sp = sp – 128; # Allocate space for 2 buffers 64 bytes each
a0 = sp; # Initialize pointer to input buffer
a1 = 61;
v0 = 8;
syscall; # Read a string
a1= sp + 127; # Initialize pointer to end out output buffer
mem(a1) = 0; # null terminator
loop:
t1 = mem(a0);
a0 = a0 +1;
if (t1 == 10) go to print;
a1 = a1 –1;
mem(a1) = t1;
go to loop;
print:
a0 = a1;
v0 = 4;
syscall;
sp = sp + 128; # Allocate space
return
Exercise 7.1 (Assembly Language)
rev: addi $sp, $sp, – 128 # Allocate buffer space ######
move $a0, $sp # Initialize pointer
li $a1 , 61
li $v0, 8
li $t4, 10
syscall # Read a string
addi $a1, $sp, 127 # Initialize pointer to end
sb $0, 0($a1) # null terminator
loop:
lb $t1, 0($a0)
addi $a0, $a0, 1
beq $t1, $t4, print
addi $a1, $a1, –1
sb $t1, 0($a1)
b loop
print:
li $a0, $a1
li $v0, 4
syscall
addi $sp, $sp, 128 # De-allocate buffer space ####
jr $ra
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #22
Exercise 7.2
Palindrome (b)
Write a reentrant function that will determine if a string is a palindrome.
The function should read in a string (14 characters max)
placing them in a buffer on the stack.
Lesson #23
Exercise 7.6
Write an efficient MIPS assembly language function Scan(&X, Num)
that will scan through a string of characters with the objective of locating where
all the lower case vowels appear in the string, as well as counting how many total
lower case vowels appeared in a string. Vowels are the letters a, e, i, o, u.
The address of the string "X" is passed to the function on the stack, and the
number of vowels found “NUM” is returned on the stack. A null character
terminates the string. Within this function, you must include code to call any
student’s “PrintDecimal” function to print, right justified, the relative position
within the string where each vowel was found. Notice this will be a nested
function call. Here is an example string: “The quick brown fox.”
For the above example string the output of your program would be
A Vowel was Found at Relative Position : 3
A Vowel was Found at Relative Position : 6
A Vowel was Found at Relative Position : 7
A Vowel was Found at Relative Position : 13
A Vowel was Found at Relative Position : 18
Analyze your Scan function. Is it a reentrant function? ___(yes/no) Explain why.
Exercise 7.6 (Scan Assembly Language Code)
.text
scan:
lw $t5, 0($sp) # get &X
li $t6, 0 # Num
li $t8, 0 # Relative Position
again:
li $t0, 0x61 # "a"
li $t1, 0x65 # "e"
li $t2, 0x69 # "i"
li $t3, 0x6f # "o"
li $t4, 0x75 # "u"
loops:
lbu $t7,0($t5) # Get a character
beqz $t7, ends
addi $t5, $t5, 1 # Inc. string pointer
addi $t8, $t8, 1 # Inc. relative position
beq $t7, $t0, print
beq $t7, $t1, print
beq $t7, $t2, print
beq $t7, $t3, print
beq $t7, $t4, print
b loops
Exercise 7.6 (Scan Assembly Language Code)
print:
li $v0, 4 # System call code for print_str
la $a0, msg # Load address of msg. into $a0
syscall # Print the string
addi $t6, $t6, 1
# <<<<<<<<<<<<<<<<
addi $sp, $sp, -16
sw $t8, 0($sp) # Relative Position
sw $t5, 4($sp) # String pointer
sw $t6, 8($sp) # Num
sw $ra, 12($sp)
jal PrintDecimal # Call PrintDecimal
lw $t8, 0($sp)
lw $t5, 4($sp)
lw $t6, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
# <<<<<<<<<<<<<<<<
b again
ends:
sw $t6, 4($sp)
jr $ra
.data
msg: .asciiz "\n A Vowel was Found at Relative Position: "
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #24
Exercise 7.3 Iterative N Factorial
Write an iterative function to to compute N Factorial “N!”
Calculate the number of clock cycles to compute 10!
$v0 = Mem($sp);
For (a0 = $v0 –1; $a0 > 0 ; $a0 = $a0 – 1)
$v0 = $v0 * $a0; Label Op-Code Dest. S1, S2 Comments
Mem($sp+4) = $v0; Fac:
lw $v0, 0($sp)
Return
addi $a0, $v0, -1
loopf:
blez $a0, return
mult $v0, $a0
mflo $v0
addi $a0, $a0. -1
b loopf
return:
sw $v0, 4($sp)
jr $ra
Exercise 7.3 Recursive N Factorial
Write a recursive function to to compute N Factorial “N!”
Calculate the number of clock cycles to compute 10!
$a0 = Mem($sp);
Mem($sp+4) = $a0 * Fac($a0 - 1);
Return
Exercise 7.3 Recursive N Factorial
FacR:
lw $a0, 0($sp)
bltz $a0, Prob
addi $t1, $a0, -13
bgtz $t1, Prob
addiu $sp, $sp, -16 #<<<<<
sw $ra, 12($sp)
sw $a0, 8($sp)
slti $t0, $a0, 2
beqz $t0, Go
li $v0, 1
b facret
Go:
addi $a0, $a0, -1
sw $a0, 0($sp)
jal FacR # Recursive Call
lw $v0, 4($sp)
lw $ra, 12($sp) facret:
lw $a0, 8($sp) addiu $sp, $sp, 16 #<<<<
mult $v0, $a0 sw $v0, 4($sp)
mflo $v0 jr $ra
Prob:
sw $0, 4($sp)
jr $ra
Exercise 7.4 Recursive Fibonacci
Fib(N, E)
If (N > 1)
E = Fib(N - 1) + Fib( N - 2)
else E = N
#########################
Fibrec:
addi $sp, $sp, -12
sw $a0, 0($sp) # Save Registers
sw $t0, 4($sp)
sw $ra, 8($sp)
lw $a0, 12($sp) # Get N
li $t2, 1
bgt $a0, $t2, deep
sw $a0, 16($sp) # Return N!
addi $sp, $sp 12
jr $ra
Exercise 7.4 Recursive Fibonacci
deep:
addi $a0, $a0, -1
addi $sp, $sp, -8 # <<<< Allocate Space
sw $a0, 0($sp)
jal Fibrec # Recursive Call
lw $t0, 4($sp) # $t0 = Fib (N-1)
addi $a0, $a0, -1
sw $a0, 0($sp)
jal Fibrec # Recursive Call
lw $a0, 4($sp) # $a0 = Fib (N-2)
addi $sp, $sp, 8 # <<<< De-allocate Space
add $a0, $a0, $t0 # Fib (N-1) + Fib (N-2)
sw $a0, 16($sp)
return:
lw $a0, 0($sp) # Restore Registers
lw $t0, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp 12
jr $ra
Exercise 7.5
Write a recursive function to find the determinant of a N x N
matrix (array). The address of the array M and the size N
are passed to the function on the stack, and the result R is
returned on the stack: det (&M, N, R)
5 8 0 2 3
1 6 1 9 5
4 7 8 1 2
2 1 1 2 0
6 8 4 1 1
Recursive Pseudo Code for the Determinant (&M , N , R )
$t9 = Mem(sp+4); # get size N
if ( $t9 < 2) { Mem(sp+8) = 0; return}
If ( $t9 == 2) Mem(sp+8) = Det2
else
{t5 = t9 -1 # Size of cofac matrix
t4 = (t5 * t5) *4 # Dynamic storage alloc.
sp = sp - t4 - 12 # Allocate space for cofac
matrix + arg
Mem(sp + 4) = t5 # Pass size (N-1)
Mem(sp) = sp + 16 # Pass addr. cofac matrix
t3=0 # accumulator
for (t6 = 0; t6 < t9; t6 = t6+1)
{ Put col t6 cofac matrix on stack
push t3,t4,t5,t6,t9 registers on stack
call det(&m, n, r)
restore t3,t4,t5,t6,t9 registers from the stack
t5=Mem(sp+ 8) #result
If (t6 && 1== 0) t3= t3 + t5 else t3= t3 - t5
}
sp = sp + t4 + 12 #clean up stack
return
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #25
Exercise 8.1
Write your most efficient MIPS assembly language code translation
for the following function and main line calling program.
Note, all communication with the function must use a stack frame.
Make use of the fact that multiplication and division by powers of 2
can be performed most efficiently by shifting.
int main()
{int J, K, L , M ;
cin >> J, K, L;
chico ( &J, K, L);
M = J - ( K + L);
cout << M;
return 0
}
Exercise 8.1 (Assembly Language Function)
void chico (int *X, int Y, int Z ) {*X = Y/ 4 - Z * 10 + *X *
8 ;}
Label Op-Code Dest. S1, S2 Comments
chico: lw $t0, 0($sp) # Get &X
lw $t1, 4($sp) # Get value of Y
lw $t2, 8($sp) # Get value of Z
asr $t1, $t1, 2 #Y/ 4
sll $t3, $t2, 1
sll $t2, $t2, 3
add $t2, $t2, $t3 # Z * 10
sub $t1, $t1, $t2 # $t1 gets partial result
lw $t3, 0($t0) # $t3 gets value of X
sll $t3, $t3, 3 #X *8
add $t3, $t1, $t3 # $t3 gets result
sw $t3, 0($t0) # J is assigned the result
jr $ra
Exercise 8.1 Main Program
Label Op-Code Dest. S1, S2 Comments
main:
addiu $sp, $sp, -16 # Allocate for 4 Local Main Var J,K,L,M
<<<<1
li $v0, 5
syscall
sw $v0, 0($sp) # Put value for J : $s0
move $s0, $v0
li $v0, 5
syscall
sw $v0, 4($sp) # Put value for K : $s1
move $s1, $v0
li $v0, 5
syscall
sw $v0, 8($sp) # Put value for L :$s2
move $s2, $v0
addi $sp, $sp, -12 # Allocate for 3 Par. <<<<<<<<<2
addi $s3, $sp, 12 # Calc. address of J <
sw $s3, 0(sp) # Put address of J on stack <
sw $s1, 4($sp) # Put value of K on stack <
sw $s2, 8($sp) # Put value of L on stack <
jal chico <
addiu $sp, $sp, 12 # Deallocate <<<<<<<<<<<<<<<2
Exercise 8.1 Main Program Continued
M = J - ( K + L);
li $v0, 1
syscall # Print M
li $v0, 10
syscall
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #26
Exercise 8.2
MUL32 ( m, n, p, f)
Write a function MUL32( m, n, p, f) that will find the 32-bit product “p”
of two arguments m and n. If the two’s complement representation of
the product cannot be represented with 32 bits, then the error flag “f”
should be set to 1 otherwise the error flag is set to 0.
Pass all arguments on the stack.
Exercise 8.2 (Assembly Language)
Label Op-Code Dest. S1, S2 Comments
Mul32:
lw $t3, 0($sp) # Get m
lw $t4, 4($sp) # Get n
mult $t3, $t4
xor $t5, $t3, $t4 # Get correct sign for prod
mflo $t6 # $t6 = 32 bit product
xor $t7, $t5, $t6 # compare signs
bltz $t7, ovf
mfhi $t8 # $t8 = Upper 32-bits of prod
bgez $t5, pos
addui $t8, $t8, 1 # Check if all upper bits are 1
pos: bnez $t8, ovf
sw $t6, 8($sp) # product
sw $0, 12($sp) # ok
jr $ra
ovf:
li $t8, 1 # overflow
sw $t8, 12($sp) # occurred
jr $ra
Exercise 8.3
Adduovf ( x, y, s)
Write a function Adduovf( x, y, s) that will find the 32-bit sum “s”
of two unsigned arguments “x” and “y”.
$t3 $t2
$t5 $t4
$t1 $t0
x1 Sum x0
Exercise 8.4 (Assembly Language)
Write a reentrant library function anyone could use, called MUL32(m, n, p).
All function parameters will be passed on the stack. This function should provide the
following features:
1. If the product of the two input arguments m and n cannot be represented with 32 bits
(assuming the two’s complement number system), then an exception should be generated.
2. This function should also provide the following optimization features:
(a) Check if m or n is zero, and return zero without taking 32 clock cycles to execute
the mult instruction.
(b) Check if m or n is plus one (1) or minus one (-1), and return the correct result
without taking 32 clock cycles to execute the mult instruction.
(c) Check if m or n is plus two (2) or minus two (-2), and return the correct result
without taking 32 clock cycles to execute the mult instruction.
If the product cannot be represented with 32-bits, then an exception should be generated.
Exercise 8.5 Continued
Provide inline comments and write a paragraph in English to describe all of the
conditions that your code tests for to detect overflow.
(HINT—The XOR instruction can be useful when writing this function.)
With this function you will demonstrate how to write MIPS assembly language code
involving nested function calls. Write a function to perform a vector product.
vectorprod (&X, &Y, &Z, N, status). Vectorprod will call the MUL32 function.
Use the stack to pass arguments. The in parameters are the starting address of three
different word arrays (vectors) : X, Y, Z, and an integer value N specifying the size
of the vectors. Status is an out parameter to indicate if overflow ever occurred while
executing this function. The procedure will perform the vector product:
Write a MIPS assembly language main program that could be used to test the
vectorprod function.
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #27
A Pipelined Implementation of the MIPS Architecture
PC
Data Cache
Rs ALU
IR Result
Address
=
Data
Rd In
Register Rt
File
Instruction
Cache
Write Back
The True Branch Instructions
Branch if Equal: beq Rs, Rt, Label
Branch if Greater Than or Equal to Zero: bgez Rs, Label
Branch if Greater Than or Equal to Zero and Link: bgezalRs, Label
Branch if Greater Than Zero: bgtz Rs, Label
Branch if Less Than or Equal to Zero: blez Rs, Label
Branch if Less Than Zero and Link: bltzal Rs, Label
Branch if Less Than Zero: bltz Rs, Label
Branch if Not Equal: bne Rs, Rt, Label
Exercise 9.1
Taking into consideration delayed branches and delayed loads,
write a MIPS function to search through an array “X” of “N” words
to find how many of the values are evenly divisible by four.
The address of the array will be passed to the function using
register $a0, and the number of words in the array will be passed
in register $a1. Return the results in register $v0.
Original Code - Modified Code
Label Op-Code Dest. S1, S2
Label Op-Code Dest. S1, S2
Div4:
Div4:
li $v0, 0
li $v0, 0
b skip
li $t3, 3
li $t3, 3
b skip
loop:
loop:
nop
lw $t2, 0($a0)
and $t0, $t2, $t3
addi $a0, $a0, 4
bnez $t0, skip
and $t0, $t2, $t3
addi $a0, $a0, 4
bnez $t0, skip
addi $v0, $v0, 1
addi $v0, $v0, 1
skip:
skip:
addi $a1, $a1, -1
addi $a1, $a1, -1
bgez $a1, loop
bgez $a1, loop
lw $t2, 0($a0)
jr $ra
jr $ra
nop
Exercise 9.2
Taking into consideration delayed branches and delayed
loads,
Write a MIPS function to sort an array ‘“ Z ” of ‘“N” words into ascending
order using the bubble sort algorithm.
The address of the array and the value N will be passed to the function
on the stack. Show how the sort function is called.
Original Code to Call Sort(&Z, 1000) Modified Code to Call Sort(&Z, 1000)
addi $sp, $sp, -8 addi $sp, $sp, -8
la $t0, z la $t0, z
sw $t0, 0($sp) sw $t0, 0($sp)
li $t0, 1000 li $t0, 1000
sw $t0, 4($sp) jal sort
jal sort sw $t0, 4($sp)
addi $sp, $sp, 8 addi $sp, $sp, 8
Original Code - Modified Code
BubSort: BubSort:
lw $t0, 4($sp) lw $t0, 4($sp) #N
again: li $t2, 0
addi $t0, $t0, -1 again:
li $t2, 0 addi $t0, $t0, -1
lw $t3, 0($sp) lw $t3, 0($sp) &Z
move $t1, $t0, move $t1, $t0,
loop: loop:
lw $t8, 0($t3) lw $t8, 0($t3)
lw $t9, 4($t3) lw $t9, 4($t3)
ble $t8, $t9, next addi $t1, $t1, -1
sw $t8, 4($t3) ble $t8, $t9, next
sw $t9, 0($t3) nop
li $t2, 1 sw $t8, 4($t3)
next: sw $t9, 0($t3)
addi $t3, $t3, 4 li $t2, 1
next:
addi $t1, $t1, -1 bgtz $t1, loop
bgtz $t1, loop addi $t3, $t3, 4
bnez $t2, again
bnez $t2, again li $t2, 0
jr $ra jr $ra
nop
MIPS Assembly Language
Programming
Bob Britton, Instructor
Lesson #28
Exercise 9.3
Taking into consideration delayed branches and delayed loads,
Write a function Adduovf( x, y, s) that will find the 32-bit sum “s”
of two unsigned arguments “x” and “y”.
li $v0, 4
la $a0, nl
syscall # new line
addi $s6, $s6, -8
bgtz $s6, loop
li $v0, 10
syscall