0% found this document useful (0 votes)
5 views50 pages

Pilation

The document outlines the process of translating and starting a program using the RISC-V Instruction Set Architecture (ISA) and the gcc toolchain. It details the steps involved in compiling a C program, including preprocessing, compiling, assembling, and linking, along with memory layout and object file formats. Additionally, it discusses assembler actions, labels, and handling unresolved references during the linking process.

Uploaded by

findingaish00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views50 pages

Pilation

The document outlines the process of translating and starting a program using the RISC-V Instruction Set Architecture (ISA) and the gcc toolchain. It details the steps involved in compiling a C program, including preprocessing, compiling, assembling, and linking, along with memory layout and object file formats. Additionally, it discusses assembler actions, labels, and handling unresolved references during the linking process.

Uploaded by

findingaish00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

M2 – Instruction Set Architecture

Translating and Starting a Program


Linkage Editor
Module Outline
● Addressing modes. Instruction classes.
● RISC-V ISA.
● Translating and starting a program.
● High level languages, Assembly languages
and object code.
● Subroutine and subroutine call. Use of stack
for handling subroutine call and return.
Steps in gcc
hello.c
Steps in gcc
hello.c

cpp
Steps in gcc
hello.c hello.i

cpp
Steps in gcc
hello.c hello.i

cpp cc1
Steps in gcc
hello.c hello.i hello.s

cpp cc1
Steps in gcc
hello.c hello.i hello.s

cpp cc1

as
Steps in gcc
hello.c hello.i hello.s

cpp cc1

as

hello.o
Steps in gcc
hello.c hello.i hello.s

cpp cc1

Libraries as
(eg. Math)

hello.o

ld
Steps in gcc
hello.c hello.i hello.s

cpp cc1

Libraries as
(eg. Math)

a.out hello.o

ld
Steps in gcc

Step Output FileType Remarks


C Preprocessor hello.i C source text #include, #define, ...
C Compiler hello.s Assembler source text Individual modules. Labels.
Assembler hello.o Object code Undefined global references.
Linkage Editor a.out Executable Global references resolved
Memory Layout of a
Program in Execution

DATA
DATA

a b c

INSTRUCTIONS
INSTRUCTIONS

i1i1 i2i2 i3i3


Memory Layout of a
Program in Execution
Program to Memory Layout

Libraries
(eg. Math)
hello.s

as a.out

ld
hello.o
Program to Memory Layout
Object File Format (hello.o)
The Assembler
The Assembler
The Assembler

Encodes
Encodeseasily!
easily!
The Assembler

Encodes
Encodeseasily!
easily!
The Assembler
The Assembler
Assembler Directives

RO
ROdata
data(constant)
(constant)begins
beginshere
here
Align at doubleword boundary
Align at doubleword boundary
The Assembler
Assembler Directives

RO
ROdata
data(constant)
(constant)begins
beginshere
here
Align at doubleword boundary
Align at doubleword boundary

Create
Createaastring.
string.Others
Othersare
are.byte,
.byte,
.half,
.half,.short,
.short,.word,
.word,.dword
.dword
The Assembler
Assembler Directives

RO
ROdata
data(constant)
(constant)begins
beginshere
here
Align at doubleword boundary
Align at doubleword boundary

Create
Createaastring.
string.Others
Othersare
are.byte,
.byte,
.half,
.half,.short,
.short,.word,
.word,.dword
.dword

Text
Text(code)
(code)section
sectionbegins
beginshere
here
Align
Alignatatbyte
byteboundary
boundary
main
mainisisaaglobal
global“label”.
“label”.Can
Can
be
bereferred
referredfrom
fromother
othermodules.
modules.
The Assembler
The Assembler

Labels
Labels
The Assembler

Labels
Labels
Assembler Actions

● How to encode?
● What information is required?
Assembler Actions

● How to encode?
● What information is required?
– Where to branch? Where is “.L2”?
– Where is “.LC0”?
Assembler Actions
● Labels – main, .LC0, .L2, .L3,
printf are names for memory locations.
Assembler Actions
● Labels – main, .LC0, .L2, .L3,
printf are names for memory locations.
– main: first instruction in the text segment
– .L3: 8th instruction in the text segment
– .LC0: starts from the first byte in the data
segment (.rodata)
– printf: undefined
Assembler Actions
● Labels – main, .LC0, .L2, .L3,
printf are names for memory locations.
– main: first instruction in the text segment
– .L3: 8th instruction in the text segment
– .LC0: starts from the first byte in the data
segment (.rodata)
main
main 00
– printf: undefined .L3
.L3 28
28
.L2
.L2 56
56
.LC0
.LC0 0x8000
0x80000000
0000
printf
printf ??
??
Assembler Actions
● Labels – main, .LC0, .L2, .L3, printf
are names for memory locations.
– main: first instruction in the text segment
– .L3: 8th instruction in the text segment
– .LC0: starts from the first byte in the data segment
(.rodata)
main
main 00
– printf: undefined .L3
.L3 28
28
.L2 56
● Symbol Table .L2 56
0x8000
.LC0
.LC0 0x80000000
0000
printf
printf ??
??
The Assembler

Library
LibraryFunction
FunctionCall
Call
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
● Will exist in the symbol table entries of those
obj files
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
● Will exist in the symbol table entries of those
obj files
● Make a list of unresolved references in the
present object file
– Relocation information
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
● Will exist in the symbol table entries of those
obj files
● Make a list of unresolved references in the
present object file
– Relocation information
64
64 call
call printf
printf ??
??
Object File Format (hello.o)
Object File Format (hello.o)
800
800 Machine
Machinecodecodesize
size
28
28 Initialized
Initializeddata
datasize
size
00 Size
Sizeof
ofuninitialized
uninitializeddata
datasize
size
36
36 Size
Sizeof
ofthe
theSymbol
SymbolTable
Table
16
16 Size
Sizeof
ofthe
theRelocation
RelocationInfo
Info
Object File Format (hello.o)
800
800 Machine
Machinecodecodesize
size
28
28 Initialized
Initializeddata
datasize
size
00 Size
Sizeof
ofuninitialized
uninitializeddata
datasize
size
36
36 Size
Sizeof
ofthe
theSymbol
SymbolTable
Table
16
16 Size
Sizeof
ofthe
theRelocation
RelocationInfo
Info
Object File Format (hello.o)
Object File Format (hello.o)

main
main 00
.L3
.L3 36
36
.L2
.L2 68
68
.LC0
.LC0 800
800
printf
printf ??
??
Object File Format (hello.o)

main
main 00
.L3
.L3 36
36
.L2
.L2 68
68
.LC0
.LC0 800
800
printf
printf ??
??

64
64 call
call printf
printf ??
??
Assembler
Assembler
● Pass 1: Finds instructions with labels. Records
their memory locations and labels so that the
relationship between symbolic names and
addresses is known when instructions are
translated.
Assembler
● Pass 1: Finds instructions with labels. Records
their memory locations and labels so that the
relationship between symbolic names and
addresses is known when instructions are
translated.
● Pass 2: Translate each assembly statement by
combining the numeric equivalents of
opcodes, register specifiers, and labels into a
legal instruction.

You might also like