Title: Microprocessor Systems and Instrumentation
1- Microprocessor Systems and Instrumentation
- SOE2121
2- Microprocessor Systems and Instrumentation
- SOE2121
- Number Systems
3- Machine code
- 01010011010100101010010010101001010010010000111100
01010011001001010010100010001001010101001001001010
01010000101010101001011100010111000100101010001010
01000100101001010100100101000100010010100010101001
00101001010000101010101001011100010111000100101010
00101001000100101001010100100101000100010010100010
10100100101001010000101010101001011100010111000100
10101000101001000100101001010100100101000100010011
00100101000100010011001001010001000100110010010100
01000100110010010100010001001010001010100100101001
01000010101010100101110001011100010010101000101001
00010010100101010010010100010001001010001010100011
11100100101001001010010100001000100010010111
4- Machine code
- 01010001 01010010 10100100 10101001 01001001
00001111 00010100 11001001 01001010 00100010
01010101 00100100 10100101 00001010 10101001
01110001 01110001 00101010 00101001 00010010
10010101 00100101 00010001 00101000 10101001
00101001 01000010 10101010 01011100 01011100
01001010 10001010 01000100 10100101 01001001
01000100 01001010 00101010 01001010 01010000
10101010 10010111 00010111 00010010 10100010
10010001 00101001 01010010 01010001 00010011
00100101 00010001 00110010 01010001 00010011
00100101 00010001 00110010 01010001 00010010
10001010 10010010 10010100 00101010 10100101
11000101 11000100 10101000 10100100 01001010
5- Machine code
- DB 33 37 56 5E 4B 67 85 44 34 77 62 A3 D9 FF 03
74 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99
55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99
55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99
55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83
82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64
EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99
55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7
F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99
55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83
82 99 55 76 F5 E2 AB 72 97 43...
6- The Decimal System
- Decimal Number 38O7 3 x
1OOO ( 1O3) 3OOO
8 x 1OO ( 1O2) 8OO
O
x 1O ( 1O1) OO
7 x 1 ( 1OO)
7 ----
Total 38O7
7- The Binary System
- Binary Number 1O11
1 x 8 (
23) 8
O x 4 ( 22) O
1 x 2 (
21) 2
1 x 1 ( 2O) 1
--
Total 11
8- The Binary System
-
Bit value
128 64 32 16 8 4 2 1
Power of 2 27 26 25 24
23 22 21 2O - Number
1 0 0 1 0 1 1 0
9- The Decimal and Binary Systems
- Decimal Number 38O7 3 x
1OOO ( 1O3) 3OOO 8 x 1OO (
1O2) 8OO O x 1O ( 1O1) OO
7 x 1 ( 1OO) 7
----
Total 38O7 - Binary Number 1O11 1 x 8
( 23) 8 O x 4 ( 22)
O 1 x 2 ( 21) 2 1
x 1 ( 2O) 1
-- Total 11
10- The Hexadecimal System
- Hexadecimal Number 21AF (Base 16)
2 x 4O96 ( 163) 8192
1
x 256 ( 162) 256
A x 16 ( 161)
16O
F x 1 ( 16O) 15
---- Total
8623
11- Number Systems
-
- Decimal Numbers (Base 1O) 1O symbols
O
1 2 3 4 5 6 7 8 9
Binary Numbers (Base 2) 2
symbols
O 1
Hexadecimal
Numbers(Base 16) 16 symbols
O 1 2 3 4
5 6 7 8 9 A B C D E F
12- Brain Exercises (Neurobics)
-
- Binary counting on fingers (and toes
after ?) - Binary Arithmetic -
Addition -
Subtraction / Division
Multiplication
- Hexadecimal Arithmetic -
Addition -
Subtraction / Division
Multiplication
13- HEX BINARY DECIMAL
O OOOO
O 1 OOO1 1
2 OO1O 2
3 OO11 3 4
O1OO 4 5 O1O1
5 6 O11O 6
7 O111 7
8 1OOO 8 9
1OO1 9 A 1O1O
1O B 1O11 11
C 11OO 12
D 11O1 13 E
111O 14 F 1111
15
14- Converting between Binary and Hexadecimal
- Binary 0010 1100 328444
- Hex 2 C 321244
15- Converting between Binary and Hexadecimal
- 111O1OO111O1 111O 1OO1 11O1
E 9 D
12AB 1 2 A
B OOO1 OO1O 1O1O 1O11
16- Number conversions - Hex to Decimal
- Convert 1234 hex to decimal
- 163 162 161 160
- 4096 256 16 1
- 1 2 3 4
-
- 1 x 4096 4096
- 2 x 256 512
- 3 x 16 48
- 4 x 1 4
- -----
17- Number conversions - Decimal to Binary
- Convert 1234 decimal to binary
- 210 1024 - 1 1234 - 1024 210
- 29 512 - 0
- 28 256 - 0
- 27 128 - 1 210 - 128 82
- 26 64 - 1 82 - 64 18
- 25 32 - 0
- 24 16 - 1 18 - 16 2
- 23 8 - 0
- 22 4 - 0
- 21 2 - 1 2 - 2 0
- 20 1 - 0
- So 1234 decimal 100 1101 0010 binary
18- Number conversions - Binary to Hex
- eg binary number 11010011101011010
- First divide up FROM THE RIGHT into groups of 4
bits - 1 1010 0111 0101 1010
- Then use the hex table (or better your brain)
to - write the hex values
- 1 1010 0111 0101 1010
- 1 A 7 5 A
- Note learn 0-9 and remember A1010 and
C1100
19- HEX BINARY DECIMAL
O OOOO
O 1 OOO1 1
2 OO1O 2
3 OO11 3 4
O1OO 4 5 O1O1
5 6 O11O 6
7 O111 7
8 1OOO 8 9
1OO1 9 A 1O1O
1O B 1O11 11
C 11OO 12
D 11O1 13 E
111O 14 F 1111
15
20- Negative Numbers - Twos Complement form
- To form a negative number, for example -3, first
invert (ones complement) the positive number of
the same magnitude (3), then add one - 00000011 3
- invert to give
- 11111100 ones complement
- 00000001 add one
- --------
- 11111101 result is -3 (Hex FD)
21- Negative Numbers
-
- Most negative number
- -128
- Most positive number
- 127
- Note bit 7 carries the sign information
- 1negative 0positive
7
0
0
0
0
0
0
0
0
1
7
0
1
1
1
1
1
1
1
0
22- Negative Numbers
- 0111 1111 7F 127
- 0111 1110 7E 126
-
- 0000 0011 03 3
- 0000 0010 02 2
- 0000 0001 01 1
- 0000 0000 00 0
- 1111 1111 FF -1
- 1111 1110 FE -2
- 1111 1101 FD -3
-
- 1000 0001 81 -127
- 1000 0000 80 -128
-
23- Signed and Unsigned Arithmetic
- Why does it work for both unsigned numbers (0 to
255) and signed numbers (-128 to 127)? - Unsigned Data Signed
-
- 253 FD -3
- 1 01 1
- --- --- ---
- 254 FE -2
-
24- Binary Coded Decimal
- Each decimal digit is encoded as a 4 bit binary
number - O OOOO 1
OOO1 2 OO1O
3 OO11
4 O1OO 5
O1O1 6 O11O
7 O111
8 1OOO 9
1OO1 - eg decimal 42 0100 0010
- 4 2
- What is this value in Hexadecimal?
- (Note 42 in binary is 0010 1010
Hex 2A)
25- Binary Coded Decimal
- Range of BCD numbers in one byte 00 - 99
- Reading a BCD number in hex gives the decimal
value
26- HEX BINARY DECIMAL
O OOOO
O 1 OOO1 1
2 OO1O 2
3 OO11 3 4
O1OO 4 5 O1O1
5 6 O11O 6
7 O111 7
8 1OOO 8 9
1OO1 9 A 1O1O
1O B 1O11 11
C 11OO 12
D 11O1 13 E
111O 14 F 1111
15
27- ASCII Table
-
- MSD-gt0 1 2 3 4 5 6 7
- LSD
- 0 NUL DLE SP 0 _at_ P p
- 1 SOH DC1 ! 1 A Q a q
- 2 STX DC2 " 2 B R b r
- 3 ETX DC3 3 C S c s
- 4 EOT DC4 4 D T d t
- 5 ENQ NAK 5 E U e u
- 6 ACK SYN 6 F V f v
- 7 BEL ETB ' 7 G W g w
- 8 BS CAN ( 8 H X h x
- 9 HT EM ) 9 I Y i y
- A LF SUB J Z j z
- B VT ESC K k
- C FF FS , lt L l
- D CR GS - M m
- E SO RS . gt N n
28- The Byte
- 7 6 5 4 3 2 1 0
- 1 Byte 8 bits
- Contains one of 256 possible patterns
OOOOOOOO
OOOOOOO1
OOOOOO1O OOOOOO11
OOOOO1OO
11111111 - Range OO-FF
O to 255
29Most Significant Bit
Least Significant Bit
30- Which is Most Significant?
- A typical lecturers salary might be
- 93,878
Least Significant Digit
Most Significant Digit
31- Hexadecimal Representation
- Binary Hex Meaning Value
- 0100 0001 41 Unsigned Binary 65 Decimal
- 0100 0001 41 ASCII code A
- 0100 0001 41 BCD number 41 Decimal
- 1111 1111 FF Signed Binary -1 Decimal
- 1111 1111 FF Unsigned Binary 255 Decimal
- 1010 0101 A5 Opcode LDA
7 6 5 4 3 2 1 0
32- Brain Exercises (Neurobics)
-
- Binary counting on fingers (and toes
after ?) - Binary Arithmetic -
Addition -
Subtraction / Division
Multiplication
- Hexadecimal Arithmetic -
Addition -
Subtraction / Division
Multiplication
33- Introduction
- Load and Store
- Transfer
34- Not Recommended Book
- 65O2 Assembly Language Programming
- by L A Leventhal
- Pub Osborne/McGraw-Hill
35- Assembly Language
- Advantages
- Fastest possible program (on a particular
processor) - Smallest size program (cheaper ROM)
- Smallest RAM requirement (cheaper RAM)
Disadvantages - Much longer software development time
- Programs more difficult to debug
- Programs not portable
36- Assembly Language When is it used?
- MASS PRODUCTIONWhere the lowest possible
production cost is required and longer more
expensive software development time is
acceptable. eg microwave oven,
mobile phone - HIGH SPEED APPLICATIONSWhere a high level
language would not respond quickly enough.
eg high speed data acquisition system - Notes faster processors, critical bits in
assembler, smallest physical size - eg Pacemaker where power consumption
is also a consideration.
37- Microprocessor Systems and Instrumentation
- SOE2121
- Number Systems
38- Machine code
- 01010011010100101010010010101001010010010000111100
01010011001001010010100010001001010101001001001010
01010000101010101001011100010111000100101010001010
01000100101001010100100101000100010010100010101001
00101001010000101010101001011100010111000100101010
00101001000100101001010100100101000100010010100010
10100100101001010000101010101001011100010111000100
10101000101001000100101001010100100101000100010011
00100101000100010011001001010001000100110010010100
01000100110010010100010001001010001010100100101001
01000010101010100101110001011100010010101000101001
00010010100101010010010100010001001010001010100011
11100100101001001010010100001000100010010111
39- Machine code
- 01010001 01010010 10100100 10101001 01001001
00001111 00010100 11001001 01001010 00100010
01010101 00100100 10100101 00001010 10101001
01110001 01110001 00101010 00101001 00010010
10010101 00100101 00010001 00101000 10101001
00101001 01000010 10101010 01011100 01011100
01001010 10001010 01000100 10100101 01001001
01000100 01001010 00101010 01001010 01010000
10101010 10010111 00010111 00010010 10100010
10010001 00101001 01010010 01010001 00010011
00100101 00010001 00110010 01010001 00010011
00100101 00010001 00110010 01010001 00010010
10001010 10010010 10010100 00101010 10100101
11000101 11000100 10101000 10100100 01001010
40- Machine code
- DB 33 37 56 5E 4B 67 85 44 34 77 62 A3 D9 FF 03
74 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99
55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99
55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99
55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83
82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64
EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99
55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7
F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99
55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83
82 99 55 76 F5 E2 AB 72 97 43...
41- HEX BINARY DECIMAL
O OOOO
O 1 OOO1 1
2 OO1O 2
3 OO11 3 4
O1OO 4 5 O1O1
5 6 O11O 6
7 O111 7
8 1OOO 8 9
1OO1 9 A 1O1O
1O B 1O11 11
C 11OO 12
D 11O1 13 E
111O 14 F 1111
15
42- The Byte
- 7 6 5 4 3 2 1 0
- 1 Byte 8 bits
- Contains one of 256 possible patterns
OOOOOOOO
OOOOOOO1
OOOOOO1O OOOOOO11
OOOOO1OO
11111111 - Range OO-FF
O to 255
43Most Significant Bit
Least Significant Bit
44- Which is Most Significant?
- A typical lecturers salary might be
- 93,878
Least Significant Digit
Most Significant Digit
45- The Byte
- 7 6 5 4 3 2 1 0
- 1 Byte 8 bits
- Contains one of 256 possible patterns
OOOOOOOO
OOOOOOO1
OOOOOO1O OOOOOO11
OOOOO1OO
11111111 - Range OO-FF
O to 255
46- Hexadecimal Representation
- Binary Hex Meaning Value
- 0100 0001 41 Unsigned Binary 65 Decimal
- 0100 0001 41 ASCII code A
- 0100 0001 41 BCD number 41 Decimal
- 1111 1111 FF Signed Binary -1 Decimal
- 1111 1111 FF Unsigned Binary 255 Decimal
- 1010 0101 A5 Opcode LDA
7 6 5 4 3 2 1 0
47Memory
7 0
Microprocessor
FFFF 0200 01FF 0100 00FF 0000
7 0
A
X
Y
15 8
PC
00000001
SP
NV-BDIZC
STACK
PS
PAGE ZERO
48- Memory
-
- 65536 bytes numbered
- in decimal O - 65535
in hex OOOO - FFFF
in binary OOOO OOOO OOOO OOOO - 1111
1111 1111 1111
- The number of each memory location is known as
its ADDRESS
2 Bytes are required to hold an
address which is 16 bits
49One byte instructions
OPCODE
Two byte instructions
OPCODE
OPERAND
Three byte instructions
OPCODE
OPER
AND
50- Load and Store Instructions
51- The LDA Instruction
- Copy the contents of memory location 8O into
- the A register
LDA 8O
This
instruction uses zero page addressing since
address 8O is on page zero
-
52- The LDA Instruction and the Instruction Set Sheet
- Assembly Language LDA 8O Machine
Code A5 8O
Instruction Set Sheet Entry
A5 3
Opcode A5 Time 3
clock cycles Length 2 bytes
53- The LDA Instruction and the Instruction Set Sheet
- ----------------------------------------
----- Immed ABS ZP
Effect on Operation
len2len3len2the flags
OP nOP nOP n NV-BDIZC
------------------------------------------
---- - LDA A M A9 2AD 4A5 3
N.....Z. -
- Assembly Language LDA 8O Machine
Code A5 8O
Instruction Set Sheet Entry
A5 3
Opcode A5 Time 3
clock cycles Length 2 bytes
54- The LDA Instruction and the Instruction Set Sheet
- Assembly Language LDA 8O Machine
Code A5 8O Instruction Set
Sheet Entry A5 3 Opcode A5 Time 3 clock
cycles Length 2 bytes - Effect on the Flags N.....Z.
- N and Z changed depending on the value that is
loaded - The N flag
becomes the sign bit (7) of the value loaded
The Z flag is
1 if the value loaded is zero The Z flag is O
if the value loaded is not zero
55- Load and Store instructions
- Absolute Zero Page
- LDA 1234 LDA 8O
- STA 1234 STA 8O
LDX 1234 LDX 8O
STX 1234 STX 8O
LDY 1234 LDY 8O
STY 1234 STY 8O
56- Immediate Addressing
- Instruction Machine code
LDA AA
A9 AA
LDX FF A2 FF
LDY
O1 AO O1
57- Dont mix these up!
- Zero Page LDA OO
Immediate LDA OO
58 59- Transfer instructions
- TAX Copy register A to register X
The old value in X is lost Register A is not
affected
60- Transfer instructions
- TAX TAY TXA TYA
- (TXS TSX)
-
- All 1 byte Long
Implied Addressing 2
Clock Cycles Flags as
for LDA
61- Arithmetic
- Instructions
- ADC SBC
62 63- Negative Numbers - Twos Complement form
- To form a negative number, for example -3, first
invert (ones complement) the positive number of
the same magnitude (3), then add one - 00000011 3
- invert to give
- 11111100 ones complement
- 00000001 add one
- --------
- 11111101 result is -3 (Hex FD)
64- Negative Numbers
-
- Most negative number
- -128
- Most positive number
- 127
- Note bit 7 carries the sign information
- 1negative 0positive
7
0
0
0
0
0
0
0
0
1
7
0
1
1
1
1
1
1
1
0
65- Signed and Unsigned Arithmetic
- Why does it work for both unsigned numbers (0 to
255) and signed numbers (-128 to 127)? - Unsigned Data Signed
-
- 253 FD -3
- 1 01 1
- --- --- ---
- 254 FE -2
-
66 67- Addition
- CLC
- LDA 8O
ADC 81
STA 82
68- Two byte arithmetic
- How do we do it in decimal?
- 36
- 25
- ---
- 61
69- Addition and Subtraction
- CLC SEC
LDA 8O
LDA 8O
ADC 81 SBC 81
STA
82 STA 82
70First Number
Second Number
Result
LSB
LSB
LSB
MSB
MSB
MSB
80
81
82
83
84
85
Addition
Subtraction
CLC SEC LDA 8O LSBs LDA
8O LSBs ADC 82 SBC 82
STA 84 STA 84 LDA
81 MSBs LDA 81 MSBs ADC 83
SBC 83 STA 85
STA 85
71 72Memory
7 0
Microprocessor
FFFF 0200 01FF 0100 00FF 0000
7 0
A
X
Y
15 8
PC
00000001
SP
NV-BDIZC
STACK
PS
PAGE ZERO
73- Decimal Arithmetic (BCD)
-
-
-
- SED SED CLC
SEC LDA 8O
LDA 8O ADC 81 SBC 81
STA 82 STA 82
CLD CLD
Addition
Subtraction
74 75 76- Logical Operations
And
AND
77- Logical Operations
And
AND Or ORA
78- Logical Operations
And
AND Or ORA
Exclusive Or EOR
79 80- The AND Instruction
LDA A9 1O1O 1OO1
(A) AND OF OOOO 1111 (M)
---------
Result OOOO 1OO1
Any O in the mask will clear the
corresponding bit in A to O - Any 1 in
the mask will leave the corresponding bit in A
unchanged
81 82- The ORA Instruction
LDA A9 1O1O 1OO1
(A) ORA OF OOOO 1111 (M)
---------
Result 1010 1111
Any 1 in the mask will set the corresponding bit
in A - to 1
- Any 0 in
the mask will leave the corresponding bit in A
unchanged
83 84- The EOR Instruction
LDA A9 1O1O 1OO1
(A) EOR OF OOOO 1111 (M)
---------
Result 1010 0110
Any 1 in the mask will invert the corresponding
bit in A - (so 1 becomes 0 and 0 becomes 1)
- Any 0 in
the mask will leave the corresponding bit in A
unchanged
85- Testing individual bits in a byte
86- Testing Individual Bits in a Byte To test
bit 2 of location 8O
LDA 8O byte to test
AND O4 mask OOOO O1OO
87- The Byte
- 7 6 5 4 3 2 1 0
-
- 0 0 0 0 0 1 0 0
- Hex 04
-
?
88- Testing Individual Bits in a Byte eg - to
test bit 2 of location 8O
LDA 8O byte to
test AND O4 mask OOOO O1OO
89- Testing Individual Bits in a Byte eg - to
test bit 2 of location 8O
LDA 8O byte to
test AND O4 mask OOOO O1OO
8O contains B7
A 1O11 O111
M OOOO O1OO ---------
OOOO O1OO
Z flag O
90- Testing Individual Bits in a Byte eg - to
test bit 2 of location 8O
LDA 8O byte to
test AND O4 mask OOOO O1OO
8O contains B7 8O contains 69
A
1O11 O111 A O11O 1OO1 M OOOO O1OO
M OOOO O1OO ---------
--------- OOOO O1OO OOOO OOOO
Z flag O Z flag 1
91- The BIT Instruction (this is rather
a funny instruction)
eg LDA O1 mask
for AND BIT 8O byte to test
-
-
- Z is set by the result of the AND
1OO1 1OO1 OOOO OOO1
---------
OOOO OOO1 ie non-zero so
Z O
7 6 5 4 3 2 1 0
1
0
0
1
1
0
0
1
N V
92- Logical instructions - Example
- LDA 33 Result is OO11 OO11 33
AND OF
ORA 9O
EOR 8O
EOR FF
93- Logical instructions - Example
- LDA 33 Result is OO11 OO11 33
AND OF
Result is OOOO OO11 O3
ORA 9O
EOR 8O
EOR FF
94- Logical instructions - Example
- LDA 33 Result is OO11 OO11 33
AND OF
Result is OOOO OO11 O3
ORA 9O Result is 1OO1
OO11 93
EOR 8O
EOR FF
95- Logical instructions - Example
- LDA 33 Result is OO11 OO11 33
AND OF
Result is OOOO OO11 O3
ORA 9O Result is 1OO1
OO11 93
EOR 8O Result is OOO1 OO11 13
EOR FF
96- Logical instructions - Example
- LDA 33 Result is OO11 OO11 33
AND OF
Result is OOOO OO11 O3
ORA 9O Result is 1OO1
OO11 93
EOR 8O Result is OOO1 OO11 13
EOR FF
Result is 111O 11OO EC
97- Shift and Rotate Instructions
98- Shift and Rotate Instructions
ASL
99- Shift and Rotate Instructions
ASL LSR
100- Shift and Rotate Instructions
ASL LSR ROL
101- Shift and Rotate Instructions
ASL LSR ROL ROR
102- The ASL instruction The bits in the
byte are shifted one position to the left
A zero is shifted into bit O Bit 7 is
shifted out into the carry flag
-
- eg ASL 42 ASL 21OF
ASL A New Addressing
Mode!
C
7
0
0
103- The LSR instruction The bits in the
byte are shifted one position to the right
A zero is shifted into bit 7 Bit 0 is
shifted out into the carry flag
-
- eg LSR 42 LSR 21OF
LSR A
C
7
0
0
104C
7
0
0
0
1
0
0
0
0
0
0
105C
7
0
0
0
0
1
0
0
0
0
0
106C
7
0
0
0
0
0
1
0
0
0
0
107C
7
0
0
0
0
0
0
1
0
0
0
108C
7
0
0
0
0
0
0
0
1
0
0
109C
7
0
0
0
0
0
0
0
0
1
0
110C
7
0
0
0
0
0
0
0
0
0
1
111C
7
0
1
0
0
0
0
0
0
0
0
112C
7
0
0
0
0
0
0
0
0
0
0
113- The ROL instruction The bits in the
byte are shifted one position to the left
The original value of the carry is shifted
into bit O Bit 7 is shifted out into
the carry flag
-
-
C
7
0
114- The ROR instruction The bits in the
byte are shifted one position to the right
The original value of the carry is shifted
into bit 7 Bit 0 is shifted out into
the carry flag
-
-
C
7
0
115- eg
LSR 80 msb - ROR 81 lsb
- 80
81 -
-
-
-
-
-
-
2 Byte Shift
7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
1
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
0
C
1
116- Jumping and Branching and Comparing
-
-
-
117- Unconditional Jump - JMP
-
O25O CLC O251 LDA
4O O253 ADC 41
O255 STA 42
--O257 JMP O25E
O25A LDA 8O O25C STA
81 --O25E LDA FOFA
O261 STA 35
118- Conditional Branch Instructions
- BNE
Branch if not equal to zero Z OBEQ Branch if
equal to zero Z 1
BCC Branch if carry clear
C OBCS Branch if carry set C 1
BPL
Branch if plus (positive) N OBMI Branch
if minus (negative) N 1
(BVC Branch if overflow
clear V O)(BVS Branch if overflow set
V 1)
119- Conditional Branch Instructions
- O25O LDA 8O
--O252 BNE O256
O254 LDA 81 --O256 STA
82 O258 BRK
O25O A5 (LDA) -4 FC
O251 8O -3 FD O252
DO (BNE) -2 FE O253 O2
-1 FF O254 A5 (LDA) O
OO O255 81 1 O1
O256 85 (LDA) 2 O2 O257
82 3 O3 O258 OO (BRK)
4 O4
120- Delay Loop 10s
- O4OO LDA 12 12 18 decimal
O4O2 STA 82 O4O4 LDA
OO O4O6 STA 8O
O4O8 STA 81
O4OA DEC 8O O4OC
BNE O4OA inner loop O4OE DEC 81
O41O BNE O4OA next
loop O412 DEC 82
O414 BNE O4OA outer loop O416
RTS
121CMP CPX CPY
122- Compare Instructions
CMP
CPX CPY - Examples
- CMP 8O CPX 8O CPY 8O CMP
1234 CPX 1234 CPY 1234 CMP O3
CPX O3 CPY O3
The Z and C flags are changed after a
compare instruction as follows
A M
then Z 1 (BEQ will branch) A ltgt M then
Z O (BNE will branch)
A gt M then C 1 (BCS will
branch) A lt M then C O (BCC will
branch)
123- To compare the contents of 5O and 51
- O25O LDA 5O first number O252
CMP 51 second number O254 BEQ O26O
branch if firstsecond O256 BCS O27O branch if
firstgtsecond O258 BCC O28O branch if
firstltsecond O25A
O26O
here if firstsecond
O27O here if
firstgtsecond
O28O here if firstltsecond
124- Loops and Indexed addressing
125- Incrementing
- and
- Decrementing
126- Incrementing and Decrementing
Memory Locations
INC 8O DEC 2F
INC O412
DEC 12BA Registers
INX DEX
INY DEY
127- Car Mileometer
- What happens if you drive forward one more
mile?
9
9
9
9
9
128- Car Mileometer
- What happens if you reverse one mile?
0
0
0
0
0
129- Decrementing X
- Instruction Value of X after
instruction - 04 DEX
O3 DEX O2
DEX O1
DEX OO (Z flag
set) DEX FF
DEX FE
DEX FD
130- Incrementing X
- Instruction Value of X after
instruction - FC INX
FD INX FE
INX FF
INX OO (Z flag
set) INX 01
INX 02
INX 03
131 132- Loop Example add the contents of
locations 80 to 85 -
O25O LDA
OO A will hold sum O252 LDX OO X -
loop counter O254 CLC
-O255 ADC 8O,X Add next byte
O257 INX O258 CPX
O6 Have we finished? -O25A BNE O255 If
not continue O25C STA 8A Store the sum
O25E BRK
80 81 82 83 84 85
86 87
133 134- Indexed Addressing
-
Zero Page,X ADC 8O,X
Zero Page,Y LDX 8O,Y
Absolute,X
LDA 123A,X
Absolute,Y LDA 456B,Y
135- Delay Loop 10ms
O3OO LDY OAouter loop 1O times O3O2 LDX
C8inner loop 2OO times O3O4 DEX
O3O5 BNE O3O4
O3O7 DEY O3O8
BNE O3O2 O3OA RTS
136- Delay Loop 10s
- O4OO LDA 12 12 18 decimal
O4O2 STA 82 O4O4 LDA
OO O4O6 STA 8O
O4O8 STA 81
O4OA DEC 8O O4OC
BNE O4OA inner loop O4OE DEC 81
O41O BNE O4OA next
loop O412 DEC 82
O414 BNE O4OA outer loop O416
RTS
137- The NOP Instruction
- NOP does nothing
- 1 byte 2 clock cycles
138- Subroutines and the Stack
139- Subroutines
- 0200 LDA 94
- AND 07
- JSR 0500
- 0207 LDA 94 0500 LDA 80
- ORA 80 AND 0F
- JSR 0500 RTS
- 020E LDX 08
- JSR 0500
- 0213 STA 60,X
- DEX
-
140 14101FF
A1
01FE
35
SP
01
FF
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
142- Push and Pull Instructions
-
- Register A
- PHA store on stack
- PLA get from stack
- Flag Register
- PHP store on stack
- PLP get from stack
143SP
01
FF
LDA 30 STA 51 CLC ADC 42 LSR A JSR 0400 CMP
05 BNE 0294 JMP 0280 LDA 50 ADC 42 LSR A CMP
05 BNE 0294 JMP 0280 LDA 50 STA 62 ROR 7E LDA
30 STA 51 CLC ADC 42 LSR A CMP 05 BNE 0294 JMP
0280 LDA 50 STA 62
0400
0250
LDA 30 STA 51 CLC ADC 42 LSR A JSR 0500 STA
9A LSR A STA 3B RTS
01FF
AA
01FE
F3
01FD
0262
29
0413
01FC
81
01FB
12
01FA
7B
0500
LSR A STA 6F STA 77 RTS
01F9
5D
Before First JSR
14401FF
A1
01FE
35
SP
01
FF
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
145- The Stack
- Store the value AA on the stack
-
01FF
A1
01FE
35
SP
01
FF
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
146- The Stack
- Store the value AA on the stack
-
01FF
AA
01FE
35
SP
01
FE
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
147- The Stack
- Store the value F3 on the stack
-
01FF
AA
01FE
35
SP
01
FE
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
148- The Stack
- Store the value F3 on the stack
-
01FF
AA
01FE
F3
SP
01
FD
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
149- The Stack
- The Stack pointer always contains the
- address of the next free location
01FF
AA
01FE
F3
SP
01
FD
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
150- The Stack
- Get a byte from the stack
01FF
AA
01FE
F3
SP
01
FD
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
151- The Stack
- Get a byte from the stack
- The Stack Pointer is first
- incremented to point to the
- last value stored
01FF
AA
01FE
F3
SP
01
FE
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
152- The Stack
- Get a byte from the stack
- We get the value F3 -
- the last value stored
- 01FE is now the next free location
01FF
AA
01FE
F3
SP
01
FE
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
0100
153- What happens when a subroutine is called before
JSR - 0260 JSR 0500
- 0263 LDA 00
- 0500 LDX 03
- 0502 TAY
- 0503 RTS
01FF
AA
01FE
F3
SP
01
FF
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
154- What happens when a subroutine is called after
JSR - 0260 JSR 0500
- 0263 LDA 00
- 0500 LDX 03
- 0502 TAY
- 0503 RTS
01FF
02
01FE
62
SP
01
FD
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
155- What happens when a subroutine is called after
RTS - 0260 JSR 0500
- 0263 LDA 00
- 0500 LDX 03
- 0502 TAY
- 0503 RTS
01FF
02
01FE
62
SP
01
FF
01FD
29
01FC
81
01FB
12
01FA
7B
01F9
5D
156 157- Interrupts
- Main Program LDA 30
- STA 51
- CLC
- ADC 42
- LSR A
- CMP 05
- BNE 0294
- JMP 0280 PHA
- LDA 50 LDA 33
- STA 62 STA 51
- ROR 7E LDA A000
- LDA 30 STA 80
- STA 51 LDA A001
- CLC STA 81
- ADC 42 INC 9A
- LSR A DEC 9B
Interrupt Routine
Interrupt occurs here
When an interrupt occurs, the return address is
stored on the stack, in the same way as for a
subroutine call. The contents of the flag
register is also stored on the stack.
158 5v
10K
___ IRQ ___ NMI ___ RES 6502 microprocessor
0v
159- 6502 Vectors
- ___
- NMI FFFA FFFB ___ RES
FFFC FFFD - ___