Computer Architecture: Running A Program Khiyam Iftikhar

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Computer Architecture

Running a Program
Khiyam Iftikhar
Steps Involved
Compiler and Assembler
• Compiler converts a higher level language
code to assembly code
• Assemble converts assembly code to machine
code and generates object file which is a
combination of machine language
instructions, data and information to place
properly in memory
Compiler Example: C Code
Compiler Example: Compiled Code
Object File Types
• Relocatable object file. Contains binary code and
data in a form that can be combined with other
relocatable object files at compile time to create an
executable object file.
• Executable object file. Contains binary code and data
in a form that can be copied directly into memory
and executed.
• Shared object file A special type of relocatable object
file that can be loaded into memory and linked
dynamically, at either load time or run time.
Relocatable Object File
• Object file format standard for unix is ELF format. We will only
study basic requirements for an object file
• Object file consists of 6 distinct parts
• Header : Size and position of other parts of file
• Text Segment: Machine Language Code
• Static Data Segment:
• Relocation information: identifies instructions and data words
that depend upon absolute addresses
• Symbol Table: symbols from code and data
• Debugging information: how the code was compiles; relating
HLL code and assembly code
Symbol Table
• It contains the information regarding symbol
which includes
– Name
– Type(Func or Data)
– index (To determine segment)
– Offset (Address inside segment)
– Value
– Size
Assembler
• Assembler makes two passes through the
assembly code
– 1st Pass: Assign Instruction addresses and finds all
the symbols and global variable names and keeps
their addresses in a symbol table.
Assembler:1 Pass Code
st

• For the given example text segment starts at


0x00400000 and data segment starts at
0x10000000.
Symbol Table
Name Index offset Size(bytes) Type value
f 1 0 4 object 0
g 1 4 4 object 0
y 1 8 4 object 0
main 0 0 44 func 0
sum 0 2C 8 func 0
Assembler: 2 Pass nd

• Symbols are replaced by their addresses as


sum of $gp and offset. At reset $gp is
• So for first instruction sw $a0,f=sw $a0,offset($gp),
Variable ‘f’ is at start of data segment i.e. 0x1000000
• Offset +$gp=0x10000000, So, Offset=0x10000000-$gp
=0x10000000-0x10008000
=-0x8000
Assembled Code:2 pass result
nd
Linker
• Compiling complete code after changing few
lines is wastage of resources and time.
• Solution: Place each procedure in different
file. Only the procedure modified, will be
compiled
• This requires an application to combine all the
independently combines programs and
resolve undefined labels
Linker
• Resolves all undefined labels using symbol table.
• These undefined labels occur in branch and jump
instruction, and data addresses (in load and store
instructions) which use labels or variables from other
files.
• Modifies addresses in instructions using absolute
memory references e.g. jal,lw,sw
• Then it determines the memory location each module
will occupy
• It generates an executable object file
Linker
Linking Example: Linking two procedures
Procedure A
Linker Example
Procedure B
Linker Example
Loader
• Creates space large enough for .text and .data of
the executable object file
• Initialize machine register and sets stack pointer
to first free location.
• Jumps to a start up routine which copies the
parameters in to argument registers and calls
“main” of the program
• When the main routine ends, the start up
routine executes an exit system call
Dynamically Linked Libraries(DLL):
Disadvantages of static linking
• Library routines become part of the
executable code and if a new version is
released, statically linked keeps using the
previous (until relinking)
• It links all the libraries including some which
may not be used. Thus file size becomes large.
Dynamic Linking at Load
• List of library name is provided at compilation.
e.g. gcc -o program main2.c ./library.so
• Linker copies some symbol table and
relocation information
• When the program is loaded, Loader uses the
information to copy .text and .data of library
in memory and relocating references in the
program
Dynamic Linking at runtime
• The jump to a library routine, results in jumping
to a dummy routine, at the end of the code e.g.
printf_dummy
• The dummy routine jumps to a label which loads
an integer in a register as parameter and jumps
to the Dynamic Linker/Loader.
• The dynamic linker/loader find the routine,
maps it in memory , replaces the jump in
dummy routine and jumps to the library routine
Fallacy: More Powerful Instruction means
higher performance
• In Intel x86 instruction set, prefixes modify the
execution of the following instruction.
• One prefix can repeat an instruction until a
counter counts to zero.
• Thus to move data in memory move can be used
with REP prefix.
• Alternate method is to use lw and sw repeatedly
(without loop)
• The second method is faster
Write in assembly language to achieve higher
performance
• Today’s compilers are efficient and generate
better code.
The importance of commercial binary compatibility means successful
instruction sets don’t change.

• Intel x86 instruction set growth rate is almost


1 instruction per month, but it provides
backward compatibility
Pitfall: Forgetting that sequential word addresses in machines with
byte addressing
do not diff er by one.

You might also like