Compiler, Assembler, and Linker - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Compiler, Assembler, and Linker

Description:

Piecing Together an Executable from Separately Compiled Units ... P&H Excellent Advice: ... by default, auto alignment is on for word, half-word, float and double ... – PowerPoint PPT presentation

Number of Views:738
Avg rating:3.0/5.0
Slides: 20
Provided by: egBuc
Category:

less

Transcript and Presenter's Notes

Title: Compiler, Assembler, and Linker


1
  • Compiler, Assembler, and Linker
  • Please find additional material in
  • /home/accounts/COURSES/cs206/fall05/student/lectur
    e07

2
Compilers are for High-Level Languages
3
A Separately Compiled File
4
Piecing Together an Executable from Separately
Compiled Units
5
Piecing Together Separately Assembled Units
6
Looking into a C Program
square.c
extern double square(double x) double
square(double x) double result
result x x return result
prog.c
  • include ltstdio.hgt
  • extern double square(double x)
  • extern double power(double x,
  • int y)
  • int main(int argc, char argv)
  • double x square(256.0)
  • double y power(256.0, 2)
  • printf("Hello there!\n")
  • printf("The square of 256 is lf\n",
  • x)
  • printf("2562 is lf\n", y)
  • return 0

power.c
extern double power(double x,
int y) double power(double x, int y)
double result return result
7
Generating an Executable from C
prog.c
power.c
square.c
gcc c prog.c o prog.o
gcc c square.c o square.o
compiler
gcc c power.c o power.o
prog.o
power.o
square.o
object files
ld prog.o square.o power.o o prog.o
linker
or
gcc prog.o square.o power.o o prog.o
prog
executable file
8
Looking at Intermediate Steps
prog.c
power.c
square.c
gcc fverbose-asm -S prog.c
compiler
gcc fverbose-asm -S power.c
gcc fverbose-asm -S square.c
prog.s
power.s
square.s
assembly files
as prog.s o prog.o
assembler
as power.s o prog.o
as square.s o prog.o
object files
prog.o
power.o
square.o
9
Peeking into an Assembly File
first.c
Say you have this C program. You can compile it
to assembly on the Sparc/Sun and save the results
to a file first.s. Say that later on, you log
into an Intel x86/Linux box and also compile it
to assembly saving the results to a file
first.x86.s. Look at the two assembly files and
compare what you see.
  • include ltstdio.hgt
  • double square(double x)
  • double result
  • result x x
  • return result
  • int main(int argc, char argv)
  • printf("Hello there!\n")
  • printf("The square of 256 is
  • lf\n", square(256.0))

10
Why Program in Assembly?
  • If what you need is to have mode direct control
    of the machine...
  • PH Excellent Advice
  • Stick to high-level language (if you have a good
    compiler) as much as you possibly can.

11
Memory Image of an Executing Program
12
What happens to libraries?
  • If you generate an executable that uses
    libraries, this is what could happen
  • The entire contents of a library file is linked
    into your program.
  • Only the library functions you actually use are
    linked into the program.
  • The library does not become part of your
    executable. Rather it stays in memory and can be
    accessed by a number of different executables.

13
Dynamically Linked Libraries
DLLs are not linked and loaded until the program
is run. Both the program and library routines
keep extra information on the location of
nonlocal procedures and their names. In the
initial version of DLLs, the loader ran a dynamic
linker, using the extra information in the file
to find the appropriate libraries and to update
all the external references.
(PH p.112)
Question Can we do better than this?
14
Assembler Directives
  • Data alignment .align n (n is a power of 2)
  • - by default, auto alignment is on for word,
    half-word, float and double
  • Data segment definition .data ltaddrgt
  • .data
  • MY_VAR1 .float 3.1416936
  • GREET .asciiz Welcome to my first MIPS
    program
  • DUDE .word 0xdada
  • B_ARRAY .byte 32, 12, 23, 3, 35, 76, 99
  • Code segment definition .text ltaddrgt
  • .text
  • addi t0, zero, 64
  • sub t1, zero, 1
  • START
  • sub t0, t0, t1 bne t0, zero,
    START

15
Assembler Directives
  • Comments this is a comment
  • Address labels
  • addi t0, zero, 64
  • sub t1, zero, 1
  • START
  • sub t0, t0, t1 bne t0, zero, START

16
Assembler Directives
  • Space allocation (in the data segment)
  • MY_EMPTY_ARRAY
  • .space n

Please refer to PH2 page A-51 for the
comprehensive list.
17
Services
If you write code for a bare machine, your
program must be a self-contained unit. All the
functionality you require must be provided by
your own code.
If you write code to run on a machine with an
operating system, you can make use of the
(possibly many) services offered. First, you must
figure out how to invoke these services, what
parameters need to be passed and how to pass
these parameters.
xspim doesnt fall in either of these cases, but
it offers you a few services that can make your
life easier.
18
XSPIM Services
19
xspim Startup
  • lw a0, 0(sp) argc
  • addiu a1, sp, 4 argv
  • addiu a2, a1, 4 envp
  • sll v0, a0, 2
  • addu a2, a2, v0
  • jal main
  • li v0, 10
  • syscall syscall 10 (exit)

Your program should better define a main symbol
because xspim will look for it.
Write a Comment
User Comments (0)
About PowerShow.com