labreport5 - Lab 5 (Sections 500 and 501) : Linking Name:...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lab 5 (Sections 500 and 501) : Linking Name: Nithej Pilli Sign the following statement: On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work Nithej Pilli 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 Computer Architecture and Design, Lab 5 2 3 4 MIPS Linking Linking files using SPIM 1. First, load the file Lab5-fib1.s into SPIM. Choose “Yes” in the dialog box “Clear program and reinitialize simulator before loading”. 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 . 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 # 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 s f e r 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 # 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 , 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 2. Give symbol table for Lab5-fib1.s. Which of the symbols are external? Note: __eoth and __start are not part of Lab5-fib1.s 0x00000000 [out_string] #external 0x00000000 [in_string] #external 0x00400024 [main] 0x00000000 [Fib] #external Computer Architecture and Design, Lab 5 3 3. Write the relocation table for Lab5-fib1.s - list all instructions that use absolute addresses. 0x00000000 [out_string] #external 0x00000000 [in_string] #external 0x00400024 [main] 0x00000000 [Fib] #external 4. Now, load the second file Lab5-fib2.s into SPIM. Choose “Yes” in the dialog box “Clear program and reinitialize simulator before loading”. 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 . data . asciiz in string : out string : . 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” . 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 . text . align 2 ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ################################## # Fibonacci subroutine # # input : 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 # # 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) , # # Fib ( 1 ) = Fib ( 2 ) = 1 . # # u s e s : $ t0 , $ t 1 # ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ################################## Fib : # procedure prologue : sw $ra , ( $sp ) addi $sp , $sp , − 4 sw $fp , ( $sp ) addi $sp , $sp , − 4 add $fp , $sp , 1 2 lw $t0 , ( $ f p ) l i $t1 , 2 bgt $t0 , $t1 , d o r e c u r s e l i $t0 , 1 n = 2) b epilogue do recurse : addi $t0 , $t0 , − 1 # 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 # copy argument t o $ t 0 : $t0 = n # i f argument n >= 2 , 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 # $ t 0 = n−1 Computer Architecture and Design, Lab 5 4 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 sw $t0 , ( $sp ) addi $sp , $sp , − 4 j a l Fib 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 epilogue : addi $sp , $sp , 4 lw $fp , ( $sp ) addi $sp , $sp , 4 lw $ra , ( $sp ) addi $sp , $sp , 4 sw $t0 , ( $sp ) addi $sp , $sp , − 4 jr $ra ## # # # # # # # # ########## # end o f F i b o n a c c i # ## # # # # # # # # ########## # # # # # # # # # # # # # # # # # # # # # # # # push argument n−1 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−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); have r e s u l t procedure e p il 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. Give symbol table for Lab5-fib2.s. Which of the symbols are external? 0x00000000 [main] #external 0x00400024 [Fib] 6. Write the relocation table for Lab5-fib2.s - list all instructions that use absolute addresses. jal Fib 0x0040005c jal Fib 0x00400070 Computer Architecture and Design, Lab 5 5 7. Load the first file Lab5-fib1.s again into SPIM. Choose “Yes” in the dialog box “Clear program and reinitialize simulator before loading”. Then, load the second file Lab5-fib2.s into SPIM, this time choosing “No” in the dialog box “Clear program and reinitialize simulator before loading”. 8. Describe all changes in the text and data segments after loading file Lab5-fib2.s. Explain each change in the text and data segments. Text sement: The source code from both the files was loaded resulting in the addresses being adjusted accordingly. Data segment: All the values from the second file were loaded. 9. Give symbol table for the combined program. 0x00400024 [main] 0x00400074 [Fib] 10. Write the relocation table for combined program. lui in string 0x00400028 ori in string 0x0040002c jal Fib 0x00400048 lui out string 0x00400058 ori out string 0x0040005c jal Fib 0x004000ac jal Fib 0x004000c0 11. Explain why the implementation given in files Lab5-fib1.s and Lab5-fib2.s is so inefficient. In this instance the fibonacci numbers were calculated using the recursion approach which happens to be the most cumbersome and time-consuming. 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 may use http://cegroup.ece.tamu.edu/∼gbooker/mipslink.php to perform the compilation and linking steps for you if you like. The first file, Lab5-source1.c, 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 asm ( v 0 asm ( ” $2 ” ) ; ” l i $2 , 5 \ n \ t ” ”syscall” : / ∗ No o u t p u t s ∗ / : / ∗ No i n p u t s ∗ / : ”%v0 ” ) ; v0 ; return } 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. The second file, Lab5-source2.c, 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 ( i n t a ) ; void p r i n t s t r i n g ( char ∗ a ) ; int r e a d i n t ( ) ; i n t my mul ( i n t a , i n t b ) { int i , r e t = 0 ; f o r ( i =0; i <b ; i ++) ret = ret + a ; return r e t ; } 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” ) ; i n t num1 = 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” ) ; i n t 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 print int print string read int prodMessage my mul main Address in file 1 0x00000000 Address in file 2 UND Address in linked file 0x00400810 .text Section .text 0x00000010 UND 0x00400820 0x00000020 0x00000000 UND UND 0x00400830 0x00440bd0 .text .data N/A N/A 0x00000000 0x0000001c 0x00400840 0x0040085c .text .text Computer Architecture and Design, Lab 5 8 6 PIC Code 1. Open funca code (from the prelab) in SPIM. 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. Since the following had absolute adresses and the "nop" shifts them down resulting in the changes. 2. Take the code you modified in the prelab for funca to be position independent. Save your program as lab5-2.s, and open it to make sure it is position independent in SPIM. Try adding instructions before your code (such as nop) and make sure that program does not change. ...
View Full Document

This note was uploaded on 03/08/2010 for the course ECEN 350 taught by Professor Sprintston during the Spring '09 term at Texas A&M.

Ask a homework question - tutors are online