1 Objective
The main objective of this lab is to experiment with linking two source files together using
MARS and the gcc compiler.
2 Pre-requisite
For this lab you are expected to be familiar with relocation entries and compiler operation.
1
Computer Architecture and Design, Lab 5 2
3 MIPS Linking
4 Linking files using MARS
1. Type the following program in as a file called lab5-part1.s and load it into MARS.
Do not run it yet.
1 . data
2 . a l i g n 2
3 . text
4 main :
5 # p r i n t ou t prompt
6 l i $v0 , 4 # sys tem c a l l code f o r p r i n t i n g s t r i n g = 4
7 la $a0 , i n s t r i n g # 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
8 s y s c a l l # c a l l o p e r a t i n g sys tem t o per form p r i n t o p e r a t i o n
9
10 # read i n t e g e r i n t o $s0
11 l i $v0 , 5 # sys tem c a l l code f o r read i n t e g e r = 5
12 s y s c a l l # c a l l o p e r a t i n g sys tem
13 move $s0 , $v0 # v a l u e read from keyb o a r d r e t u r n e d in r e g i s t e r $v0
14 # t r a n s f e r t o $s0
15
16 sw $s0 , ( $sp ) # push argument f o r F ib on s t a c k
17 addi $sp , $sp ,4 # and decremen t s t a c k p o i n t e r
18 j a l Fib # jump t o s u b r o u t i n e
19 addi $sp , $sp , 4 # incremen t s t a c k p o i n t e r
20 lw $s1 , ( $sp ) # and pop r e s u l t from s t a c k
21
22 # p r i n t ou t prompt
23 l i $v0 , 4 # sys tem c a l l code f o r p r i n t i n g s t r i n g = 4
24 la $a0 , o u t s t r i n g # 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
25 s y s c a l l # c a l l o p e r a t i n g sys tem
26
27 # p r i n t ou t r e s u l t ( s t o r e d in $s1 )
28 l i $v0 , 1 # sys tem c a l l code f o r p r i n t i n g i n t e g e r = 1
29 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
30 s y s c a l l # c a l l o p e r a t i n g sys tem t o per form p r i n t
31 j r $ r a
2. Provide the expected symbol table for the program. Which of the symbols are external?
Computer Architecture and Design, Lab 5 3
3. Write the relocation table for the program. Hint: list all instructions that use absolute
addresses.
4. Now, type in the following new program and save it as lab5-part2.s.
1 . data
2 i n s t r i n g : . a s c i i z Inpu t a p o s i t i v e i n t e g e r : n
3 o u t s t r i n g : . a s c i i z The Fi b o n a c ci number i s : n
4 . g lob l 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
5 . g lob l o u t s t r i n g
6 . g lob l Fib
7 . a l i g n 2
8 . text
9 #####################################################################
10 # F i b o n a c c i s u b r o u t i n e
11 # i n p u t : i n t e g e r n , on s t a c k
12 # o u t p u t : F ib ( n ) , n th F i b o n a c c i number
13 # d e s c r i p t i o n : r e c u r s i v e l y compu tes F ib ( n ) = F ib (n1) + F ib (n2) ,
14 # F ib ( 1 ) = F ib ( 2 ) = 1 .
15 #
16 # u s e s : $t0 , $t 1
17 #####################################################################
18 Fib :
19 # p r oce d u re p r ol o g u e :
20 sw $ra , ( $sp ) # s ave 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 ,
21 addi $sp , $sp ,4 # and decremen t s t a c k p o i n t e r
22 sw $ fp , ( $sp ) # s ave p r e v i o u s frame p o i n t e r on s t a c k
23 addi $sp , $sp ,4 # and decremen t s t a c k p o i n t e r
24 add $ fp , $sp , 1 2 # s e t frame p o i n t e r t o p o i n t a t b a se o f s t a c k frame
25 lw $t0 , ( $ fp ) # copy argument t o $t 0 : $t 0 = n
26 l i $t1 , 2
27 bgt $t0 , $t1 , d o r e c u r s e # i f argument n >= 2 , branch t o r e c u r s i v e seq uence
28 l i $t0 , 1 # e l s e s e t r e s u l t t o 1
29 #( b a se c a s e s n = 1 and n = 2)
30 b e pil o g u e # branch t o end
31 d o r e c u r s e :
32 addi $t0 , $t0 ,1 # $t 0 = n1
33 sw $t0 , ( $sp ) # push argument n1 on s t a c k
34 addi $sp , $sp ,4 # and decremen t s t a c k p o i n t e r
35 j a l Fib # c a l l F i b o n a c c i w i t h argument n1
36 # l e a v e r e s u l t on s t a c k f o r now
37 lw $t0 , ( $ fp ) # recopy argument t o $t 0 : $t 0 = n
38 addi $t0 , $t0 ,2 # $t 0 = n2
39 sw $t0 , ( $sp ) # push argument n2 on s t a c k
Computer Architecture and Design, Lab 5 4
40 addi $sp , $sp ,4 # and decremen t s t a c k p o i n t e r
41 j a l Fib # c a l l F i b o n a c c i w i t h argument n2
42 addi $sp , $sp , 4 # incremen t s t a c k p o i n t e r
43 lw $t0 , ( $sp ) # and pop r e s u l t o f F ib (n2) from s t a c k i n t o $t 0
44 addi $sp , $sp , 4 # incremen t s t a c k p o i n t e r
45 lw $t1 , ( $sp ) # and pop r e s u l t o f F ib (n1) from s t a c k i n t o $t 1
46 add $t0 , $t0 , $ t 1 # $t 0 = F ib (n2) + F ib (n1) ; have r e s u l t
47 e pil o g u e : # p r oce d u re e p i l o g u e : $t 0 h o l d s r e s u l t
48 addi $sp , $sp , 4 # incremen t s t a c k p o i n t e r
49 lw $ fp , ( $sp ) # and pop s ave d frame p o i n t e r i n t o $f p
50 addi $sp , $sp , 4 # incremen t s t a c k p o i n t e r
51 lw $ra , ( $sp ) # and pop r e t u r n a d d r e s s i n t o $ra
52 addi $sp , $sp , 4 # incremen t s t a c k p o i n t e r
53 # t o pop argument ( n ) from s t a c k ( d i s c a r d )
54 sw $t0 , ( $sp ) # push r e s u l t on to s t a c k
55 addi $sp , $sp ,4 # and decremen t s t a c k p o i n t e r
56 j r $ r a # r e t u r n t o c a l l e r
57 ####################
58 # end o f F i b o n a c c i #
59 ####################
5. Provide the expected symbol table for the program. Which of the symbols are external?
6. Write the relocation table for the program. Hint: list all instructions that use absolute
addresses.
7. Save Lab5-part1.s and lab5-part2.s into the same folder and make sure the option
Assemble all files in directory is checked in Settings.
Computer Architecture and Design, Lab 5 5
8. Describe all changes in the text and data segments after loading both files.
9. Give symbol table for the combined program. Make sure the option Show Label
Window is checked in Settings.
10. Write the relocation table for combined program.
11. Explain why the implementation given in files Lab5-part1.s and Lab5-part2.s is so
inefficient.
Computer Architecture and Design, Lab 5 6
5 Experiments with a real gcc compiler
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 oduc t i s ;
void p r i n t i n t ( int a )
{
asm ( l i $2 , 1
t
s y s c a l l
: /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
t
s y s c a l l
: /No o u t p u t s /
: r ( a )
: %v0 ) ;
}
int r e a d i n t ( )
{
r eg ist er unsigned long v 0 asm ( $2 ) ;
asm ( l i $2 , 5
t
s y s c a l l
: /No o u t p u t s /
: /No i n p u t s /
: %v0 ) ;
return v 0 ;
}
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.
Computer Architecture and Design, Lab 5 7
extern char prodMessage ;
void p r i n t i n t ( int a ) ;
void p r i n t s t r i n g ( char a ) ;
int r e a d i n t ( ) ;
int my mul ( int a , int b )
{
int i , r e t = 0 ;
for ( i =0; i r e t = r e t + a ;
return r e t ;
}
int main ( void)
{
p r i n t s t r i n g ( Enter the f i r s t number ) ;
int num1 = r e a d i n t ( ) ;
p r i n t s t r i n g ( Enter the sec ond number ) ;
int num2 = r e a d i n t ( ) ;
p r i n t s t r i n g ( prodMessage ) ;
p r i n t i n t (my mul (num1 , num2 ) ) ;
return 0 ;
}
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 Address in file 1 Address in file 2 Address in linked file Section
print int
print string
read int
prodMessage
my mul
main
Computer Architecture and Design, Lab 5 8
6 PIC Code
1. Open funca code (from the prelab) in MARS. 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.
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
MARS. Try adding instructions before your code (such as nop) and make sure that
program does not change.
7 Deliverables
Submit the following:
A completed copy of this lab.
All source code files created in this lab (with comments).
151/251A, 5, :, EECS, Lab, Linking, solved
[SOLVED] EECS 151/251A Lab 5 : Linking
$25
File Name: EECS_151/251A_Lab_5_:_Linking.zip
File Size: 273.18 KB
Only logged in customers who have purchased this product may leave a review.
Reviews
There are no reviews yet.