You are on page 1of 8

Lab 5 : Linking

Name:

Sign the following statement: On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work

1

Objective

The main objective of this lab is to experiment with linking two source files together by using spim and a gcc compiler.

2

Pre-requisite

For this lab you are expected to be familiar with relocation entries and compiler operation.

1

$sp .0x00000000 YES YES YES YES .0x00000000 --. Type the following program and load it into QTSPIM. 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 . Don’t Run it. $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 . $sp . ( $sp ) addi $sp . i n 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 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 . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 .0x00000000 --. 4 l a $a0 . align 2 main : # p r i n t o u t prompt l i $v0 . 4 lw $s1 . Give symbol table for the program. o u t s t r i n g syscall # push argument f o r Fib on s t a c k # and decrement s t a c k p o i n t e r # jump t o s u b r o u t i n e # increment s t a c k p o i n t e r # and pop r e s u l t from s t a c k # 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 # 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 . $ 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 2. − 4 j a l Fib addi $sp .0x00400024 --. main in_string out_string Fib --.Computer Architecture and Design. Save it as lab5-part1. 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 . 4 l a $a0 .s. Which of the symbols are external? Note: __eoth and __start are not part of the program. t r a n s f e r sw $s0 . Lab 5 2 3 4 MIPS Linking Linking files using SPIM 1. text . ( $sp ) # p r i n t o u t prompt l i $v0 .

out_string 4. $ t 1 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Fib : # procedure prologue : sw $ra . # # Fib ( 1 ) = Fib ( 2 ) = 1 . ( $sp ) # s a v e r e t u r n a d d r e s s on s t a c k . 1 n = 2) b epilogue do recurse : addi $t0 . $sp . − 4 sw $fp .s. 1 2 lw $t0 . $sp . globl o u t s t r i n g . 2 bgt $t0 . on s t a c k # # o u t p u t : Fib ( n ) . asciiz ” 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” . s i n c e r e c u r s i v e . in_string jal Fib la $a0. − 1 sw $t0 . branch t o r e c u r s i v e s e q u e n c e # e l s e s e t r e s u l t t o 1 ( b a s e c a s e s n = 1 and # branch t o end 34 35 36 37 38 # $ t 0 = n−1 # push argument n−1 on s t a c k . 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 .Computer Architecture and Design. globl Fib . $t1 . align 2 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Fibonacci subroutine # # input : i n t e g e r n . Now. la $a0. # 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 # copy argument t o $ t 0 : $t0 = n # i f argument n >= 2 . ( $sp ) addi $sp . ( $sp ) addi $sp . Lab 5 3 3.list all instructions that use absolute addresses. ( $ f p ) l i $t1 . # # u s e s : $ t0 . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 . Write the relocation table for the program . data in string : . − 4 add $fp . n t h F i b o n a c c i number # # d e s c r i p t i o n : r e c u r s i v e l y computes Fib ( n ) = Fib ( n −1) + Fib ( n − 2) . $sp . d o r e c u r s e l i $t0 . text . type the following new program and save it as lab5-part2. asciiz out string : . $t0 .

( $sp ) addi $sp . Lab 5 4 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 addi $sp . 4 sw $t0 . − 4 j a l Fib addi $sp . ( $sp ) addi $sp . $sp . jal Fib -------. − 2 sw $t0 . − 4 j a l Fib lw $t0 . $sp . $t0 . ( $ f p ) addi $t0 .0x00000000 g Fib ------. 4 lw $fp . $sp . Write the relocation table for the program .Computer Architecture and Design. $sp . ( $sp ) addi $sp .0x0040005c jal Fib -------. 4 lw $t0 . ( $sp ) addi $sp . $t0 . 4 lw $ra . 4 lw $t1 .0x00400024 6. $sp .0x00400070 . − 4 jr $ra # # # # # # # # # # # # # # # # # # # # # end o f F i b o n a c c i # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 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 n−1 l e a v e r e s u l t on s t a c k f o r now re −copy argument t o $ t 0 : $t0 = n $ t 0 = n−2 push argument n−2 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 n−2 increment s t a c k p o i n t e r and pop r e s u l t o f Fib ( n −2) 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 ( n −1) from s t a c k i n t o $ t 1 $ t 0 = Fib ( n −2) + Fib ( n − 1). $sp . ( $sp ) add $t0 . have r e s u l t procedure e p i l og u e : $t0 holds r e s u l t increment s t a c k p o i n t e r and pop s a v e d frame p o i n t e r i n t o $ f p increment s t a c k p o i n t e r and pop r e t u r n a d d r e s s i n t o $ ra increment s t a c k p o i n t e r t o pop argument ( n ) from s t a c k ( d i s c a r d ) push r e s u l t onto s t a c k and decrement s t a c k p o i n t e r return to c a l l e r 5. $ t 1 epilogue : addi $sp . $sp .list all instructions that use absolute addresses. $sp . ( $sp ) addi $sp . Give symbol table for the program Which of the symbols are external? g main -----.

Load Lab5-part1. and for higher numbers this becomes unreasonable . Explain why the implementation given in files Lab5-part1. $1.text . Give symbol table for the combined program. Lab 5 5 7. Describe all changes in the text and data segments after loading both files.text .text . Explain each change in the text and data segments.0x00400024 g Fib ---------.data .data . 0 lui $1. 4097 ori $4.text .s into QTSPIM. Then use ”Load File” to load lab5-part2. 28 jal Fib ------------------------------------0x0040005c 0x00400028 0x0040002c 0x00400058 0x0040005c 0x00400074 11.Computer Architecture and Design. $1. Write the relocation table for combined program.s is so inefficient. jal Fib lui $1.s.0x00400074 10. Use ”ReInitialize and Load Program”. SEGMENT . 4097 ori $4. g main --------.text ADDRESS 0x1001001d 0x1001002c 0x004000ac 0x004000c0 0x00400048 0x00400028 0x00400058 CHANGE add in in_string add in out_string relocate the address of jal Fib relocate the address of jal Fib relocate the address of jal Fib relocate the address of in_string relocate the address of out_string 9.s and Lab5-part2. 8. it must call the function several times for each argument to get the value.

multiplies them together. return } The asm blocks are simply a way to write assembly code within C. v0 . and then prints the product to the screen.edu/~gbooker/mipslink. .c. If you examine the assembly. ” l i $2 . void p r i n t i n t ( i n t a ) { asm ( ” l i $2 .c. you will see that each is only 3 instructions long. } void p r i n t s t r i n g ( char ∗ a ) { asm ( ” l i $2 . These functions simply set up registers for a syscall. } int r e a d i n t ( ) { r e g i s t e r unsigned long asm ( v 0 asm ( ” $2 ” ) . You may use http://cegroup. Lab 5 6 5 Experiments with a real gcc compiler The goal of this section is to experiment with linking two source files together.ece.tamu. 4 \ n \ t ” ”syscall” : / ∗ No o u t p u t s ∗ / : ”r”(a) : ”%v0 ” ) . Type the code below and save it as Lab5-part3-2. 5 \ n \ t ” ”syscall” : / ∗ No o u t p u t s ∗ / : / ∗ No i n p u t s ∗ / : ”%v0 ” ) .Computer Architecture and Design. 1 \ n \ t ” ”syscall” : / ∗ No o u t p u t s ∗ / : ”r”(a) : ”%v0 ” ) . The code contains utility functions to perform IO operations: char ∗ prodMessage = ”The p r o d u c t i s ” . Type the following program and save it as Lab5-part3-1.php to perform the compilation and linking steps for you if you like. The code below reads in two integers from the user.

int r e a d i n t ( ) . Look at the symbol tables for both files and fill in the following table. return r e t . } This is the same program that you designed in lab 4. i ++) ret = ret + a .text. i n t num1 = r e a d i n t ( ) . i <b . Also include the section (. and examine their object files. p r i n t s t r i n g ( prodMessage ) . i n t b ) { int i . Write ‘UND’ for undefined symbols. Compile the two files. i n t my mul ( i n t a . Lab 5 7 extern char ∗ prodMessage . } i n t main ( void ) { p r i n t s t r i n g ( ” Enter t h e f i r s t number” ) . and write ‘N/A’ for symbols not present in a particular file. p r i n t i n t ( my mul ( num1 . .data. r e t = 0 . f o r ( i =0. void p r i n t s t r i n g ( char ∗ a ) .Computer Architecture and Design. void p r i n t i n t ( i n t a ) . return 0 . 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 Address in linked file Section . i n t num2 = r e a d i n t ( ) . p r i n t s t r i n g ( ” Enter t h e s e c o n d number” ) . num2 ) ) .

Try adding instructions before your code (such as nop) and make sure that program does not change. and open it to make sure it is position independent in QTSPIM.s.Computer Architecture and Design. . Save your program as lab5-part4. Open funca code (from the prelab) in QTSPIM. Modify the code by adding few nop instruction in the beginning of the program. Take the code you modified in the prelab for funca to be position independent. 2. Lab 5 8 6 PIC Code 1. List places in code where the native instructions have changed and explain why the change occurred.