Professional Documents
Culture Documents
N2 - ASM Instruction Execution
N2 - ASM Instruction Execution
ECE
HC12/S12 Programmer’s Model 331
Register File
D 15 ‐ 8 7 ‐ 0
– Accumulators: 2 @ 8-bit OR 1 @ 16-bit
ALU
16-bit Index Register: X
Control Unit
16-bit Program Counter: addr. next instruction
ECE
Condition Code Register 331
CCR 7 S X H I N Z V C 0
Stop Disable Carry/Borrow
External Interrupt Mask Overflow BLACK = set by ALU
Half Carry Zero GRAY = other; covered later
Interrupt Mask Negative
• ALU Status Flags: each condition tested after each instr. exec.
– C: Carry Flag
• = 1 if carry (addition) or borrow (subtraction) occurs in instr. exec.
– V: Overflow Flag 2C _ Overflow An 1 Bn 1 S n 1 An 1 Bn 1 S n 1
• = 1 if 2’s complement overflow occurs in instr. exec.
– Z: Zero Flag: =1 if ALU result = 0
– N: Negative Flag: =1 if MSU of ALU result is 1 (i.e., negative in S2C)
– H: Half-Carry Flag: =1 if carry from lower nibble (nibble = ½ byte =4 bits)
1
2013, Professor A. Mason AMS Instruction Execution p.4
ECE
CCR Examples 331
• 07 + F9
• B6 + 9A
ECE
CCR Examples 331
• B7 + 4A
• 07 + F9
• B6 + 9A
• 00 – 17
ECE
CCR Example: Carry Flag & Subtraction 331
• Evaluate: -4 – (-3)
– operation is A – B (subt.) C = Cout
– A < B borrow will be needed C should be 1
Note: in S2C form, the more negative a number is, the “smaller” it is
EX: -4 = 1100 is smaller than -3 (= 1101)
Thus, you can compare A < or > B in normal/decimal or S2C notation
6
2013, Professor A. Mason AMS Instruction Execution p.8
ECE
HC12/S12 Assembly Instructions 331
ECE
HC12/S12 Assembly Instructions 331
• $ = HEX value
– EX: $12 = 1810
• % = BIN value
– EX: %10 = 210
• none = DEC value
– EX: 12 = 1210
• # denotes a data value (rather than an address)
– EX: ADDA #$A5, adds value $A5 to accA
ADDA $A5, adds value at address $A5 to accA
– illustration to clarify will come soon!
• first need to make things even more complicated
ECE
ASM Instructions & Program Assembly 331
• Program Assembly
– instr._mnemonics converted to HEX op-codes
ECE
68HC12 Expanded Programmer’s Model 331
ECE
Instruction Execution Cycle 331
• Example
1. fetch bytes for LDAA #$00
2. decode: set ALU to load value
#$00 into accA
3. execute: accA contains $00
4. fetch bytes for CMPA #$0A
5. decode: set ALU to compare accA with the value $0A
6. execute: set C/V/N/Z flags (e.g., if accA were $0A, Z1)
7. etc…
• Code Executed:
– LDAA $3000 (load from $3000)
– STAA $2000 (store to $2000)
ECE
Instruction Cycle Example 331
• Code Executed:
– LDAA $3000
• (load from $3000)
– STAA $2000
• (store to $2000)
ECE
Reading HO_3 331
Data Transfer/Manipulation
Table A. Load instructions put data into CPU memory (e.g. Register File)
Mnemonic Function Operation C V Z N
LDAA Load accumulator A AM -- 0
LDAB Load accumulator B BM -- 0
LDD Load accumulator D A M, B M+1 -- 0
LDX Load index register X X M:M+1 -- 0
LDY Load index register Y Y M:M+1 -- 0
LDS Load stack pointer (SP) SP M:M+1 -- 0
• Accumulators
– accA is MSBy of accD; accB is LSBy of accD
• Memory operations
– instructions refer to only one 8-bit memory address
• where does 2nd byte come from for 16-bit instructions (e.g., LDX)
– defined/reference memory address (M) MSBy; M+1 LSBy
ECE
Quick Review: HC12 Instructions 331
Data Transfer/Manipulation
Table B. Store instructions put data (from CPU) into memory
Mnemonic Function Operation C V Z N
STAA Store accumulator A AM -- 0
STAB Store accumulator B BM -- 0 You don’t have to memorize
A M, B M+1
these. Instruction tables will
STD Store accumulator D -- 0
STX Store index register X X M:M+1 -- 0
STY Store index register Y Y M:M+1 -- 0 always be available to you.
STS Store stack pointer (SP) SP M:M+1 -- 0 Even on exams!
Table C. Move/transfer instructions copy data to a memory/register
Mnemonic Function Operation C V Z N
TAB Transfer acc. A to acc. B BA -- 0
TBA Transfer acc. B to acc. A AB -- 0
TAP Transfer acc. A to CCR CCR A
TPA Transfer CCR to acc.A A CCR -- -- -- --
(M)1 (M)2
which is which?
MOVB Mem to Mem: move byte -- -- -- --
MOVW Mem to Mem: move Word (M:M+1)1(M:M+1)2 -- -- -- --
Shift/rotate instructions
Logic shift: input = 0, output to carry_out
Arithmetic shift: shift right puts copy of MSB into MSB
Rotate: input rolls from output (carry_out included)
• Arithmetic
– Addition: adds Register with Memory (or Register), stores in Register
• note: A A+B, but no B A+B
– Subtraction: subtr. Register with Memory (or Register), stores in Register
– Decrement: subtr. 1 from value in (M, A, B, SP, X or Y)
– Increment: add 1 to value in (M, A, B, SP, X or Y)
• Logic
– AND, OR, XOR, Complement (invert)
– 2’s complement = $00 – A (or B); additive inverse, same as A’+1
• Bit Operations
– Clear CCR flags: C, I, V
– Bit Test; AND’s A (or B) with Memory: application unknown to me
– Bit Set/Clear
• clears (make 0) or sets (make 1) individual bits of a byte
• can affect 1 or multiple bits
• very useful for defining individual bits of an I/O port
• requires a “mask” byte
2013, Professor A. Mason AMS Instruction Execution p.23
ECE
Masking Concept 331
ECE
Quick Review: HC12 Instructions 331
• Test Data
Test: C= 0, V=0, Z=, N=
– subtracts $00
– CCR flags show if value in tested memory/register is
negative (N) or zero (Z)
ECE
Address Modes 331
Identify the address mode for each of the following instructions
– Inherent, Immediate, Direct, Extended
ECE
Instruction Register Chart 331
Initial Values
Action: _______________________ A ABA
Action: _______________________ B
Inherent
Add B to A
aA $A2 aB $4B $0060 $20
aA $ED aB $4B $0060 $20 A2
$0061 $00 $0061 $00
+4B
IX $2100 IX $2100
=ED
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
SP ignore SP ignore
$2001 $5E $2001 $5E
PC ignore $2002 $20 PC ignore $2002 $20
CCR H N Z V C CCR H N Z V C
address value address value
Initial Values
Action: _______________________ A INCB
Action: _______________________ B Inherent
Increment B
aA $A2 aB $4B $0060 $20
aA $A2 aB $4C $0060 $20 (B B+1)
IX $2100 $0061 $00 IX $2100 $0061 $00
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
SP ignore SP ignore
$2001 $5E $2001 $5E
PC ignore $2002 $20 PC ignore $2002 $20
CCR H N Z V C CCR H N Z V C
address value address value
2013, Professor A. Mason AMS Instruction Execution p.30
ECE
Instruction Register Chart 331
Initial Values
Action: _______________________ A ANDA #$4C
Action: _______________________ B
Immediate
AND aA w/ M
aA $A2 aB $4B $0060 $20
aA $00 aB $4B $0060 $20 (A2)(4C)
$0061 $00 $0061 $00
A2 = 1010 0010
IX $2100 IX $2100
4C = 0100 1101
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000 0000 0000
$2000 $31 $2000 $31
SP ignore SP ignore
$2001 $5E $2001 $5E
PC ignore $2002 $20 PC ignore $2002 $20
CCR H N Z V C CCR H N Z V C
address value address value
Immediate Inherent
Initial Values
Action: _______________________ A
Action: LDX #$8000
_______________________ B ABX Add B to iX
Action: _______________________ C
aA $A2 aB $4B $0060 $20 $A2
aA aB $4B $0060 $20
aA $A2 aB $4B $0060 $20
IX $2100 $0061 $00IX $8000 $0061 $00 IX $804B $0061 $00
$00C7 $FFIY $00C7 $FF $00C7 $FF
Memory
Memory
Memory
IY $1000 $1000 IY $1000
$2000 $31 $2000 $31 $2000 $31
SP ignore SP ignore SP ignore
$2001 $5E $2001 $5E $2001 $5E
PC ignore $2002 $20PC ignore $2002 $20 PC ignore $2002 $20
CCR H N Z V C CCR H N Z V C CCR H N Z V C
address value address value address value
2013, Professor A. Mason AMS Instruction Execution p.31
ECE
Instruction Register Chart 331
Initial Values
Action: _______________________ A LDX $60
Action: _______________________ B Direct
Load iX from M (60)
aA $A2 aB $4B $0060 $20
aA $A2 aB $4B $0060 $20
IX $2100 $0061 $00 IX $2000 $0061 $00
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
SP ignore SP ignore
$2001 $5E $2001 $5E
PC ignore $2002 $20 PC ignore $2002 $20
CCR H N Z V C CCR H N Z V C
address value address value
Direct
LDAA $C7
Action: _______________________ C STAA $2001
Action: _______________________ D
Extended
aA $FF aB $4B $0060 $20
aA $FF aB $4B $0060 $20
IX $2000 $0061 $00 IX $2000 $0061 $00
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
ignore ignore
$2001 $FF
SP SP
$2001 $5E $5E
PC ignore $2002 $20 PC ignore $2002 $20
CCR H N Z V C CCR H N Z V C
address value address value
2013, Professor A. Mason AMS Instruction Execution p.32
ECE
Instruction Register Chart: CCR Flags 331
ABA
Action: _______________________ B INCB
Action: _______________________ B
aA $ED aB $4B $0060 $20
aA $A2 aB $4C $0060 $20
IX $2100 $0061 $00 IX $2100 $0061 $00
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
SP ignore SP ignore
$2001 $5E $2001 $5E
PC ignore $2002 $20 PC ignore $2002 $20
0 1 0 0
CCR H N Z 0
V C
address value - 0 0 x
CCR H N Z -
V C
address value
ANDA #$4C
Action: _______________________ B A
Action: LDX #$8000
_______________________ B
aA $00 aB $4B $0060 $20
aA $A2 aB $4B $0060 $20
IX $2100 $0061 $00 IX $8000 $0061 $00
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
SP ignore SP ignore
$2001 $5E $2001 $5E
PC ignore $2002 $20 PC ignore $2002 $20
- 0 1 0V -
CCR H N Z C
address value - 1 0 0 -
CCR H N Z V C
address value
2013, Professor A. Mason AMS Instruction Execution p.33
ECE
Instruction Register Chart: CCR Flags 331
ABX Add B to iX
Action: _______________________ C LDX $60
Action: _______________________ B
aA $A2 aB $4B $0060 $20
aA $A2 aB $4B $0060 $20
IX $8048 $0061 $00 IX $2000 $0061 $00
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
SP ignore SP ignore
$2001 $5E $2001 $5E
PC ignore $2002 $20 PC ignore $2002 $20
0 1 0 0
CCR H N Z 0
V C
address value - 0 0 0V -
CCR H N Z C
address value
LDAA $C7
Action: _______________________ C STAA $2001
Action: _______________________ D
aA $FF aB $4B $0060 $20
aA $FF aB $4B $0060 $20
IX $2000 $0061 $00 IX $2000 $0061 $00
$00C7 $FF $00C7 $FF
Memory
Memory
IY $1000 IY $1000
$2000 $31 $2000 $31
ignore ignore
$2001 $FF
SP SP
$2001 $5E $5E
PC ignore $2002 $20 PC ignore $2002 $20
- 1 0 0V -
CCR H N Z C
address value - 1 0 0 -
CCR H N Z V C
address value
2013, Professor A. Mason AMS Instruction Execution p.34
ECE
Indexed Address Mode 331
offset
6019
6017 601A
$XX 601B
reference 601C
CPU register 601D
601E
block of memory
2013, Professor A. Mason AMS Instruction Execution p.35
ECE
Indexed-Immediate Addressing 331
• Indexed-Immediate
– reference address in iX, iY, SP, or PC
– offset address in operand
• just like Immediate address mode has data in operand
• Format: MNEMONIC offset,reference
• Example: LDX #$6017
LDAA $05,X {accA <$B5+<iX>>}
• accA loaded with value in memory at addr ($65 + value in iX)
ex: addr.
• note: value at index, < <iX> >, is irrelevant 6015
– unless instr. was LDAA $00,X 6016
index 6017
1 6018
offset
2 6019
$27
accA $AA iX 6017 3 601A
target of reference 4 601B
5 $27 601C
instruction 601D
601E
block of memory
2013, Professor A. Mason AMS Instruction Execution p.36
ECE
Indexed Immediate Addressing 331
ECE
Instruction Register Chart 331
Initial Values
Action: _______________________ A LDX $3011
Action: _______________________ B Add B to A
A2
aA $A2 aB $01 $00F0 $2C
aA $A2 aB $01 $00F0 $2C
+4B
=ED
IX $2100 . . . ... IX $3100
$3011 . . ...
$3011 $31 $3011 $31
Memory
Memory
IY $1000 IY $1000
$3012 $00 $3012 $00
SP ignore ...
SP ignore ...
. . . . . .
PC ignore $301A $20 PC ignore $301A $20
CCR H N Z V C $3109 $BE CCR H N Z V C $3109 $BE
address value address value
ORAB #$F0
Action: _______________________ C which is right?
STAB $09,X
Action: _______________________ D
which is right?
aA $A2 aB $F1 $00F0 $2C
OR$A2
aA aA aB
& <F0>
$F1 $00F0 $2C aA + 09 iX
A2 OR 2C
IX $3100 . . ... IX $3100 . . ... aB + 09 iX
$3011 $31 $3011 $31
Memory
Memory
ECE
HC12 Compiler Directives 331
ECE
Assembly Process Example 331