Lecture5 INSTRUCTIONS MICROPROCESSOR APLICATIONS
Lecture5 INSTRUCTIONS MICROPROCESSOR APLICATIONS
Lecture 5
Dr. Tao Li 1
• Software and Hardware Engineering (Old
version) Chapter 4
Or
• Software and Hardware Engineering (New
version) Chapter 7
And
• CPU12 Reference Manual Chapter 5
Some Tips
• 68HC12 has >1000 instructions!
– They are grouped into a few (17) functional categories
Dr. Tao Li 2
– Besides operation, variance w.r.t. effect on CCR,
available addressing modes, etc
Dr. Tao Li 3
• Decrement/Increment • Loop primitive
• Clear/Set • Jump and branch
• Arithmetic • Condition code
• Logic • Interrupt
• Miscellaneous
Load and Store Instructions
• 8-bit load and store instructions (LDAA, LDAB, STAA,
STAB)
Dr. Tao Li 4
MSB lower address (EA)
LSB higher address (the next location)
Dr. Tao Li 5
Endianness (Byte Order)
• Big endian: the most significant byte of multibyte data
is stored at the lowest memory address
– Sun's SPARC, Motorola's 68K, and the PowerPC
families
• Little endian: the least significant byte of multibyte
data is stored at the lowest memory address
– Intel's 80x86
Dr. Tao Li 6
A little-endian memory dump
Load Register Instructions
Load Registers (see Section
4.4)
Mnemonic Operation Mnemonic Operation
LDAA (M) 6 A LDAB (M) 6 B
LDD (M:M+1) 6 D Load memory LDS (M:M+1) 6 SP
Dr. Tao Li 7
LDX (M:M+1) 6 X data to Reg. LDY (M:M+1) 6 Y LEAS EA 6 SP
Load effective LEAX EA 6 X LEAY EA 6 Y address to Reg.
PULA (SP) 6 A PULB (SP) 6 B
PULD Load stack (SP) 6 CCR
PULX (SP:SP+1) 6 D PULC (SP:SP+1) 6 Y
element to Reg.
(SP:SP+1) 6 X PULY
See Freescale manual for
supported addressing mode and impact on CCR
Dr. Tao Li 8
STAA A 6 (M) Store Reg. data STAB B 6 (M)
Pull RA
Dr. Tao Li 12
To obtain SUBR output parameter,
caller can execute PULA
Dr. Tao Li 13
Load Effective Address Instructions
• LEA instructions (LEAX, LEAY, LEAS) to save
computed EA in 16-bit pointer register
– An Effective Address (EA) is the memory address from
or to which data are transferred
Dr. Tao Li 14
Example: LEA_ Instructions
Adds 10 bytes passed on the stack and returns
the sum on the A register
NUM: EQU !10 CalcSum:
PROG: EQU $0800 leas 2,SP
DATA: EQU $0900pula decb
STACK: EQU $0a00
ORG PROG add_loop:
adda 1,SP+
dbne b,add_loop
lds #STACK
leas {0-NUM-2},SP
ldab #NUM
rts
ldx #BUF
LoadLoop:
ldaa 0,x ORG DATA
psha inx BUF: DB 1,2,3,4,5,6,7,8,9,!10
dbne b,LoadLoop
ldab #NUM jsr
Dr. Tao Li 15
CalcSum leas
NUM,SP swi
Move Instructions
• Transfer from one memory location to another
w/o using CPU registers (MOVB or MOVW)
– Valuable for a CPU with only a few registers
– Index addressing /w 9- and 16- bit constant offsets
and indexed-indirect addressing are not allowed
Dr. Tao Li 17
• Copies data from memory specified by first
operand to memory specified by second (no
CPU register involved)
MOVB #$64,DATA1 ; initializes byte in memory
MOVW DATA2,DATA3 ; copies word from address DATA2 to DATA3
MOVB 0,Y,1,X+ ; copies byte from (Y+0) in memory to (X); ; then
X←X+1
Dr. Tao Li 19
LDX #BUFF1 ; initialize pointer to first buffer
LDY #BUFF2 ; initialize pointer to second buffer
LDAA #LEN ; initialize loop counter to length of buffer
LOOP: MOVB 1,X+,1,Y+ ; copy element from first to second buffer
DBNE A,LOOP ; decrement loop counter and branch if not zero
Dr. Tao Li 20
Decrement and Increment Instructions
Dr. Tao Li 21
DES LEAS -1, S
INS LEAS 1, S
Dr. Tao Li 22
LOOP:
- - dec Cnter
bne LOOP
- - -
LOOP:
- - -
Dr. Tao Li 23
pshx ; Save X register ldx
Cnter ; Get the counter and
dex ; decrement it
Dr. Tao Li 25
Use BCLR and BSET to turn on and off LEDs
ON OFF
0
1
Dr. Tao Li 26
Example: Clear & Set Instructions
BIT0: EQU %00000001 ; Mask for bit 0
…
BIT7: EQU %10000000 ; Mask for bit 7
ALL: EQU %11111111
REGS: EQU $0000 ; Start of the I/O regs
PORTH: EQU REGS+$24 ; Offset for Port H
DDRH: EQU REGS+$25 ; Data dir register
---
bset DDRH,ALL ; Make all bits outputs
LOOP: bset PORTH,ALL ; Turn out all LEDs (NOTE: active-low)
bclr PORTH,BIT0 ; Turn on bit 0
…
bclr PORTH,BIT7 ; Turn on bit 7
bra LOOP ; Do it forever
Of course, LEDs will appear on the whole time since mP is much faster than our
eyes!
Dr. Tao Li 27
Shift and Rotate Instructions
– Shifts come in two kinds, arithmetic and
logical. Shifts and rotates move left or right
by one bit
• Logical (LSL) and arithmetic (ASL) shift left are
identical; 0 fed in (LSB), and out (MSB) goes to C
bit in CCR
Dr. Tao Li 28
• For arithmetic shift right (ASR), copy of sign bit fed
in (MSB), out (LSB) goes to C bit
Dr. Tao Li 29
Arithmetic Shift Instructions
• ASL: Arithmetic Shift Left
Multiply
by 2
Dr. Tao Li 30
Divide
• ASR: Arithmetic Shift Right
by 2
Rotate Instructions
• Rotate left (ROL) and rotate right (ROR)
instructions rotate through the C bit
– i.e. w/ ROL, all move left, LSB updated from C, then C
updated by old MSB
Dr. Tao Li 33
Carry bit is involved in rotation
Dr. Tao Li 35
Arithmetic Instructions
• See textbook for a complete list of arithmetic
instructions
• Binary addition and subtraction
– Add register to register (ABA for A+B→A) or memory
to register (ADDA, ADDB, ADDD)
Dr. Tao Li 36
Arithmetic Instructions
• Binary addition and subtraction (contd.)
–useful for multi-byte arithmetic w/o size limit,
performed in stages; for example:
Add two 16-bit numbers stored in NUM1:NUM1+1 and
NUM2:NUM2+1
Carry bit is
LDAA NUM1+1 ;load lower byte of first
numberproduced ADDA NUM2+1 ; add lower byte of second
number
STAA NUM3+1 ; store lower byte of resultSTAA and
Dr. Tao Li 37
Arithmetic Instructions
LDAA NUM1 ; load upper byte of first numberLDAA do not
Of course, here we could more easily just add into D register in one step!
• Binary addition and subtraction (contd.)
– Same for subtraction (SBCA, SBCB); e.g. SBCA
means A-(M)-C→A
Dr. Tao Li 38
Decimal (packed BCD) addition
• We sometimes use pBCD code to input, use, or store
data (one byte contains two digits)
• DAA instruction used immediately after a binary byte
addition to adjust for proper pBCD format
• DAA automatically determines correction factor to add;
for example:
NUM1 DC.B $12 ; represents decimal 12 in pBCD 00010010
NUM2 DC.B $09 ; represents decimal 9 in pBCD 00001001
NUM DS.B 1 ; sum to go here will be 21 ----------------
… 00011011
ldaa NUM1 ; load first pBCD value
Dr. Tao Li 39
adda NUM2 ; add second pBCD value as if they’re binary
daa ; adjust back to pBCD format 00100001 staa NUM3 ; store
pBCD result
Dr. Tao Li 40
padding with zeros as we would do for unsigned
extension)
Multiplication
• Signed or unsigned binary may be multiplied (or
divided), but separate instructions since signed
multiplication works differently than unsigned
• Registers involved are implicit operands, and we have
but three choices:
Dr. Tao Li 41
MUL ;8-bit unsigned A×B→D; C=1 if bit 7 of result = 1 to
;allow rounding for fractional #s (more later)
Example: Multiplication
; 8-bit×8-bit unsigned multiply
ldaa DATA1 ; Get the multiplier ldab
DATA2 ; Get the multiplicand mul ;
The product is in D std DATA3
Dr. Tao Li 42
; 8-bit × 8-bit signed multiply
ldaa DATA1 ; Get the multiplicand
sex a,y ; Sign extend into Y
ldaa DATA2 ; Get the multiplier
tfr a,d ; Same as SEX A,D
emuls ; Extended multiply Y*D
; 32-bit product is in Y:D
; The 16 bits we need are in D
std DATA3
Dr. Tao Li 43
– 0.50 + 0.25 = .10002 + .01002 = .11002 = 0.75
C=1
A B
xxxxxxxx1xxxxxxx
Dr. Tao Li 45
Division
• Unsigned division with IDIV (16-bit), FDIV (16-bit
fractional), and EDIV (32-bit); signed with IDIVS (16-bit)
and EDIVS (32-bit)
Example: Division
; Extended, unsigned multiply and divide (here: DATA1*DATA2/100)
ldd DATA1 ; load first unsigned number
ldy DATA2 ; load second unsigned number
emul ; 32-bit product in Y:D
ldx #100 ; load divisor
ediv ; do the division
sty DATA3 ; save quotient
std DATA4 ; save remainder
Dr. Tao Li 47
Logic Instructions
• Bit-wise logical operations of AND (good to clear bits),
OR (set bits), and EOR (bit-wise XOR)
Dr. Tao Li 48
– e.g. ANDA #$0F will clear upper half of A, ORAA #$F0 will
set upper half
Dr. Tao Li 49
$30 => 0011 0000 ASCII Code Table
Dr. Tao Li 51
• Different conditional branches for signed and
unsigned data, since e.g. $FF might be large or
small
• See textbook for detailed definition of various
branches and their symbolic operations
Loop Primitive Instructions
• Decrement/increment counter in registers A, B,
D, X, Y, or SP, then branch if the counter equals
(or does not equal) to zero
Dr. Tao Li 53
• Unconditional jumps (JMP) and branches (BRA, LBRA)
always go to the target
Dr. Tao Li 54
Never Jump out of a Subroutine
jsr SUB
BACK: nop
; - - -Main program
; - - -
; - - -
SUB: nop
; - - -Subroutine
jmp BACK
Dr. Tao Li 55
Other Instructions
• CCR instructions: ORCC and ANDCC used to set/clear
C and V bits; useful when returning Boolean result from
subroutine (i.e. need not waste register for result; just
indicate correct outcome or error, and let calling
program branch accordingly to handle the outcome)
Dr. Tao Li 56
Example: Using the Carry Bit for
Boolean Information Transfer
STACK: EQU $0c00 ;Stack location
CARRY: EQU %00000001 ;Bit 0 is carry
lds #STACK ;Init stack pointer
Initialization
; - - -
bsr check_range ;Branch to subroutine that checks if a variable is within a set range
bcc IN_RANGE ;C=0 for variable in range
OUT_OF_RANGE: ; Print an error message if
out of range
; - - - Main Program
IN_RANGE:
; Continue with the process
; - - -
; - - -
Dr. Tao Li 57
; Subroutine to check if a variable is in range. If it is, clear the carry bit,
; otherwise set the carry bit and return
check_range:
; - - - ; Imagine the code to do the checking is here.
OK: clc ; Clear carry bitandcc #
%11111110
bra DONE
NOT_OK: sec ; Set carry bitorcc #%00000001
DONE: rts ; Return with the bit clear or set Subroutine
Dr. Tao Li 58