Software Notes
Software Notes
Review:
IDE
HLL Software Concepts from PreReq’s
Compiler is often integrated into a suite of development Program: data + processing
tools May also include input/output later!
IDE: Integrated Development Environment Data: (information)
Type
Compiler: transforms HLL to assembly language Variable
Assembler: transforms to “object format” (obj) Pointer
Linker: joins libraries (obj) with program (obj) and Composite type (array and struct)
creates “executable” form (exe) Processing: (instructions)
Sequential execution
Loader: loads executable form into memory Expression
Debugger: controls the execution to allow debugging Conditional: if-then-else, switch
Loop: while, for
Relevant @ H/S Interface Functions, parameters
Dec 3, 2015 5 Dec 3, 2015 6
Data: Type Data: Variable
A description of a set of (information) values + Variable: declared to have a name and be of a specific type
operations on those values
E.G. int X;
E.G. boolean, int, char, float Used in a program to hold (store) dynamic information during
The range of values is limited by the fixed-width program execution
binary values used for encoding Can change (write) the stored value during program
execution using the assignment operator (=)
@ H/S Interface: must understand the binary encoding E.G. X = 7; L-value use of X (Left of =)
of values (constants) and how to manipulate binary Can use (read) the current stored value
values (operations) to get new values E.G. Y = X + 1; R-value use of X (Right of =)
E.G. signed integers @ H/S Interface: variables are Main Memory locations
Have seen some examples of manipulating values Name of variable is associated with the variable’s address
using ALU Need instructions to load and store data values from memory
Dec 3, 2015 7 Dec 3, 2015 8
Relevant OpCode(s) uniquely identify instruction(s) Form 1 ADD 01 R=X+Y 0001 (1)
simplifies writing specific instructions (by people) Form 1 AND 04 R = X AND Y 0100 (4)
WAIT !!!
Does the encoding change for 2 operand ALU
Problem & Solution
PROBLEM: Can’t use the same execution states for
instructions impact the microarchitecture? both 3 operand & 2 operand ALU instructions!
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
bit Form 1
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0 The IR formats no longer share the same bits for
use Op Rd Rsx Rsy Not used (always 0)
the SY Reg
hex H7 H6 H5 H4 H3 H2 H1 H0 SOLUTION: Add new states to deal with the 2 operand
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
format
bit Form 2 9 8 7 6 5 4 3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 Create a branch in the FSM graph
use Op Rd Rs Not used (always 0)
Change destination from Decode state based on
hex H7 H6 H5 H4 H3 H2 H1 H0 instruction OpCode
What has changed?
How has the microarchitecture handled this previously?
Dec 3, 2015 29 Dec 3, 2015 30
Revised FSM Impact on FSM Design?
Need more Control FSM Output ROM words to hold outputs
Fetch Fetch Fetch
State F0 State F1 State F2 for the new Form 2 ALU instruction execution states
Need to change the Decode ROM entries for Form 2 ALU
instructions to point to the first execution state for Form 2
Decode Execution Execution Execution instructions Decode ROM? …. Lab 4!!!
Form 1 State E0 State E1 State E2
State
(and F3) ALU (Form 1) (Form 1) (Form 1)
That’s all ?!?
OpCode In general, could this be done to introduce many new
Form 2 instruction forms? Yes! (Lab 5 ☺ )
ALU
OpCode
Execution Execution Add a new branch in the FSM graph
State E3 State E4
(Form 2) (Form 2) Add corresponding new states in FSM Output ROM
Make Decode ROM entry for new instructions of new
form to point to new states
New states
Dec 3, 2015 31 Dec 3, 2015 32
ADD 01 01 04 (Form 1)
Decode Execution Execution Execution
Form 1 State E0 State E1 State E2
State
ALU
SUB 02 02 04 (Form 1)
(and F3) (Form 1) (Form 1) (Form 1)
OpCode 04 05 06 MOV 03 03 07 (Form 2)
03
Form 2 AND 04 04 04 (Form 1)
State encoding ALU Execution Execution
OpCode
in hex State E3 State E4 OR 05 05 04 (Form 1)
(also FSM (Form 2) (Form 2)
Output ROM 07 08 XOR 06 06 04 (Form 1)
‘Next State’
address!) Decode ROM for form 2 ALU NOT 07 07 07 (Form 2)
Dec 3, 2015 instructions must point here! 33 Dec 3, 2015 34
Syntax:
NOP This handling is the same as in Lab 4
… but different from Lab 5!
OR 05 0000 0101 OR 25 0010 0101 • Yes … must put “#” in front of the imm16 value
XOR 06 0000 0110 XOR 26 0010 0110
NOT 07 Form 2
hex H7 H6 H5 H4 H3 H2 H1 H0
24 4 4 0001
hex H7 H6 H5 H4 H3 H2 H1 H0
• imm20 value is 0-extended to 32 bits MOV 03 0000 0011 MOV 23 0010 0011
• imm20 value is 0-extended to 32 bits, then NOT 07 0000 0111 MVN 27 0010 0111
inverted
RTL: Rd NOT ( 0-extend( imm20 ) )
Flags: C = 0, V = 0, N = 1, Z = 0
Register (Indirect) Addressing Mode LDR and STR Register Mode Form
for Memory Operand
Rn contains the address of the memory operand 2 operands: both are registers
Rn is a pointer to the memory operand! bit
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
Implementations Examples
LDR (0x30): Requires a Main Memory Read: Suppose current state: Registers (hex) Main Memory (hex)
Reg Value Address Contents
1st Execution State: MAR [ Rn ] R0 1 200 300F
before
2nd Execution State: MDR MMem[ [ MAR ] ] R4 2 201 AC05
3rd Execution State: Rd [ MDR ] Execute LDR R4, [ R12 ] R12 200
LDR and STR General Form Sign-Extend( imm16 ) onto Data Bus?
for Offset Immediate memory operand 15
3 operands: 2 registers and 1 immediate
IR imm16
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
bit 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
Goes onto
16 bits tied
together low 16 bits
use Op Rd Rn imm16 Need new FSM of Data Bus
31 – 16 splitter 15 – 0
hex H7 H6 H5 H4 H3 H2 H1 H0 Output ROM bit Goes onto
Have we seen this form before? Yes! Form4! for IRS16 high 16 bits
of Data Bus IRS16
Do we need new execution states? … Yes!
32
Internal Data bus
NOTE: imm16 is signed!
Need new hardware sign-extend( imm16 )
Remember … Scenario
Head
Suppose list of 3 Nodes:
The Name of HLL variable is associated Similar diagrams were NodeX Contents
Samples
with the address where the variable is common in SYSC 2006 array[10]
integers
stored in Main Memory
NodeY Contents
Next Node pointer
Samples array[10]
integers
Address Contents NodeZ Contents
(Name) Samples array[10]
Next Node pointer
integers
}
addrs + 0xA Next 0x300
@ H/S Interface suppose use R8 as Cur pointer R8 some value
Cur = Head Recall Head @ 0x100 MOV R8, #0x100 ; R8 address of Head
MOV R8, #0x100 ; R8 address of Head
LDR R8, [ R8 ] ; R8 points to first node LDR R8, [ R8 ] ; R8 points to first node
Cur = Cur -> Next Recall Next at offset 0xA in Node
LDR R8, [ R8, #0xA ] ; R8 points to next node
Visualize this!
Dec 3, 2015 87 Dec 3, 2015 88
MOV R8, #0x100 ; R8 address of Head MOV R8, #0x100 ; R8 address of Head
R8 0x100 Source immediate mode R8 0x100 Source immediate mode
LDR R8, [ R8 ] ; R8 points to first node LDR R8, [ R8 ] ; R8 points to first node
MOV R8, #0x100 ; R8 address of Head MOV R8, #0x100 ; R8 address of Head
R8 0x100 R8 0x100
LDR R8, [ R8 ] ; R8 points to first node LDR R8, [ R8 ] ; R8 points to first node
R8 0x200 ???? Source Register mode R8 0x200 Source Register mode
Cur = Head; ☺
Dec 3, 2015 91 Dec 3, 2015 92
Dec 3, 2015 Cur = Cur -> Next; 93 Dec 3, 2015 Cur = Cur -> Next; ☺ 94
Examples Examples
Suppose current state: Registers (hex) Main Memory (hex) Suppose current state: Registers (hex) Main Memory (hex)
Reg Value Address Contents Reg Value Address Contents
before R2 1 200 300F before R2 1 200 300F
R4 2 + 201 AC05 R4 2 201 AC05
Execute LDR R2, [ R12,R4 ] R12 200 202 FF Execute LDR R2, [ R12,R4 ] R12 200 202 FF
encoding: Registers (hex) Main Memory (hex) encoding: Registers (hex) Main Memory (hex)
0x322C 4000 after
R2 FF 200 300F 0x322C 4000 before
R2 FF 200 300F
R4 2 201 AC05 R4 2 201 AC05
+
R12 200 202 FF R12 200 202 FF
Process an Array in a Node in the List Working with the Array Index
HLL: for (Cur = Head; Cur != null; Cur = Cur -> Next) { Use R8 as Cur pointer (also points to first array element)
for ( i = 0; i < 10; i++ ) { Use R9 as the index i: (could use any register … except R15)
… do something with Cur -> Samples[ i ] MOV R9, # 0 ; initialize i = 0
… load Cur -> Samples[ i ] into R10 … to do something …
} LDR R10, [ R8, R9 ] ; get Samples[i]
} Increment i: i++
@ H/S Interface: again, use R8 as Cur pointer ADD R9, R9, # 1 ; increment i
Assume pointing to node to be processed
Recall: R8 also pointing to first array element! Visualize loading array element!
LDR and STR General Form Sign-Extend[ imm20 ] onto Data Bus?
for PC-Relative memory operand 19
2 explicit operands: 1 register and 1 (signed) imm
IR imm20
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
bit 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
12 bits tied
use Op Rd imm20
together
31 – 20 19 – 0
hex H7 H6 H5 H4 H3 H2 H1 H0
Goes onto
high 12 bits
of Data Bus IRS20
Have we seen this form before? Yes! Form5!
Is all of the hardware is in place to get the operands 32
from the IR? … NO … need to sign-extend imm20! Internal Data bus
Do we need new execution states? … Yes! Need new FSM Output ROM bit for IRS20
Processor Decode
More Efficient PC-Relative Implementation? Control FSM Register Address Bus (4 bits)
Visualize #offset_to_Head
Calculating #offset_to_variable when Y < X (Y at lower addrs)
Suppose variable is stored at some address Y and instruction
accessing the variable is stored at some address X
Offset (number of words) from X to Y is Y – X addrs Y Head
Offset is signed!
– Y – ( X + 1 ) = #offset_to_Head negative offset
If Y < X then Y – X is negative, otherwise non-negative
But don’t forget that the PC has already been incremented addrs X LDR R8, [ #offset_to_Head ]
before the instruction calculates the operand address addrs X + 1 …
Therefore, the required #offset_to_variable is Y – ( X + 1 )
Visualize #offset_to_Head
when X < Y (X at lower addrs)
Do People Calculate Offsets?
Only for labs and exams ☺
incremented PC
addrs X LDR R8, [ #offset_to_Head ] The Assembler does this in practice:
addrs X + 1 …
Allocates memory to use for variables and instructions
Calculates the required offsets
– Y – ( X + 1 ) = #offset_to_Head positive offset Assembly Syntax: Label: defines Name
Directive: DeClare Data
addrs Y Head Head DCD ; reserve a word for
… ; variable named “Head”
LDR R8, [ Head ]
What would happen if X == Y? Offset?
Dec 3, 2015 131 Dec 3, 2015 132
Summary: LDR and STR Addressing Modes In this Deck: Towards the Software
1. ALU instructions manipulate data Registers only
For LDR and STR: need to access memory Constants? Addressing Modes
Addressing modes for memory operand: 2. Load and Store Instructions
Moving values between Registers and Main Memory
Register Addressing Modes
Offset Immediate 3. Control Flow Instructions
Offset Register Unconditional, Conditional
PC relative vs. absolute addressing
PC-Relative PC-Relative
Subroutines more elaborate control flow
variables (data)
desired instructions
(and not variables) are
loaded into the IR
PC
-ve offset
+ve offset
instructions
variables (data)
instructions instructions
+ve offset
variables (data) variables (data)
instructions instructions
Hi addrs
Is the ALU used for anything other than the execution of Is the ALU used for anything other than the execution of
Data Manipulation (ALU) instructions? Data Manipulation (ALU) instructions?
YES! ALU ADD operation is used in several places
• Incrementing the PC
• Calculating memory operand addresses
– Offset Immediate, Offset Register, PC-Relative
Ancillary use of the ALU (vs. ALU instruction execution)
PROBLEM: Want the FLAGS to reflect results of Data
Manipulation instructions, not results of ancillary use!
SOLUTION: Add FLAGS register and “modify FLAGS”
control signal (FCE)
Dec 3, 2015 145 Dec 3, 2015 146
Programmer must know which conditions to test when X==Y X same as Y X equal to Y
dealing with different types of values!
Dec 3, 2015 153 Dec 3, 2015 154
Unsigned: C, Z Signed: N, V, Z
Code Meaning (for comparing X to Y) Flags Tested
Code Meaning (for comparing X to Y) Flags Tested Equal
1 eq
( X == Y )
Z==1
Equal
1 eq
( X == Y ) Z==1 2 ne
Not equal
Z==0
( X != Y )
Not equal Negative. The mnemonic stands for "minus“
2 ne
( X != Y ) Z==0 5 mi
(X–Y<0)
N==1
The content is included for completeness, but will Condition is Always true!
not be covered in the course (not enough time!)
Flags have no bearing on the condition being true
We will assume that the flags are always set as
specified whenever the associated condition is true
Still need to understand the table, the conditions
and the flag settings
bit
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0 T2 NOT 0-extend( imm20 ) (FCE = 0)
nd
2 execution state: T1 [ T2 ]
use 57 Rx Imm20
rd
3 execution state: T2 [ T1 ] + 1 (FCE = 0)
hex H7 H6 H5 H4 H3 H2 H1 H0
th
4 execution state: T1 [ T2 ]
Form 5 !!! (Recall MOV immediate) No new hardware 5th execution state: -- [ T1 ] + [ Rx ] (FCE = 1)
Need new execution states
Discard difference (i.e. don’t save result in a register)
Dec 3, 2015 163 Dec 3, 2015 164
FCE = 1
Dec 3, 2015 165 Dec 3, 2015 166
What condition can test if the result is 0? 7 vs Signed overflow. The mnemonic stands for "V set". V==1
[table on next slide] 8 vc No signed overflow. The mnemonic stands for "V clear". V==0
Does the name of the condition make sense in 9 hi Unsigned higher. (C==1) && (Z==0)
Does the name really matter as long as there is 11 ge Signed greater than or equal. N==V
an appropriate condition to use after AND sets 12 lt Signed less than. N!=V
cc cTrue = 1
16-Input Multiplexer
...
7 vs (V==1)
Implementation? cTrue 8 vc (V==0)
logic for cc = i i cTrue
8 Variable K-Map?
9 hi ((C==1) && (Z==0)) ...
10 ls ((C==0) || (Z==1))
+ N != V
11 ge (N==V) 14
Z
cc 12 lt (N!=V) 1 15
logic for cc = 14 ALWAYS Use cc to select
13 gt ((Z==0) && (N==V)) (less than or equal)
condition code the relevant
((Z==1) || (N!=V))
(cc) from IR 14 le ((Z==1) || (N!=V)) condition
Dec 3, 2015 183 Dec 3, 2015 184
15 al ALWAYS cc
use 80 cc imm20
hex H7 H6 H5 H4 H3 H2 H1 H0
Seen form before? No! Form 6
Need new hardware to deal with cc
cc Mux & selection circuit
cTest control signal from FSM
cTrue Circuit
connect cc from IR (bits 23 – 20) to cc inputs of cTrue
Connect FLAGS register outputs to FLAGS inputs of cTrue
Present in Lab 7 circuit
Dec 3, 2015 Declaration can appear before or after Reference!! 193 Dec 3, 2015 194
What Address does a Label Identify? What Address does a Label Identify?
The address of the next word used in the program The address of the next word used in the program
DoThat ; on a line by itself
DoThat ; on a line by itself
(a blank line)
MOV R1, #0 ; followed by another comment line
DoThat identifies (names) the address of the MOV R1, #0
word that contains the MOV instruction DoThat identifies (names) the address of the word that
contains the MOV instruction
Assembled form: Assembled form:
Address Contents Assembly Language Address Contents Assembly Language
0000 0100 DoThat 0000 0100 DoThat ; (comment)
(blank line)
0000 0100 2310 0000 MOV R1, #0 ; (comment)
0000 0100 2310 0000 MOV R1, #0
Dec 3, 2015 195 Dec 3, 2015 196
… ; instructions for “do something” block go here 6 pl Positive or zero. The mnemonic stands for "plus". N==0
After_if ; label declaration of 7 vs Signed overflow. The mnemonic stands for "V set". V==1
; first instruction after “do something” block 8 vc No signed overflow. The mnemonic stands for "V clear". V==0
Variation 2 9 6
initialize loop, then perform 5 iterations of loop body
{ if ( (‘a’ <= X ) && (X <= ‘z’) ) { if ( (‘a’ <= X ) && (X <= ‘z’) )
{ X = X – 0x20; } { X = X – 0x20; }
return X; return X; return, return value
} }
Dec 3, 2015 219 Dec 3, 2015 220
use 83 cc imm20
Calling instruction: Branch and Link (BL)
hex H7 H6 H5 H4 H3 H2 H1 H0
R14 is the Link Register (LR)
RTL: IF (cc is true)
This approach is different than Intel processors,
which have fewer registers and use the run-time THEN LR [ PC ]
stack to hold the return address PC [ PC ] + sign-extend( imm20 )
We will talk about the run-time stack later Seen form before? Yes! Form 6
10 ls Unsigned lower or same. (C==0) || (Z==1) SUB R0, R0, #0x20 ; convert char in R0
11 ge Signed greater than or equal. N==V DoneUpCase ; R0 contains converted char
12 lt Signed less than. N!=V MOV R15, R14 ; return, with R0 = converted char
13 gt Signed greater than. (Z==0) && (N==V)
while ( strg[ i ] != null ) // null = 0x00 MOV R7, #strg ; get address of str
CheckChar LDR R0, [ R7, R6 ] ; R0 = unconverted char from str
// will assume variable named strg
CMP R0, #0 ; char == null?
{ BEQ DoneStrg ; yes! – done string
strg[ i ] = UpCase ( strg[ i ] ); BL UpCase ; convert char call Upcase, R0 = ??
i++; ; on return: R0 = converted char
...
...
Value Ry
Stack Pointer (SP) Contiguous region SP addrs Value X
addrs Value X of memory for Value W
Value W stack storage
...
Value Ry is now the value
...
addrs Value X
Value W
SP addrs Value X Values
...
Value W currently in
...
...
...
0x xxxx xxxx 0x0000 0001
SP 0x110 0x0000 000A SP 0x10F 0x0000 000A
0x5000 0000 0x5000 0000
Value currently Value currently
...
...
on top of stack on top of stack
0x xxxx xxxx 0x xxxx xxxx
0x160 0x xxxx xxxx 0x160 0x xxxx xxxx
on top of stack
0x xxxx xxxx
0x160 0x xxxx xxxx
• Mask: 0b 1000 0000 0000 1111 use Opcode unused (0) imm16
Another example: hex H7 H6 H5 H4 H3 H2 H1 H0
PUSH { R1, R6 – R8, R10 }
New operand configuration Form 7
• Mask: 0b 0000 0101 1100 0010 Needs new h/w outside the scope of this course
If you are really interested look at circuits in lab ☺
...
POP { R2 – R6, R15 } initial addrs
neat, eh! ☺
First PUSH goes here high address
Not in stack space
1 eq Equal (X == Y) Z==1
The following slides include “proofs” of the flag settings For bn-1 through bi + 1 , bit value in A = bit value in B
Included for the inquisitive mind
Will not be covered in course!
Dec 3, 2015 299 Dec 3, 2015 300
Consider Unsigned Values A and B Add Unsigned Values A + B
When A > B , then for A and NOT( B ): When A > B , then for A + NOT( B ):
Both values have bit bi = 1 (for bi on previous slide) Adding bits bi = 1 for both values causes a carry to
Both values have inverse bit values for all bits above ripple to next higher bit addition
(more significant than) bi 1 bi ?
bi
A bn-1 bi + 1 1 A bn-1 bi + 1 1
bi
B bn-1 bi + 1 0 + B bn-1 bi + 1 1
bi
B bn-1 bi + 1 1
bn-1 bi + 1 x
+ B bn-1 bi + 1 1 + B bn-1 bi + 1 1
bn-1 0x Carry 1 0 0x
Carry + 1
C=1
1 0000 0000 Z=1
bi
Y bn-1 bi + 1 1 + Y bn-1 bi + 1 0
bi
Y bn-1 bi + 1 0
bn-1 bi + 1 x
+ Y bn-1 bi + 1 0 + Y bn-1 bi + 1 1
bn-1 1x Carry 0 1 1x
What about other Signed Conditions? Signed Values X and Y and X >= Y
Code Meaning (for cmp) Flags Tested Recall number line: Suppose both non-ve and X >= Y
Signed greater than or equal – max 0 Y X +max
11 ge
( X >= Y ) N==V -ve +ve
Signed less than Calculating X – Y always gives a non-ve number, and
12 lt
(X<Y) N!=V
Signed greater than never overflows
13 gt
(X>Y) (Z==0) && (N==V)
• This scenario always gives N = 0, V = 0
Signed less than or equal
14 le
( X <= Y ) (Z==1) || (N!=V)
Signed Values X and Y and X < Y Signed Values X and Y and X < Y
Suppose both non-ve and X < Y Suppose both -ve and X < Y
– max 0 X Y +max – max X Y 0 +max
-ve +ve -ve +ve
|Y|
Calculating X – Y always gives a -ve number, and Calculating X – Y:
never overflows X Y 0
-ve +ve
This scenario always gives N = 1, V = 0 |Y|
Result: –ve value with no overflow: N = 1, V = 0
This scenario gives: N != V This scenario always gives N = 1, V = 0