Professional Documents
Culture Documents
Size
Destination Source
MOVE.B
Opcode
D0, D1
Operand
Machine Instruction
0011 0000 0000 0011
0011 Op-code
Types of instruction i.e. addition or subtraction Data size, byte, word or long word The amount of the word in complete instruction Where the data for an instruction (operand) can find
Cycle of Instruction
Collection
Implement
Cycle of Instruction
Collection phase:
cont
Fill-in program counter with instruction address Collect instruction from memory, put in the instruction register For control the instruction, clean-up PC for next instruction
Implement phase:
Count for the operation address Collect operation Do the operation Store the product of operation Back to first step
Syntax
Dn An xxx.W xxx.L d16 (PC) d8 (PC,Xn) (An) (An)+ -(An) d16(An) d8 (An, Xn) #xxx #1-#8 SR USP SP PC
010000 42 010002 55
51 13
010000 010002
00 55
51 13
010000 42 010002 55
51 13
010000 010002
00 55
00 13
010000 42 010002 55
51 13
010000 010002
00 00
00 00
01000 42 01002 55
51 13
01000 00 01002 55
51 13
Cont
16-bit (0000~7FFF, 8000~FFFF) Sign extend the 16-bit value into 24-bit (000000~007FFF, FF8000~FFFFFF)
Absolute Long:
PC Relative Addressing
PC Relative with Offset MOVE.W $1200(PC), D5
Assume the instruction is located at $122000 Effective address: $00122002 + $00001200 = $00123202
PC Relative Addressing
Cont
PC Relative Addressing
Cont
PC Relative Addressing
Cont
Immediate Addressing
Immediate MOVE.L #$1FFFF, D0 Before:D0 = 12345678 After: D0 = 0001FFFF
Note: $ = value for hexadecimal @ = value for octal % = value for binary & or blank = decimal AB = character ASCII
Immediate Addressing
Cont
Quick Immediate 1. MOVEQ #$2C, D3 Before: D3 = 1234562C After: D3 = 0000002C 2. MOVEQ #$8F, D3 Before: D3 = 1234568F After: D3 = FFFFFF8F
Implied Addressing
Data transfer Arithmetic Logical Shift and rotate Bit manipulation BCD operations Program control System control
Logic Instructions
Shift Instructions
Rotate Instructions
Bit-Manipulation Instructions
MOVE.W
D1
$10000, D1
D1 FE ED 42 51
FE ED BE EF
010000 010002
42 55
51 13
010000 42 010002 55
51 13
MOVE.L
D1
$1000, D1
D1 42 51 55 13
FE ED BE EF
01000 42 01002 55
51 13
01000 42 01002 55
51 13
MOVE.B
D1
$1000, D1
D1 FE ED BE 42
FE ED BE EF
01000 42 01002 55
51 13
01000 42 01002 55
51 13
MOVE.W
D1
D1, $1000
D1 FE ED BE EF
FE ED BE EF
01000 42 01002 55
51 13
01000 BE EF 01002 55 13
MOVE.L
D1
D1, $1000
D1 FE ED BE EF
FE ED BE EF
01000 42 01002 55
51 13
01000 FE ED 01002 BE EF
MOVE.B
D1
D1, $1000
D1 FE ED BE EF
FE ED BE EF
01000 42 01002 55
51 13
01000 EF 51 01002 55 13
MOVE.W
$1000, $1006
42 55 01 90
51 13 02 AB
42 55 01 42
51 13 02 51
MOVE.B
$1000, $1006
42 55 01 90
51 13 02 AB
42 55 01 42
51 13 02 AB
Immediate Mode
MOVE.B
D1
#$F1, D1
D1 FE ED BE F1
FE ED BE EF
MOVEQ Instruction
MOVEQ
D1
#$F1, D1
D1 FF FF FF F1
FE ED BE EF
Fill up Memory
MOVE.B
001000 001002 001004 001006
#$F1, $1000
42 55 01 90 51 13 02 AB 001000 001002 001004 001006 F1 55 01 90 51 13 02 AB
EXG Instruction
EXG
D1, D5
D1 12 12 20 30 D5 00 FF 48 7C
D1 00 FF 48 7C D5 12 12 20 30
SWAP Instruction
SWAP
D1
D1 12 34 20 30
D1 20 30 12 34
LEA
$00200001, A0
A0 00 20 00 01
A0 00 12 70 02
LEA
(A1), A0
A0 00 10 FF FF A1 00 10 FF FF
A0 00 12 70 02 A1 00 10 FF FF
LEA
8(A0), A1
A0 00 12 70 02 A1 00 12 70 0A
A0 00 12 70 02 A1 00 00 40 00
LEA
8(A0, D4.L), A1
D4 00 00 20 00 A0 00 12 70 02 A1 00 12 90 0A
D4 00 00 20 00 A0 00 12 70 02 A1 00 00 40 00
Arithmetic Instruction
ADD Instruction Destination + Source p Destination ADD.W D1, D3
D1 12 34 56 78 D1 12 34 56 78 D3 FD CC 57 9B
$1000,$2000 $2000,D3
D3 FD CC 01 23
ADD.W ADD.W
Arithmetic Instruction
Cont
Solution:
- Copy the first word (B) from memory to register - Add the second word (C) to register - Copy the addition product to the memory
Program:
MOVE.W ADD.W MOVE.W B,D1 C,D1 D1,A ; Take B ; Add with C ; Store product in A
Arithmetic Instruction
Cont
No carry, C = 0
Arithmetic Instruction
Cont
Arithmetic Instruction
Cont
ADD.B D0, D3
V = 0, C = 0
V = 1, C = 0
V = 0, C = 1
V = 1, C = 1
Arithmetic Instruction
Cont
ADDA Instruction
Data size: W, L ADDA.L #$70, A1 A1 00 00 20 30 A1 00 00 20 A0
Use ADDQ.L #$1, A1 for value 1~8, it is faster than ADDA.L #$1, A1
Arithmetic Instruction
Cont
SUBA Instruction
Data size: W, L
SUBA.L
#$70,A1
A1 00 00 1F C0
A1 00 00 20 30
Use SUBQ.L #$1, A1 for value 1~8, it is faster than SUBA.L #$1, A1
Arithmetic Instruction
Cont
D6 12 34 56 FF
D6 12 34 57 02
Arithmetic Instruction
Cont
ADDQ
vs
MOVEQ
ADDQ MOVEQ -128+127 Dn only L only
1..8 B, W, L
Destination Any
Arithmetic Instruction
Cont
D3 FD CC 01 23
SUB.W
$1000, $2000
Arithmetic Instruction
Cont
ADD
s
S, D
d Answer (d)
Any Any Positive Negative Negative Positive
Overflow
Nothing happen (always V=0) Nothing happen (always V=0) V=0 V=1 V=0 V=1
Negative Negative
Arithmetic Instruction
Cont
SUB s
Positive
S, D d
Positive
Answer (d)
Any Any Positive Negative Negative Positive
Overflow
Nothing happen (always V=0) Nothing happen (always V=0) V=0 V=1 V=0 V=1
Arithmetic Instruction
Cont
C = Set; 2 unsigned values V = Set; 2 signed values Z = Set; product is 0 N = MSB X=C
Arithmetic Instruction
Cont
D0 before SUB
D0 00 D0 00 D0 00 D0 00 D0 00 D0 00 00 00 00 00 00 00 30 00 FF 00 90 70 40 40 FF 00 00 00
D1 before SUB
D1 00 D1 00 D1 00 D1 00 D1 00 D1 00 00 00 00 00 00 00 00 00 55 03 70 90 02 40 AA 00 00 00
XNZVC
After instruction
0 0 0 1 0 1 0 0 1 1 0 1 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 1
Arithmetic Instruction
Cont
D6 12 34 56 FF
D6 12 34 56 F8
Arithmetic Instruction
Cont
SUBQ
vs
MOVEQ
SUBQ MOVEQ -128+127 Dn only L only
1..8 B, W, L
Destination Any
Arithmetic Instruction
Cont
Multiplication Instruction 1. Unsigned number MULU.W source, Dn 2. Signed number MULS.W source, Dn
Note:
Source: 16-bit of any address mode Both operand: 32-bit data register Multiply product: 32-bit stores in destination data register
Arithmetic Instruction
Cont
D3 00 00 02 00
32-bit
Arithmetic Instruction
Cont
D3 FF FE 00 01
Arithmetic Instruction
Cont
D3 00 00 00 01
Arithmetic Instruction
Cont
(3 x 160 = 48010) (3 x 160 = 48010) (3 x (-256) = -76810) (3x 65280 = 19584010) (-20480 x (-256) = 524288010) (45056 x 65280 = 294125568010)
Arithmetic Instruction
Cont
Division Instruction
1. Unsigned number DIVU.W source, Dn 2. Signed number DIVS.W source, Dn
Note:
Source: 16-bit of any address mode Both operand: 32-bit data register Division product: lower word destination register Remainder: upper word destination register
Arithmetic Instruction
Cont
Arithmetic Instruction
Cont
Result D3 = 100 0000 0000 0000 0000 (19-bit) Destination can hold 16-bit data only Operand remains unchanged and a division overflow sets the V = 1
Arithmetic Instruction
Cont
Division Instruction Example 3: (Case divide with zero) MOVE.L #$122, D3 CLR D0 DIV D0, D3
Arithmetic Instruction
Cont
Arithmetic Instruction
Cont
Arithmetic Instruction
Cont
Arithmetic Instruction
Cont
Convert byte @ word operand to word @ longword by extending the sign bit of the operand
EXT.W
D5
D5 34 67 00 F0 D5 34 27 20 65
D5 34 67 FF F0 D5 34 27 00 65
Arithmetic Instruction
Cont
EXT.L
D5
D5 34 67 30 F0 D5 34 27 99 65
D5 00 00 30 F0 D5 FF FF 99 65
Practical Example
Write a sequence of program to compute X = 5 * Y + Z/W where Y, Z and W are 16-bit signed integers and the result is stored as longword X. Solution:
MOVE.W MULS MOVE.W EXT.L DIVS EXT.L ADD.L MOVE.L Y, D0 #5, D0 Z, D1 D1 W, D1 D1 D1, D0 D0, X Location and register Location X Location Y Location Z Register D0 Register D1 As Hexadecimal 0012 FFAE FF00 00000308 FFFFFE00
Logical Instruction
A 0 0 1 1
B 0 1 0 1
AB 0 0 0 1
AB 0 1 1 1
AB 0 1 1 0
A 1 1 0 0
B 1 0 1 0
Logical Instruction
Cont
<sea>, <dea> #data, <dea> <sea>, <dea> #data, <dea> <sea>, <dea> #data, <dea> <ea>
Either one is Dn operand The 1st immediate operand Either one is Dn operand The 1st immediate operand Either one is Dn operand The 1st immediate operand
Logical Instruction
Cont
AND Instruction
@ D0 = $0000000A
Logical Instruction
Cont
OR Instruction
@ D0 = $A2ED
Logical Instruction
Cont
EOR Instruction
@ D0 = $A2D2
Logical Instruction
Cont
NOT Instruction
@ D0 = $A2D2
Shift Instruction
4 instructions of shift operations: ASL (arithmetic shift left) ASR (arithmetic shift right) LSL (logical shift left) LSR (logical shift right) Size: B, W, L Affected flag: X & C = depend to the bit shift out N & Z = depend to the product V = 1 if sign operand changes by shifting, but 0 for rotate
Shift Instruction
Cont
D1 12 34 56 FC
(-4)
Shift Instruction
Cont
Unsigned value
0
D1 12 34 56 F0
(240)
D1 12 34 56 3C
(60)
Rotate Instruction
4 instructions of rotate operations: ROL (rotate left) ROR (rotate right) ROXL (rotate left through X flag) ROXR (rotate right through X flag) Size: B, W, L Affected flag: X & C = depend to the bit shift out N & Z = depend to the product V = 1 if sign operand changes by shifting, but 0 for rotate
Rotate Instruction
Cont
Example: ROR.B D1 12 34 56 A1
(10100001)
#2, D1 D1 12 34 56 68
(01101000)
Rotate Instruction
Cont
D1 12 34 56 A8
(10101000)
Example
Running light in address $80000
MOVE.B REPEAT MOVE.B JSR ROL.B BRA #1,D0 D0,$800000 DELAY #1,D0 REPEAT
;Delay
Test the specified bit in the destination Data size = B or only 1. The bit manipulation instruction included:
a. BCHG (bit test and change) instruction b. BCLR (bit test and clear) instruction c. BSET (bit test and set) instruction d. BTST (bit test) instruction
Example 1:
In address $FF8000 contains $AA. What is the result after BTST.B #3, $FF8000?
Solution:
Example 2:
Data register D5 contains 2C3459A7. What is the state of the Z flag and what are the contents of D5 after BCHG #6, D5?
Solution:
lower byte of D5 = 101001112. Bit 6, 1<0>100111, is a 0. This will set the Z flag & complement bit 6. This result in D5 = 2C3459E716.
Example 3:
Data registers D6 and D7 contain 0000000C and 75793290, respectively. What is the result of BCLR D6, D7?
Solution:
Register D6 specifies that the 12th bit position 001<1>001010010000) should be tested and cleared. Since the 12th bit is a 1, Z = 0 bit 12 of D7 will be cleared D7 = 75792290.
Example 4:
What is the result of BSET #2,(A3)?
Solution:
The bit in position 2 of the memory location pointed to by A3 is tested and then set.
Cont
Cont
Jump Instruction JMP Unconditional transfer to target destination PC n effective address JMP <ea> Type of effective address:
a. Absolute short b. Absolute long c. Address register indirect
Cont
JMP
$FF8000
0100 1110 1111 1001 0000 0000 1111 1111 1000 0000 0000 0000
Cont
If the test is true Z=1 Z=0 N=1 N=0 C=1 C=0 V=1 V=0 Z+(NV)=0 NV=1 NV=0 Z+(NV)=0 C+Z=0 C+Z=1
Cont
Cont
Cont
F T
Cont
VARA NEXT1
Action 1
BRA NEXT1
. .
NEXT
Action 2
NEXT
Cont
DBF
D7, LOOP
Cont
Cont
AGAIN LOOP
Action 2
Each BCD byte contents 2 digit of 4-bit BCD. Each BCD instruction involve in X bit. Z bit is changed if the product is not zero. Before doing the first BCD operation, it have to start with X=0 and Z=1. Syntax: ABCD Dx,Dy (source)10 + (destination)10 + X p destination ABCD- (Ay)-(Ax) NBCD <ea> 0 (source)10 X p destination SBCD Dx,Dy (destination)10 - (source)10 - X p destination
Cont
Example 2:
Subtract BCD number in D0 from D1 MOVE #4, CCR ; x=0 and z=1 SBCD D0, D1
Cont
Destination
A0 after addition
Source
A0 before addition
Cont
Example 1:
What is the 10s complement for fill-in D0. Let say D0 = 23.
MOVE #0,CCR NBCD D0 NBCD operation: Original D0: X bit: Product:
100 -23 - 0 77
(0 borrow 1)
Cont
Data structure that is used to save and restore information in a last-in, first-out (LIFO) fashion. Register A7 is implicitly used as the stack pointer. Push (save): MOVE source, -(SP) or MOVE.L source, -(SP) Pop (retrieve): MOVE (SP)+, destination or MOVE.L (SP)+, destination
Stack
Stack
STACKA DS.W MOVEA.L MOVE.L MOVE.W MOVE.W MOVE.L MOVE.L 50 #STACKA + 100, A2 D1, -(A2) VAR, -(A2) #$25, -(A2) (A2)+, D2 (A2)+, D3
Stack
Subroutine
Subroutine
Cont
Subroutine Concept A program structure where one part of the program is called the main program. In addition to this, a smaller segment attached to the main program, known as a subroutine. The subroutine is written to provide a function that must be performed at various points in the main program. A return instruction must be included at the end of the subroutine to initiate the return sequence to the main program environment. The instructions provided to transfer control from the main program to a subroutine and return control back to the main program are called subroutine-handling instructions.
Subroutine
Cont
Interrupt
An external event which informs the CPU that a device needs its service. Interrupt Service Routine (ISR):
a. a program which associates with interrupt. b. also called the interrupt handler c. there are 4 bytes of memory allocated as interrupt vector table. d. handled in the supervisor mode, S = 1.
Interrupt
Cont
Example 1:
Calculate the sum of 10 numbers in the address memory $2000.
extra