0% found this document useful (0 votes)
10 views

Computer Architecture

Computer architecture compiler and assembly code conversions

Uploaded by

zaki4103856
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Computer Architecture

Computer architecture compiler and assembly code conversions

Uploaded by

zaki4103856
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

SYST 27198

CPU Architecture and


Assembly
Dr. Rachel Jiang
Sheridan Institute of Technologies and
Advanced Learning
Winter 2017

Rachel Jiang, 2007


Acknowledgement

This notes is based on Professor Victor


Ralevich’s course Notes of
“Structured Computer Organization”

Updated by Rachel Jiang


March 2017
2022-03-28 2
Dr. Rachel Jiang
Chapter Seven

Languages and the Machine

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.

 Translated program is converted into an


object program or executable binary
program.

2022-03-28 6
Dr. Rachel Jiang
Example
int main() {
int x=3,y=4,z;
z = x+y;

return 0;
}

3/28/2022 R Jiang, June 30th, 2011 7


2022-03-28 7
Dr. Rachel Jiang
 Compilation
 translates a program written in a high level
language into a functionally equivalent
program in assembly language
 Assembly language will be further
processed which is called “assembling”,
into machine language.
 Example:
 Consider a simple high-level language
assignment statement:

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 +

 This portion of compilation is referred


to as lexical analysis
2022-03-28 10
Dr. Rachel Jiang
syntactic analysis
A = B + 4;
 Parsing symbols to recognize the underlying
program structure
 the parser must recognize the form for the
above statement:
Identifier “=” Expression
 where Expression is further parsed into the form:

Identifier “+” Constant


2022-03-28 11
Dr. Rachel Jiang
semantic analysis
 Determining the underlying meaning of program
components
A = B + 4;
 Name analysis:
 associating the names A and B with particular
program variables, and further
 associating them with particular memory
locations where the variables are located at run time.
 Type analysis:
 determining the types of all data items.
 In the example above
 variables A and B and constant 4 would be recognized
as being of type int in some languages.

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

 Compiler also has to:


 allocate variables to registers,
 track registers usage, and,
 optimize the program.

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 ?

3/28/2022 R Jiang, June 30th, 2011 15


2022-03-28 15
Dr. Rachel Jiang
Example
int main() {
int x=3,y=4,z;
z = x+y;

return 0;
}

3/28/2022 R Jiang, June 30th, 2011 16


2022-03-28 16
Dr. Rachel Jiang
Inclass Exercise I
 1. How many steps to translate a program
written in a high level language into an
assembly language program in the
compilation process?
 2. What are the tasks involved in each step?
 3. provide the compilation process for the
following HL statement:
A=B*C+6

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

 Provide some examples of Pseudo


instruction used in EMU8086 AL
program

 List all the Pseudo instructions in our AL


program we have created…
2022-03-28 29
Dr. Rachel Jiang
Assembly Process
 process of translating an AL program
into a machine language program
 assemblers
 provide
 the support of allowing programmer to specify locations
of data and code
 mnemonics for all machine instructions and addressing
modes, and
 translate
 valid AL statements into the equivalent machine
language.
2022-03-28 30
Dr. Rachel Jiang
 assemblers
 Permit symbolic labels to represent
addresses and constants.
 Provide programmers to specify the starting
address of the program if there is one
 provide a degree of assemble-time arithmetic.
 Include a mechanism
 allows variables to be defined in one AL program
and used in another, separately assembled
program
 Support macro expansion
2022-03-28 31
Dr. Rachel Jiang
2022-03-28 32
Dr. Rachel Jiang
Inclass Ex3
 List 3 tasks that can be done in a
assembly process
 Use the AL example in our assignment3
to list the details for each task above

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….

 You can select while loop, for loop, or


do-while loop for this exercise…

3/28/2022 R Jiang, June 30th, 2011 50


2022-03-28 50
Dr. Rachel Jiang
int main()
{
int array[10]={0,1,2,3,4,5,6,7,8,9}; //can be byte type…

int found,i, index, vaueLookfor = 5; //can be byte type…

found = false;

for (i=0; i<10; i++){


if (array[i] == valuelookfor)
{
found = true;
index = i;
break;
}
}
if (found == true) printf("item is found\n");
else printf("item is not found\n");

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…

int minvalue,i, index; //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

3/28/2022 R Jiang, June 30th, 2011 54


2022-03-28 54
Dr. Rachel Jiang
Forward Reference example
org 100h
mov al, a
cmp al,b
jg bigger
mov bl,b
mov c,bl
mov c,b
jmp finish
bigger: mov c,al
finish: ret
a db 5
b db 3
c db ?
3/28/2022 R Jiang, June 30th, 2011 55
2022-03-28 55
Dr. Rachel Jiang
Forward Reference example

3/28/2022 R Jiang, June 30th, 2011 56


2022-03-28 56
Dr. Rachel Jiang
!Add example in
ARCTools
!This program adds two
numbers
Reference X in memory:
.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 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)

 Provide Information about


 global and external symbols.
 any library routines that are referenced
by the module.
 The values of any constants
 are to be loaded into memory.
 Relocation information

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)

 Assembler marks symbols as being


relocatable
 based on it’s own and operating system’s
conventions.
 The relocation information
 included in the assembled module’s
relocation dictionary for use by the linker
and/or loader.

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

 Dynamic linking of separately


compiled procedures means to link
each procedure at the time it is first
called

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.

 Required DLLs are then loaded immediately


into the memory, and then the program is
ready to run.

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

 1. List the tasks performed by a program


loader…
 Answer:
 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 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

You might also like