05 Basic Computer Part2
05 Basic Computer Part2
Microcontroller-Based System
Memory
CPU
I/O
Interface
To I/O
BUS
CPU: Central Processor Unit
Microcontroller
I/O: Input/Output
e.g. M68HC11
Memory: Program and Data
Bus: Address signals, Control signals, and Data signals
Accumulator A
15
15
15
15
15
15
Clock is implied
0 7
Accumulator B
Double Accumulator D
INDEX REGISTER X
INDEX REGISTER Y
STACK POINTER
PROGRAM COUNTER
00
Decode stage
Instruction is decoded into a set of microinstructions (e.g. FSM)
Execution stage
The micro-instructions are executed. This may
involve loading the operand from memory.
Simple Example
Reset EQU $FFFE ; Set symbol Reset to FFFE16
Program EQU $E000 ; Set symbol Program to E00016
ORG Program
; Set the assemblers location to the value
; represented by symbol Program.
Top: LDAA #$23
; Load the A register with 2316
LDAB #$BE
; Load the B register with BE16
ABA
; Add the A and B registers. Result is stored in A.
STAA $1000
; Store the value in the A register in
; memory location $1000. The A reg is
; unchanged.
L0: BRA L0
Org Reset
; Branch to Label L0
FDB Top
Simple Example
We can also represent this as:
A $23 ; The A register is assigned $23
B $BE ; The B register is assigned $BE
AA+ B ; A= A+ B
($1000) A ;The contents at memory location
; $0100 is replaced with $23+$BE
$86
This is the Operation Code or opcode
The opcode is the Hex (i.e. binary) representation of the
instruction.
$86 means LDAA # or
Load the A register with a constant value for the 68HC11
$23
This is the operand for the opcode. For this instruction, 23 is the
constant value to be loaded into the A register.
$be
This is the operand for the opcode. For this instruction, 23 is the
constant value to be loaded into the A register.
$E005 b7 10 00
$b7 = STAA in an extended memory address
address is stored at the operand
$1000 is the storage address
Note: the contents at location $1000 are
overwritten
Decode stage
Instruction is decoded into a set of micro-instructions
(e.g. FSM)
Execution stage
The micro-instructions are executed. This may involve
loading the operand from memory.
Fetch Opcode
3.
Decode Opcode
IR ($E000) = 86
CPU decodes as LDAA #
4.
Execute opcode
CPU fetches operand as next byte from program memory.
Instruction is executed.
Program counter is incremented PC = PC + 2
Fetch Opcode
3.
Decode Opcode
IR ($E002) = c6
CPU decodes as LDAB #
4.
Execute opcode
CPU fetches operand as next byte from program memory.
Instruction is executed.
Program counter is incremented PC = PC + 2
.
.
TPS Quiz
THRSIM11
Design Procedure
1.
2.
3.
4.
5.
6.
7.
Assembler
An assembler converts the assembly
language program into machine code.
The machine code is also known as the
op-code. We will use a Mnemonic (e.g.
LDAA) to represent this op-code.
Assembler THRSIM 11
Well use the THRSIM11 assembler
Syntax: () = Optional
(Label) Opcode (Operand) (Comment)
Ex:
Loop: LDAA #$2F Load Accumulator A with
Hex $2f
Operand Field
Operand of Op-Code (if, needed)
Can consist of:
Symbols assembler replaces the symbol
with its value
Ex: COUNT EQU $5F
LDAA COUNT
EQU is an assembler directive. It instructions the
assembler to replace the symbol COUNT with the
value of $5F, so the above is equivalent to:
LDAA $5F
Operand Field
Constants - Value does not change
Formats:
Operand Field
Expressions Assembler can evaluate
simple expressions to compute the value
of the operand
Operators
+ = Addition
- = Subtraction
* = Multiplication
/ = Division
Operand Field
Expressions
Operators (continued)
Operand Field
Expressions
Examples
COUNT EQU $1F
EX1: LDAA COUNT+$0A
A ($1F+$0A) = ($19)
LDX #TEST
This is the same as
LDX #$3000
ORG Code
Assembler assumes Program Counter now
contains the value of $3000
Ex:
Data
EQU $1000
ORG Data
Buffer1 FCB $10,$FA,$2F
Result is at memory address $1000, we have
$1000: 10 FA 2F
Example:
Buffer1 FDB $10FA,$2FFF
Contents of memory bytes starting at address Buffer1:
$10, $FA, $2F, $FF
Example:
String: FCC Hello
The data bytes stored at address String are
$48, $65, $6c, $6c, $6f
Comment Field
Available with all assembler directives and
instructions.
Placed after operand field
Use asterisk to indicate comment in first
character position
Best Practices
Best Practices
2. All subroutines must begin with the
following comment
***************************
*
Subroutine Name: Mysub
*
Input parameter list:
*
Output parameter list:
*
Registers changed list:
****************************
*
Description of Subroutine
Best Practices
3. All lines must contain a comment
4. All labels must end with a colon (:)
5. Must declare and use a symbol for all
constants, using EQU.
Best Practices
6. Program must have a well-organized overall format,
such as:
Header Comment, from previously
***********************
*
Standard Symbols
Data
EQU $0200 ; Start of RAM in U5, to $FFF
Program EQU $1040 ; Just above config registers
Stack
EQU $7FFF ; Top of RAM in U5
Reset
EQU $FFFE ; Reset vector location
************************
; comment
***********************
*
Data area
Example
Write an 68HC11 assembly language
program to monitor the temperature of a
power plant. If the temp >= 50C, sound
the alarm.
Given the following memory addresses
Analog to digital converter = temperature (in Hex)
Port B bit 0 = Output alarm
0 no alarm
1 alarm will sound
Pseudo-Code
Arithmetic Instructions
Other Instructions
Logic
Data Test
Conditional Branch
Unconditional Jump and Branch
Subroutines
Interrupt
Miscellaneous
Instruction Cycle
1. IR (PC)
Opcode is fetched into Instruction Register
2. Instruction is decoded
3. Data or address is loaded from memory
(if needed)
Program Counter is updated
Load Instructions
Mnemonic Operation
LDAA Load Accumulator A
LDAB Load Accumulator B
LDD - Load Accumulator D
LDS - Load Stack Pointer
LDX - Load Index X Register
LDY - Load Index Y Register
Addressing modes:
All except INH
Condition codes:
Set N,Z and V=0
86 02
96 02
B6 10 02
A6 02
A6 00
Store Instructions
Mnemonic Operation
(Illegal operation)
5A 02
7A 10 02
6A 02
6A 00
In other words:
[$C000]=$12, [$C001]=$34, [$C002]=$56,
[$C003]=$78, [$C004]=$9A, [$C005]=$BC,
[$C006]=$DE, [$C007]=$F0
Address
$C000
$C001
$C002
$C003
$C004
$C005
$C006
$C007
Data
$12
$34
$56
$78
$9A
$BC
$DE
$F0
Ex: $C000: 12 34 56 78 9A BC DE
LDAA $C001 A ($C001) = $34
LDX $C002
X $5678
STX $C004 ($C004) $5678
$C000: 12 34 56 78 56 78
TAB : Transfer A to B:
B (A)
TBA : Transfer B to A:
A (B)
TSX : Transfer SP to X:
X (SP) + 1
TSY : Transfer SP to Y:
Y (SP) +1
TXS : Transfer X to SP:
SP (IX) -1
TYS : Transfer X to SP:
SP (IY) 1
XGDX: Exchange D and X:
X D
XGDY: Exchange D and Y:
Y D
Decrement Instructions
Mnemonic Operation
DECA
DECB
DES
DEX
DEY
Decrement Instructions
Mnemonic Operation
DEC : Decrement Mem: (M) <- (M) -1
Increment Instructions
Mnemonic Operation
INCA
INCB
INS
INX
INY
Increment Instructions
Mnemonic Operation
INC : Increment Mem: (M) <- (M) +1
Clear Instructions
Mnemonic Operation
CLR : Clear Memory: (M) <- 0
CLRA : Clear A: A <- 0
CLRB : Clear B: B <- 0
Addressing modes:
CLR: EXT, IX, IY
CLRA, CLRB: INH only
S = Stop
X = X Interrupt Bit
I = Interrupt Mask
Control Bits
N = Negative
Z = Zero
V = Overflow
C = Carry
H = Half Carry
Arithmetic Bits
CBA: Compare B to A:
A-B
CMPA: Compare memory to A: A - (M)
CMPB: Compare memory to B: B - (M)
CMPD: Compare memory to D: D - (M:M+1)
CMPX: Compare memory to X: X - (M:M+1)
CMPY: Compare memory to Y: Y (M:M+1)
S
u
bt
ra
he
n
d
(B
)
Minuend (A)
7F
80
81
AB
00
01
(0)
(1)
(127)
(128, 128)
(129, 127)
(255, 1)
00
00
NZVC
01
NZVC
7F
NZVC
80
NZVC
81
NZVC
FF
NZVC
FF
NZVC
00
NZVC
7E
NZVC
7F
NZVC
80
NZVC
FE
NZVC
81
NZVC
82
NZVC
00
NZVC
01
NZVC
02
NZVC
80
NZVC
80
NZVC
81
NZVC
FF
NZVC
00
NZVC
01
NZVC
7F
NZVC
7F
NZVC
80
NZVC
FE
NZVC
FF
NZVC
00
NZVC
7E
NZVC
01
NZVC
02
NZVC
80
NZVC
81
NZVC
82
NZVC
00
NZVC
(0)
01
(1)
7F
(127)
80
(128, 128)
81
(129, 127)
FF
(255, 1)
FF
Branch Instructions
A Branch Instruction typically follows a
Compare instruction. The Branch
instruction will branch if certain CCR bits
are set or clear
Branch Instructions
BRA Branch Always
Unconditional Branch (i.e. GoTo)
Branch Instructions
BMI - Branch if Minus
Branch if N = 1
Branch Instructions
Unsigned
BHI - Branch if High
Unsigned, Branch if C OR Z = 0
Branch Instructions
Signed
BGE - Branch if Greater Than or Equal
Signed, Branch if N XOR V = 0
Test
Rationale
Si
g
n
e
d
BLT
NV = 1
BGE
NV = 0
BLE
Z+(NV) = 1
AB if either A<B,
or if A=B (that is if AB=0, so Z=1).
BGT
Z+(NV) = 0
U
n
si
g
n
e
BLO,BCS C = 1
BHS,BCC C = 0
BLS
C+Z = 1
BHI
C+Z = 0
AB=?
Universal
Signed Only
Unsigned
B B B B B B B B B B B B B B
N E M P G L G L V V H H L L
N Z V C E Q I L E E T T S C I S O S
Condition
Code Flags
FF 80 = 7F
N Z V C
01 FF = 02
N Z V C
81 7F = 02
N Z V C
80 80 = 00
N Z V C
FF 7F = 80
N Z V C
80 81 = FF
N Z V C
7F 81 = FE
N Z V C
Branch Examples
Example 1
LDAA #$F2 (-14, signed) (242 unsigned)
LDAB #$F0 (-16 signed) (240 unsigned)
CBA (Compute A-B)
Branch Example
Example 2
LDAA #$F2 (-14, signed) (242 unsigned)
LDAB #$F2 (-14, signed) (242 unsigned)
CBA (Compute A-B)
N bit = 0
Z bit = 1 (result is zero)
V bit = 0
C bit = 0 (not carry out)
Branch Example
Example 3
LDAA #$F2 (-14, signed) (242 unsigned)
LDAB #$FF (-1, signed) (255 unsigned)
CBA (Compute A-B)
Branch Example
Example 4
LDAA #$F2 (-14, signed) (242 unsigned)
LDAB #$02 (2, signed) (2 unsigned)
CBA (Compute A-B)
Branch Example
Example 5
LDAA #$02 (2, signed) (2 unsigned)
LDAB #$F2 (-14, signed) (242 unsigned)
CBA (Compute A-B)
Branch Example
Example 6
LDAA #$80 (-128 signed, +128 unsigned)
LDAB #$7F (+127 signed, +127 unsigned)
CBA (Compute A-B)
Branch Example
Example 6
LDAA #$80 (-128 signed, +128 unsigned)
LDAB #$7F (+127 signed, +127 unsigned)
CBA (Compute A-B)
Example 4-43
Let A=$FF and ($1000) = $00
Given Code Fragment:
CMPA $1000
Unsigned Branches
BHS yes, BLS no, BHI yes, BLO no
Signed Branches
BGE no, BLE yes, BGT no, BLT yes
Example 4-43:
Solution Summary
Signed Branches
BGE no, BLE yes, BGT no, BLT yes
Unsigned Branches
BHS yes, BLS no, BHI yes, BLO no
Other Branches
BEQ no, BNE yes
TPS Quiz
Test Example
PULA ; A (SP)
* Pull instruction does not affect CCR
TSTA ; Tests if A=0
* This instruction will set N and Z bits
BPL Label ; Branch if A>0
Bit Example
Memory EQU $1000
MASK EQU %10000000
Shifting Instructions
Multiplies by 2
Divides by 2
Multiplies by 2
Divides by 2
Logic Instructions
Mnemonic Operation
Complement Instructions
Mnemonic Operation
COM: 1s Complement of mem: (M) (M)
COMA: 1s Comp of A: A (A)
COMB: 1s Comp of B: B(B)
Subroutine Instructions
JMP/JSR/BSR/RTS
JMP Instructions
Jump to Address
Similar to BRA but uses absolute address
Mnemonic Operation
JMP Address
JMP Example
Program EQU $E000
Stack
EQU $00FF
ORG Program
E000: 8E 00 FF Top:
E003: 7E E0 03 Done:
********** Compare with
E000: 8E 00 FF Top:
E003: 20 FE
Done:
LDS #Stack
JMP Done
LDS #Stack
BRA Done
JSR Instructions
Jump to Subroutine
Similar to JMP except we have the ability to return to
the calling program. Same as HLL.
Mnemonic Operation
JSR Address
JSR Instruction
JSR
Pushes Program Counter onto Stack
Note: PC is pointing to the next instruction
BSR Instructions
Branch to Subroutine
Mnemonic Operation
BSR Relative_Address
JMP/JSR/BSR Instructions
JMP Jump to Address
Unconditional jump to address
RTS - Instruction
RTS Return from Subroutine
PULL PC from Stack
Recall this contains the EA of the instruction
following the original JSR/BSR
Base 16
Sixteen Digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Example: EF5616
Positional Number System
0000
0001
0010
0011
n1
16 L 16 16 16 16 16
0
1
2
3
0100
0101
0110
0111
4
5
6
7
1000
1001
1010
1011
8
9
A
B
1100
1101
1110
1111
C
D
E
F
Example: 5137
As Hex = $1411
As BCD = $0501,$0307
As Packed BCD = $5137
Subroutine Example
*************************
*
Subroutine Name: Bcd2Asc
*
Input parameter list:
A
*
Output parameter list: A,B
****************************
* This routine converts a Packed BCD byte in A
* into a pair of ASCII characters in A,B.
**************************************************
LOWER
EQU
$0F
; Mask for low nybble
UPPER
EQU
$F0
; Mask for high nybble
ASCII
EQU
$30
; ASCII code for numeral 0
Bcd2Asc: TAB
; Copy A register to B
ANDB #LOWER
; Mask-select lower nybble of B
ADDB #ASCII
; Convert B to ASCII code
ANDA #UPPER
; Mask-select upper nybble of A
LSRA
; Logical shift right 4
positions
LSRA
LSRA
LSRA
ADDA #ASCII
; Convert A to ASCII
Using Subroutine
* Standard equates
CODE
EQU $E000
DATA
EQU $0000
STACK
EQU $7FFF
EOS
EQU $FF
array
result
ORG
FCB
RMB
ORG
Main:
LDS
LDX
LDY
Loop:
LDAA
CMPA
BEQ
JSR
STD
INX
INY
INY
BRA
Done:
WAI
Infloop: BRA
;
;
;
;
Start
Start
Start
Well
code in EEPROM
data in page 0 internal RAM
stack at top of external RAM.
use hex $FF to mark end-of-string of BCD bytes.
DATA
$01,$23,$45,$67,$89,EOS
10
CODE
#STACK
#array
#result
0,X
#EOS
Done
BCD2ASC
0,Y
;
;
;
;
;
;
;
;
;
;
;
Loop
; Do it all again
; End of main program, wait for interrupts
; If we ever get here, stay here
Infloop
Example:
MUL Instruction
MUL Multiply Instruction
Multiplies 8-bit unsigned numbers loaded into the A
and B registers
D AB
Max result = $FF$FF = $FE01 = 65,020 =2552
For signed multiplication:
Determine sign of A and B
()()=(+), (+)(+)=(+),
NEG negative numbers
MUL, NEG (if needed)
()(+)=(),
(+)()=()
IDIV Instruction
IDIV Integer Divide Instruction
Divides 16-bit unsigned D register by the 16-bit
unsigned X register
Quotient is stored in the X register
X Integer portion of D/X
Fractional Numbers
Recall 20=1, 21=2, 22=4, .
But, we can also go the other way
2-1=0.5, 2-2=0.25, 2-3=0.125, 2-4=0.0625,
Fractional Numbers
We have
2.5 = %10.10 and 5.25 = %101.01
Fractional Numbers
Lets use 16-bit numbers where 4-bits (or one
nybble) are reserved for the fractional part
So, Y = $NNN.N
Example, What is 375.875 in Hex?
375 $177
0.875 = 1*0.5+1*0.25+1*0.125+0*0.0625
= %1110 = $E
Fractional Numbers
Lets use 16-bit numbers where 4-bits (or one
nybble) are reserved for the fractional part
So, Y = $NNN.N
Example, What is $24E3 as a fractional decimal?
$24E 590
$3 = 0*0.5+0*0.25+1*0.125+1*0.0625=0.1875
Or, $24E3 = 590.1875
FDIV Instruction
FDIV Fractional Divide Instruction
Divides 16-bit unsigned D register by the
16-bit unsigned X register
D register is assumed less than X register.
(Answer is less than one)
Radix point is assumed to be the same
Fractional quotient is stored in the X register.
Ex: %0.1010.
Remainder is stored in the D register
If X is $0000, C 1, and D $FFFF
Miscellaneous Instructions
NOP Instruction
NOP No operation
Performs no operation
Can be used for crude timing routines
2 Clock Cycles for each instruction
STOP Instruction
STOP STOP operation
Puts the CPU to sleep
Can be awakened by using an interrupt
Interrupt
Instructions
Interrupt Instructions
CLI Clear Interrupt Mask
Clears I bit to 0
End of Section
TPS Quiz
Example 4-43
Let A=$FF and ($1000) = $00
A = -1 (signed), A=255 (unsigned)
Given Code Fragment:
CMPA $1000
N=1
Z=0
V=0
C=0
BEQ: Z = 1| Z = 0 | No
BNE: Z = 0| Z = 0 | Yes