Lab 05
Lab 05
Name:
Kevin Bradshaw
Objective
The main objective of this lab is to experiment with linking two source files together using
QtSpim and the gcc compiler.
Pre-requisite
For this lab you are expected to be familiar with relocation entries and compiler operation.
MIPS Linking
1. Type the following program in as a file called lab5-part1.s and load it into QTSPIM.
Do not run it yet.
. text
. align 2
main :
# p r i n t o u t prompt
l i $v0 , 4
l a $a0 , i n s t r i n g
syscall
1
2
3
4
5
6
7
# s y s t e m c a l l code f o r p r i n t i n g s t r i n g = 4
# l o a d a d d r e s s o f s t r i n g t o be p r i n t e d i n t o $a0
# c a l l o p e r a t i n g s y s t e m t o perform p r i n t o p e r a t i o n
# r e a d i n t e g e r i n t o $s0
l i $v0 , 5
# s y s t e m c a l l code f o r r e a d i n t e g e r = 5
syscall
# c a l l o p e r a t i n g system
move $s0 , $v0
# v a l u e r e a d from k e y b o a r d r e t u r n e d i n r e g i s t e r $v0 ; t r a n
9
10
11
12
13
14
15
16
17
18
sw $s0 , ( $sp )
addi $sp , $sp , 4
j a l Fib
addi $sp , $sp , 4
lw $s1 , ( $sp )
# p r i n t o u t prompt
l i $v0 , 4
l a $a0 , o u t s t r i n g
syscall
# s y s t e m c a l l code f o r p r i n t i n g s t r i n g = 4
# l o a d a d d r e s s o f s t r i n g t o be p r i n t e d i n t o $a0
# c a l l o p e r a t i n g system
19
20
21
22
23
24
# p r i n t o u t r e s u l t ( s t o r e d i n $s1 )
l i $v0 , 1
# s y s t e m c a l l code f o r p r i n t i n g i n t e g e r = 1
move $a0 , $ s 1
# move i n t e g e r t o be p r i n t e d i n t o $a0 : $a0 = $s1
syscall
# c a l l o p e r a t i n g s y s t e m t o perform p r i n t
jr $ra
25
26
27
28
29
2. Give symbol table for the program. Which of the symbols are external? Note: __eoth
and __start are not part of the program.
Label
main:
in_string
Fib
out_string
Address
0x00400024
External symbol
External symbol
External symbol
3. Write the relocation table for the program - list all instructions that use absolute
addresses.
Address
0x00400028
0x0040002c
0x00400048
0x00400058
0x0040005c
Instr. Type
lui
ori
jal
lui
ori
Dependency
l.in_string
r.in_string
Fib
l.out_string
r.out_string
1
2
3
Input a p o s i t i v e i n t e g e r : \ n\n
The F i b o n a c c i number i s : \ n\n
. globl i n s t r i n g # i n s t r i n g i s a g l o b a l l a b e l
. globl o u t s t r i n g
. globl Fib
5
6
7
8
. text
. align 2
9
10
11
#####################################################################
# Fibonacci subroutine
12
13
#
# input :
14
i n t e g e r n , on s t a c k
#
# o u t p u t : Fib ( n ) , n t h F i b o n a c c i number
15
#
# d e s c r i p t i o n : r e c u r s i v e l y computes
#
16
17
#
# u s e s : $t0 , $ t 1
18
#
#####################################################################
19
20
21
Fib :
# procedure prologue :
sw $ra , ( $sp )
addi $sp , $sp , 4
sw $fp , ( $sp )
addi $sp , $sp , 4
add $fp , $sp , 1 2
22
23
24
25
26
27
28
# s a v e r e t u r n a d d r e s s on s t a c k , s i n c e r e c u r s i v e ,
#
and decrement s t a c k p o i n t e r
# s a v e p r e v i o u s frame p o i n t e r on s t a c k
#
and decrement s t a c k p o i n t e r
# s e t frame p o i n t e r t o p o i n t a t b a s e o f s t a c k frame
29
lw $t0 , ( $ f p )
l i $t1 , 2
bgt $t0 , $t1 , d o r e c u r s e
l i $t0 , 1
30
31
32
33
n = 2)
# copy argument t o $ t 0 :
$t0 = n
34
b epilogue
# branch t o end
do recurse :
addi $t0 , $t0 , 1
sw $t0 , ( $sp )
addi $sp , $sp , 4
j a l Fib
# l e a v e r e s u l t on s t a c k
lw $t0 , ( $ f p )
addi $t0 , $t0 , 2
sw $t0 , ( $sp )
addi $sp , $sp , 4
j a l Fib
addi $sp , $sp , 4
lw $t0 , ( $sp )
addi $sp , $sp , 4
lw $t1 , ( $sp )
add $t0 , $t0 , $ t 1
# $ t 0 = n1
# push argument n1 on s t a c k
#
and decrement s t a c k p o i n t e r
# c a l l F i b o n a c c i w i t h argument n1
f o r now
# recopy argument t o $ t 0 :
$t0 = n
# $ t 0 = n2
# push argument n2 on s t a c k
#
and decrement s t a c k p o i n t e r
# c a l l F i b o n a c c i w i t h argument n2
# increment s t a c k p o i n t e r
#
and pop r e s u l t o f Fib ( n2) from s t a c k i n t o $ t 0
# increment s t a c k p o i n t e r
#
and pop r e s u l t o f Fib ( n1) from s t a c k i n t o $ t 1
# $ t 0 = Fib ( n2) + Fib ( n 1); have r e s u l t
epilogue :
addi $sp , $sp , 4
lw $fp , ( $sp )
addi $sp , $sp , 4
lw $ra , ( $sp )
addi $sp , $sp , 4
#
t o pop argument ( n )
sw $t0 , ( $sp )
addi $sp , $sp , 4
jr $ra
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
####################
# end o f F i b o n a c c i #
####################
5. Give symbol table for the program Which of the symbols are external?
Label
in_string
out_string
do_recurse
epilogue
Fib
Address
0x10010000 - 0x100100e
0x1001000f - 0x100100b
0x00400044
0x0040004c
0x00400024
6. Write the relocation table for the program - list all instructions that use absolute
addresses.
Address
0x00400088
0x0040005c
0x00400070
Instr. Type
b
jal
jal
Dependency
epilogue
Fib
Fib
7. Load Lab5-part1.s into QTSPIM. Use ReInitialize and Load Program. Then use
Load File to load lab5-part2.s.
8. Describe all changes in the text and data segments after loading both files. Explain
each change in the text and data segments.
The pseudo-instructions are broken down into their true instruction types with actual addresses in
the program. The branch statements all have addresses that are position dependent with the
proper addresses used instead of an arbitrary label.
Address
0x00400024
0x10010000
0x1001000e
0x00400094
0x0040009c
0x00400074
Instr. Type
lui
ori
jal
jal
lui
ori
Dependency
0x1001
0x0000
Fib
Fib
0x1001
0x001e
11. Explain why the implementation given in files Lab5-part1.s and Lab5-part2.s is so
inefficient.
There are two files that supports 4 function calls. In order to increase efficiency and to make it
easier to check for bugs, more text files can be made, separating each bit of code.
The goal of this section is to experiment with linking two source files together.
You will use http://dropzone.tamu.edu/350/ to perform the compilation and linking
steps. Alternately, you may use a stand-alone MIPS cross compiler if you have one available.
Type the following program and save it as Lab5-part3-1.c. The code contains utility
functions to perform IO operations:
char prodMessage = The p r o d u c t i s ;
void p r i n t i n t ( i n t a )
{
asm (
l i $2 , 1\n\ t
syscall
: /No o u t p u t s /
: r(a)
: %v0 ) ;
}
void p r i n t s t r i n g ( char a )
{
asm (
l i $2 , 4\n\ t
syscall
: /No o u t p u t s /
: r(a)
: %v0 ) ;
}
int r e a d i n t ( )
{
r e g i s t e r unsigned long
v 0 asm ( $2 ) ;
asm (
l i $2 , 5\n\ t
syscall
: /No o u t p u t s /
: /No i n p u t s /
: %v0 ) ;
return
v0 ;
The asm blocks are simply a way to write assembly code within C. These functions simply
set up registers for a syscall. If you examine the assembly, you will see that each is only 3
instructions long.
Type the code below and save it as Lab5-part3-2.c. The code below reads in two integers
from the user, multiplies them together, and then prints the product to the screen.
This is the same program that you designed in lab 4. Compile the two files, and examine
their object files. Look at the symbol tables for both files and fill in the following table.
Write UND for undefined symbols, and write N/A for symbols not present in a particular
file. Also include the section (.data, .text, or another section) for each symbol.
Symbol
print int
print string
read int
prodMessage
my mul
main
Address in file 1
Address in file 2
Section
00000000
*UND*
00400570
.text
00000010
*UND*
00400580
.text
00000020
*UND*
00400590
.text
00000000
*UND*
00410800
.data
N/A
00000000
004005a0
.text
N/A
00000018
004005b8
.text
PIC Code
1. Open funca code (from the prelab) in QTSPIM. Modify the code by adding few nop
instruction in the beginning of the program. List places in code where the native
instructions have changed and explain why the change occurred.
The place where it changed was directly after the nop instruction because this is basically an
instruction used as a time delay so that there is extra time for a value to be put into a register
before the next instruction is executed.
2. Take the code you modified in the prelab for funca to be position independent. Save
your program as lab5-part4.s, and open it to make sure it is position independent in
QtSpim. Try adding instructions before your code (such as nop) and make sure that
program does not change.
Deliverables