Computer Architecture
Computer Architecture
2022-03-28 3
Dr. Rachel Jiang
2022-03-28 4
Dr. Rachel Jiang
Topics:
The compilation process
More on Introduction to assembly
language
Pseudoinstructions
The assembly process
Linking and loading
2022-03-28 5
Dr. Rachel Jiang
The Compilation Process
Translator
Program that converts a user’s
program written in a source language
into another target language -ASL.
2022-03-28 6
Dr. Rachel Jiang
Example
int main() {
int x=3,y=4,z;
z = x+y;
return 0;
}
A = B + 4;
2022-03-28 8
Dr. Rachel Jiang
compile A = B + 4;
Steps involved in compile into assembly code:
lexical analysis
syntactic analysis
semantic analysis
Name analysis
Type analysis
Action mapping & code generation
2022-03-28 9
Dr. Rachel Jiang
Lexical analysis
A = B + 4;
Reducing the program text to the basic
symbols of the language
identifiers such as A and B
denotations the constant value 4, and
program delimiters such as = and +
2022-03-28 12
Dr. Rachel Jiang
Action mapping & code generation
Associating program statements with their
appropriate assembly language sequence
A = B + 4;
;EMU8086version 2
;EMU8086version1
LEA SI,B
MOV AL,B
MOV AL,[SI]
ADD AL, 5
ADD AL, 5
MOV A, AL
LEA DI, A
MOV [DI], AL
2022-03-28 13
Dr. Rachel Jiang
Action mapping & code generation
2022-03-28 14
Dr. Rachel Jiang
EMU8086 Example
org 100h
int main() { MOV AL, X
int x=3,y=4,z; ADD AL, Y
z = x+y; MOV Z, AL
Machine code
ret
X db 3
return 0; Y db 4
} Z db ?
return 0;
}
2022-03-28 17
Dr. Rachel Jiang
Introduction to AL
a symbolic representation for a binary coded
machine language
consists of mnemonics for each of the
machine language instructions
allows symbolic names to refer to the
memory locations
assembler later translate into actual numerical
memory addresses.
2022-03-28 18
Dr. Rachel Jiang
2022-03-28 19
Dr. Rachel Jiang
Creating a Symbol Table
2022-03-28 20
Dr. Rachel Jiang
3/28/2022 R Jiang, June 30th, 2011 21
2022-03-28 21
Dr. Rachel Jiang
AL programmer
can access all features and
instructions available on the target
machine
AL is architecture specific
can run only on one family of machines
AL program
usually much smaller and faster
than the program written in HLL
2022-03-28 22
Dr. Rachel Jiang
!Add example in
ARCTools
!This program adds two
numbers
.begin
.org 2048
prog1: ld [x],%r1
ld [y], %r2
addcc %r1,%r2,%r3
st %r3, [z]
jmpl %r15+4, %r0
x: 15
y: 9
z: 0
.end
2022-03-28 23
Dr. Rachel Jiang
Format of an AL Statement
Assembly language statements have four
parts: org 100h
mov al, a
A label field cmp al,b
jg bigger
An operation (opcode) field mov bl,b ;..
mov c,bl
Operands field, and mov c,b
jmp finish
Comments field bigger: mov c,al
Examples in next slide: finish: ret
a db 5
1. Pentium 4 AL (MASM): computation of N = I + J. b db 3
2. Motorola 680x0 AL : computation of N = I + J. c db ?
3. SPARC AL : computation of N = I + J.
2022-03-28 24
Dr. Rachel Jiang
2022-03-28 25
Dr. Rachel Jiang
Pseudo instructions
Assembler instructions
commands to the assembler
Each assembler has its own set of pseudo
instructions.
not dictated by the machine architecture, but by
the assembler functionality.
2022-03-28 26
Dr. Rachel Jiang
Some pseudoinstructions
in the Pentium 4 assembler.
2022-03-28 27
Dr. Rachel Jiang
ARC Pseudo-ops
2022-03-28 28
Dr. Rachel Jiang
Inclass Ex II
provide the definition of Pseudo
instructions
2022-03-28 33
Dr. Rachel Jiang
The Compiler Mapping
Specification
The information about the particular ISA
must be embedded into the compiler
This embedding is called the mapping
specification for the compiler
2022-03-28 34
Dr. Rachel Jiang
Variable Storage in Memory
Only global variables (in C and similar
languages)
have relative addresses that are
known at the compile time
Local variables
defined and visible only within the code
block, are implemented on the stack.
2022-03-28 35
Dr. Rachel Jiang
2022-03-28 36
Dr. Rachel Jiang
Data Movement
In an array
the memory address of the entire array is
taken to be the lowest or base address.
Machine address of an array element at
run time is:
ElementAddress = BASE + (INDEX – START) * SIZE
BASE the starting address of the array
INDEX the index of the desired element,
START the starting index of the array,
SIZE the size of the individual element in bytes
2022-03-28 37
Dr. Rachel Jiang
Example
Assume in EMU8086
BASE is in SI,
INDEX in Cl,
START in BX,
SIZE=2,
the code to load an array specified element from
memory into Al :
LEA SI, arr
mov al,cl
Mov dl,2
Mul dl ;offset*2
Add si,ax
Mov al,[si]
2022-03-28 38
Dr. Rachel Jiang
Example
Assume in ARC
BASE is in %r2,
INDEX in %r3,
START in %r4,
SIZE=4,
the code to load an array specified element
from memory into %r1 is:.
2022-03-28 39
Dr. Rachel Jiang
Example (2)
It costs three instructions to access an
array element, and more if SIZE is not
power of 2.
In languages such as C and Java
specify that START = 0
one machine instructions is saved for each array
access.
2022-03-28 40
Dr. Rachel Jiang
Arithmetic Instructions
In load/store machines (such as RISC)
there is always a possibility
arithmetic operation requires more registers than
are available.
The compiler must store variables on
registers on the stack (register-spill).
To decide which register are available
compilers use graph theoretic technique
known as register coloring.
2022-03-28 41
Dr. Rachel Jiang
Arithmetic Instructions (2)
To decide when the value in a register
is no longer needed
compilers use “live-dead analysis”
algorithm.
2022-03-28 42
Dr. Rachel Jiang
Program Control Flow
The goto Statement
The if-else Statement
The while Statement
The for Statement
2022-03-28 43
Dr. Rachel Jiang
Program Control Flow
Statement goto is implemented by
ba finish
(branch always) unconditional branch in ARCtools
jmp finish ; in emu8086
2022-03-28 44
Dr. Rachel Jiang
Program Control Flow
if (expr) stmt1 else stmt2
Assume that expr is %r1==%r2, then the code to
implement if-else statement is:
subcc %r1, %r2, %r0 !set flags, discard result
bne Over !stmt1 code
ba End !exit if-else
Over: … !stmt2 code
End: … !…
2022-03-28 45
Dr. Rachel Jiang
Program Control Flow
if (expr) stmt1 else stmt2
Assume that expr is AX==BX, then the code to
implement if-else statement is:
cmp AX, BX ;set flags, discard result
jne Over
; stm1
jmp End ;exit if-else
Over: …
;stm2 ;
End: … ;…
2022-03-28 46
Dr. Rachel Jiang
Program Control Flow (2)
while (AX==BX) CX=CX+1; is efficiently
implemented as:
jmp Test
True: ;loop body here…
inc CX
Test: cmp AX, BX
je True
.
2022-03-28 47
Dr. Rachel Jiang
Program Control Flow (2)
while (%r1==%r2) %r3=%r3+1; is
efficiently implemented as:
ba Test
True: add %r3, 1, %r3
Test: subcc %r1, %r2, %r0
be True
The do-while statement is implemented
exactly like the while statement except that
the first ba instruction is eliminated.
2022-03-28 48
Dr. Rachel Jiang
Program Control Flow (3)
for(expr1; expr2; expr3) stmt;
in C language is equivalent to
expr1;
while (expr2){
stmt;
expr3;
}
it is implemented exactly like while
statement, with the addition of code for
expr1 and expr3.
2022-03-28 49
Dr. Rachel Jiang
Ex3
Apply the program control examples to
Select one of the following C programs
and convert it into AL statements….
found = false;
return 0;
}
2022-03-28 51
Dr. Rachel Jiang
int main()
{
int array[10]={0,1,2,3,4,5,6,7,8,9}; //can be byte type…
minvalue = array[0];
index = 0;
for (i=1; i<10; i++){
if (array[i] < minvalue)
{
minvalue=array[i];
index = i;
}
printf("the minimum element in array is %d\n", minimumvalue)
}
return 0;
}
2022-03-28 52
Dr. Rachel Jiang
Forward Reference Problem
Translate AL to machine code, we need to
reference and provide memory addresses of
each instruction and variable
Machine code for each AL statement
addresses of variables
labels in the statements
in case of branching
the reference in memory before it has been defined
This leads to two-pass translators
Pass one
Pass two
2022-03-28 53
Dr. Rachel Jiang
Forward Reference example
2022-03-28 57
Dr. Rachel Jiang
Example: ARC program
2022-03-28 58
Dr. Rachel Jiang
Pass One
The purpose of pass one is to build the
symbol table, containing values of all
symbols.
A symbol is either a label or a symbolic
name that refers to a value used during
the assembly process
2022-03-28 59
Dr. Rachel Jiang
Creating a Symbol Table
2022-03-28 60
Dr. Rachel Jiang
ILC (Instruction Location Counter)
a variable used to keep track of the execution-
time address of the presently assembled
instruction
set to 0 at the beginning of pass one and
incremented by the instruction length for each
processed instruction
Pass one of most assemblers uses at least
three tables:
Symbol table
Pseudo instruction table
Opcode table
2022-03-28 61
Dr. Rachel Jiang
Example
ILC
keeps track of the address where the
instructions will be loaded in memory
Example
the statements prior to MARIA occupy 100 bytes.
2022-03-28 62
Dr. Rachel Jiang
Example (2)
2022-03-28 63
Dr. Rachel Jiang
2022-03-28 64
Dr. Rachel Jiang
Pass Two
generates
the object program
provides
Information for linking up procedures
assembled at different times into a single
executable file.
2022-03-28 65
Dr. Rachel Jiang
Example: ARC program
2022-03-28 66
Dr. Rachel Jiang
The assembled code for the
whole program
2022-03-28 67
Dr. Rachel Jiang
Creating a Symbol Table
2022-03-28 68
Dr. Rachel Jiang
2022-03-28 69
Dr. Rachel Jiang
Inclass Ex4
Provide the memory addresses of all the
variables used in the example in the
previous slide…
2022-03-28 70
Dr. Rachel Jiang
Final Tasks of the Assembler
Assembler must
add (write down) some additional
information to the assembled module for
the linker and loader:
The module name and size.
Sizes and identities of the memory segments if
they are used.
The address of the start symbol, if one is defined
in the module.
2022-03-28 71
Dr. Rachel Jiang
Final Tasks of the Assembler (2)
2022-03-28 72
Dr. Rachel Jiang
Location of Programs in
Memory
It is irrelevant where the assembled
program is located in memory prior to
execution
Most addresses are specified as being
relocatable in memory except sometimes I/O
addresses
I/O addresses
may be fixed at an absolute memory location
2022-03-28 73
Dr. Rachel Jiang
Location of Programs in Memory (2)
2022-03-28 74
Dr. Rachel Jiang
Linking
Generation of an executable binary
program from a collection of
independently translated source
procedures requires using a linker.
2022-03-28 75
Dr. Rachel Jiang
Each module has its own address
space, starting at 0
2022-03-28 76
Dr. Rachel Jiang
Diagram on the left:
(a) The object modules before being relocated and linked.
Diagram on the right(b)
The same object modules after linking and after relocation
has been performed.
2022-03-28 77
Dr. Rachel Jiang
The internal structure of an object module
produced by a translator.
2022-03-28 78
Dr. Rachel Jiang
Dynamic Linking
2022-03-28 79
Dr. Rachel Jiang
Dynamic linking in Windows
Windows OS
support dynamic linking based on special
file format called DLL (Dynamic Link
Library)
DLL
can contain procedures, data, or both.
is a lib of procedures that can be loaded into
mem and accessed by multiple processes at
the same time.
2022-03-28 80
Dr. Rachel Jiang
Dynamic linking (2)
DLL can be updated independently with
respect to the entire program.
Program can bind to a DLL in two ways:
implicit linking
explicit linking
2022-03-28 81
Dr. Rachel Jiang
implicit linking
Program
statically linked with the import library file
generated by a utility program that extracts
certain information from the DLL.
can be linked with multiple import libraries.
2022-03-28 82
Dr. Rachel Jiang
Explicit linking
does not require import libraries
does not load DLLs at the same time the
program is loaded into the memory.
Program
makes an explicit call at run time to bind
to a DLL
makes the additional call to obtain the
addresses of the procedures it needs,
and calls the procedures afterwards.
2022-03-28 83
Dr. Rachel Jiang
Resolving External References
.global
used in the module where a symbol is
defined
.extern
used in every other module that refers to it
Once use of the procedure is completed
program makes the final call to unbind from
the DLL
2022-03-28 84
Dr. Rachel Jiang
2022-03-28 85
Dr. Rachel Jiang
Symbol tables for the previous
example:
2022-03-28 86
Dr. Rachel Jiang
Loading
The loader
a software that places the load module into
main memory
loads various memory segments with the
appropriate values and
initializes certain registers
such as the stack pointer %sp and the program
counter %pc, to their initial values.
2022-03-28 87
Dr. Rachel Jiang
Loader Ex
2022-03-28 88
Dr. Rachel Jiang
Loaders (2)
Relocating loader
modifies relocatable addresses within the single
load module
several programs can reside in memory
simultaneously
Linking loader
performs both the linking and the loading
process
An alternative approach
relies on MMU (memory management unit)
relocate all memory references transparently.
2022-03-28 89
Dr. Rachel Jiang
Questions?
???
2022-03-28 90
Dr. Rachel Jiang