You are on page 1of 94

Microprocessors (1)

Microprocessor

A microprocessor is an electronic device that


consists of millions (or billions) of transistors
packed into one IC.
The microprocessor contains three basic
components:
Data

Registers (Dn).
Address Registers (An)
Arithmetic-Logic Unit (ALU).

Inside the Microprocessor

Data Register: Used to store data for


calculations.
Address Register: Used to store memory
addresses that the microprocessor refers to.
Arithmetic-Logic Unit (ALU): Is the processing
unit that takes data and processes it. It can
perform a variety of operations:
Data

movement (MOVE).
Arithmetic (ADD, SUB, MUL, DIV).
Logic (AND, OR, NOT, EOR)

Microprocessor
The microprocessor can be used to
perform complex operations by giving it
instructions.
These instructions are called programs.
Programs are loaded into memory, and
are executed line-by line by the
microprocessor.

Example Program

Microprocessor
Systems

Memory Address
Each memory location is always given a
unique address.
The address is usually represented using
a hexadecimal number.
The microprocessor refers to this address
when it wants to take data/instruction from
memory.

Memory Address
Address

Data

$000000

$03

$000001

$AB

$000002

$11

$000003
$000004

$FFFFFE
$FFFFFF

1 memory location can store


1 byte (8 bits) of data.

What the CPU Does


Parallel I/O

Serial I/O

Interrupt
Circuit

System Bus

Timing

CPU

Memory

Take instruction from memory

What the CPU Does


Parallel I/O

Serial I/O

Interrupt
Circuit

System Bus

Timing

CPU

Memory

Take required data from memory

What the CPU Does


Parallel I/O

Serial I/O

Interrupt
Circuit

System Bus

Timing

CPU
Execute Instruction

Memory

What the CPU Does


Parallel I/O

Serial I/O

Interrupt
Circuit

System Bus

Timing

CPU

Memory

Save data back into memory

The M68000
We will be using a specific microprocessor
called the M68k.
It is a 16-bit microprocessor manufactured
by Motorola in 1979.
Its speed is around 8-12 MHz.
Still being used today.

The M68k Microprocessor

M68k Programmer Model


MSB
31

16,15

8,7

MSB
31

LSB
0
D0
D1
D2
D3

DATA
REGISTERS

MSB

ADDRESS
REGISTERS

16,15

A7 (USP)
USER STACK POINTER
31

0
PC

D4
D5
D6
D7
31

LSB
0

PROGRAM COUNTER
0

CCR
16,15

LSB
A0
A1
A2
A3
A4
A5
A6

CONDITION CODE REGISTER


31

16,15

0
A7 (SSP)

SUPERVISOR STACK POINTER


15

8,7
CCR

STATUS REGISTER

SR

How Data Registers Work - Example


MEMORY

DATA REGISTERS (IN M68K)

Add.

Contents

D0

$12340000

D1

$00005678

Contents

D2
D3
D4

M68k wants to add


together contents of
A and C.

D5

D6

D7

How Data Registers Work - Example


DATA REGISTERS (IN M68K)

MEMORY

Contents

Add.

Contents

D0

$12340000

$12340000

D1

$00005678

D2

$00005678

D3

D4

D5

D6
D7

Step 1: M68k loads data


from memory locations into
data registers.

G
H

How Data Registers Work - Example


DATA REGISTERS (IN M68K)

MEMORY
Add.

Contents

$12340000

D2

$00005678

D3

D4

Contents
D0

$12345678

D1

$00005678

D5
D6
D7

$12340000 + $00005678 = $12345678

Step 2: M68k adds together


the numbers and stores them
in register. Result stored in D0.

F
G
H

Addressing Modes

Introduction

All CPU instructions have similar requirements:


Action

- What action should it perform?


Source - Where is the data that it is supposed to
process?
Target - Where should it put the results?

Action is done using instructions.


Addressing modes identify source and target.

Addressing Modes

Methods to access data inside:


CPU

registers.
Memory.

M68k allows 14 addressing modes:


Direct

reference.
Indirect reference.

Addressing modes allow flexible & effective


programs design.

Address Register Indirect


ARI + PostIncrement
Register Addressing Mode

ARI + Predecrement
ARI + Displacement

Data Register Direct


Address Register Direct

ARI + Index
Absolute Short Address
Absolute Long Address

AM

PC with Displacement
PC with Index
Mem. Addressing Mode

Immediate Data
Quick Immediate Data
Implied Addressing

Register Addressing
Modes

Register Addressing Modes

Modes to access registers in M68k:


Data

Register.
Address Register.

Consists of 2 methods:
Data

Register Direct (DRD).


Address Register Direct (ARD).

DRD (Data Register Direct)


Used to access data registers.
Represented to as Dn:

represents data register.


n is register number.
From D0 to D7.

DRD Example
MOVE.B
ADD.W
MULU

D0,D1
D4,(A0)
D5,D7

ARD (Address Register Direct)


Used to access address registers.
Referred to as An:

A represents

address register.
n is register number.
From A0 to A7.

A7 = stack pointer.

ARD Example
MOVEA.L
ADDA.W
MOVEA.W

A0,A4
A3,A6
#$1234,A2

Memory Addressing
Modes

Memory Addressing Modes


Modes to access memory locations.
12/14:

Memory

space is large area.


Many varieties of addressing modes.
Depends on desired function.

ARI (Address Register Indirect)

Refers to contents of memory location


pointed by An.

Address register enclosed in parenthesis


(An).

Example: ARI
D0 = $12345678
A1 = $007A92
MOVE.B
D0,(A1)
(This command does not move the data
inside D0 into A1, but moves the data
inside D0 into the memory location pointed
by A1).

Example: ARI
D0 = $12345678
A1 = $00007A92
MOVE.L
D0,(A1)

D0.L = $12345678
A1 = $007A92 (A1 is still unchanged).

Memory Contents
$7A90
$7A91
$7A92

$12

$7A93

$34

$7A94

$56

$7A95

$78

Example: ARI

MOVE.B

D1,(A4)

Moves

a byte from D1 into the memory


location specified by A4.

ADD.W
Adds

(A3),D3

the word content of memory address


specified by A3 to data register D3.

ARI + PI (Address Register Indirect


with Post-Increment)
Same as ARI, but An automatically
incremented after execution (postincrement).
Use the + sign after (A n) = (An)+

Useful in for loops.

ARI + PI (Address Register Indirect


with Post-Increment)

Increment value depends on data length:


If

.B is used, An is incremented by 1.

If

.W is used, An is incremented by 2.

If

.L is used, An is incremented by 4.

Example 1: ARI + PI
D0 = $12345678
A1 = $001002
MOVE.W
D0,(A1)+

D0.W = $5678

Memory Content
$1001

A1 (new)

After execution, A1 is incremented


by 2 since .W was used.
A1 = $1002 + 2 = $001004 (new value).

$1002

$56

$1003

$78

$1004
$1005
$1006

ARI + PD (Address Register


Indirect with Pre-Decrement)
Same as ARI, but value in address
register automatically decremented before
execution (pre-decrement).
Use the - before (An) sign -(An)
Useful to push data to stack.

ARI + PD (Address Register


Indirect with Pre-Decrement)

The decrement value depends on data


length:
If

.B is used, An is decremented by 1.

If

.W is used, An is decremented by 2.

If

.L is used, An is decremented by 4.

Example: ARI + PD Moving Data


to Stack
D0 = $12345678
A7 = $001002
MOVE.B
D0,-(A7)

Before execution, A7 is decremented


by 1 since .B was used.

Memory Contents

A7 (new)

$1001

A7 (old)

$1002

A7 = $1002 - 1 = $001001 (new value).

$1003
$1004

D0.B = $78

$1005
$1006

$78

ARI + D (Address Register


Indirect with Displacement)
Adds a displacement value to ARI.
Format: d(An)

An
d

is address register.

is 16-bit displacement value.

-32,768 ($8000) d 32,767 ($7FFF)


$8000

= %1000 0000 0000 0000 = -32768


$7FFF = %0111 1111 1111 1111 = 32767

Example: ARI + D
D3 = $12345678
A4 = $004500

MOVE.B

Memory Contents
A4

D3, $750(A4)

A4=$004500, Disp.=$750
***$750 = %0111 0101 0000
Effective Address (EA):
A4:
$004500
D:
$000750 +
EA: $004C50

$4500
$4501

+$750

$4C50
$4C51

$78

Example: ARI + D
D3 = $00000000
A4 = $004500

MOVE.B

Memory Contents

$FFF3(A4),D3

$FFF3 = %1111 1111 1111 0011


$FFF3 is negative (MSB = 1)
2s complement = $000D

Effective Address (EA)


A4:
$004500
D:
$00000D
EA: $0044F3

-$000D
A4

$44F1

$11

$44F2

$22

$44F3

$33

..

..

$44FF

$55

$4500

$66

New value of D3 = $00000033

ARI + D Example
MOVE.B
ADD.B

D1,34(A0)
$1254(A2),D4

Displacement must not be more


than 16-bits long.

Example: ARI + D
D3 = $00000000
A4 = $004500

MOVE.B

-5(A4),D3

Memory Contents
$44F9
$44FA
$44FB

$14

$44FC
$44FD

Effective Address:
A4
$004500
Disp. $000005
$0044FB

$44FE

D3 = $00000014

ARI + I (Address Register


Indirect with Index)

Similar to ARI + D, but adds another index term.


Displacement range $80 (-128) < D < $7F (127).
Index term from Dn or An.
Used for implementing 2-D arrays.
Adds index term into bracket:
D(An,

Dn.W)
D(An, An.W)

Effective Address is ARI + D + Index (Dn.W or


An.W).

ARI + I Example
MOVE.B
ADD.B

D1,34(A0,D3.W)
$54(A2,A4.W),D4

Displacement must be 8-bits long.


Index must be 16-bits long.

ARI + I Example
D1 = $00000012
A4 = $00001000
MOVE.B
#$FF,$78(A4,D1.W)
Effective Address (ARI + D + I):
A4

+
+

ARI $00001000
D1.W $
0012
D
$
78
EA

$0000108A

$1000

$108A
$108B
$108C
$108D
$108E

$FF

ALA (Absolute Long Address)


Directly addresses memory locations.
Address must be 24-bits.
No sign extension performed.
Slower than ASA, requires more machine
code.

ALA Example
MOVE.L
ADD.B
MOVE.B

D0,$100100
$001000,D3
$000100,$400400

Example: ALA

MOVE.B
Moves

$001000,D0

byte content of memory address $1000

to D0.

ADD.W $400400,D1
Adds

the word content of $400400 to D1.

MOVE.L
Moves

D4,$003000
a long-word from D4 to address $3000.
*Address length must always be 24-bits

Absolute Long Address


D1 = $00000000
MOVE.W
$001000,D1
Memory
$1000

$12

$1001

$34

$1002

$FF

$1003

$56

$1004

$AA

$1005

$AC

$1006

$12

D1 =

0 0 0 0 1 2 3 4

Absolute Short Address

Used to directly address data in two memory


ranges:
$000000

to $007FFF.
$FF8000 to $FFFFFF.

Only specify 16-bit address, automatically signextended to 24-bits.


Requires less machine code than ALA.
Be careful with sign-extension.

Example: Absolute Short


Address

SUB.B $4601,D4
4
6
0
1
0100 0110 0000 0001
MSB is 0

0
0
4
6
0
0000 0000 0100 0110 0000 0001

sign-extended to 24-bits

E.A. is $004601
Will subtract the byte value in address $004601 from D4

Example: Absolute Short


Address

MOVE.B

$8432,D3

8
4
3
2
1000 0100 0011 0010
MSB is 1

F
F
8
4
3
2
1111 1111 1000 0100 0011 0010
sign-extended to 24-bits

E.A. is $FF8432
Will move byte value from address $FF8432 to D3.

ASA vs. ALA


MOVE.B $8432,D3
and
MOVE.B $FF8432,D3
are equal, but MOVE.B $8432,D3 executes faster &
uses less memory space.

PC + D (Program Counter with


Displacement)
Similar to ARI + D, but An replaced with
PC.
Allows flexible program placement:

Can

be put anywhere in memory.


Address referred relative to PC.
Would still run even with different starting
addresses.

Program Counter with


Displacement

Format: d(PC)
PC

is program counter.
d is 16-bit displacement value.

16 bit displacement:

-32,768 ($8000) d 32,767 ($7FFF)

Displacement must be properly calculated


during program design.

Why PC + D allows flexible


addressing?
Data #1
Data #2
Data #2 is located 2 memory
locations above Instruction #1

Data #3

Data #1 is located 3 memory


locations above Instruction #1

Instruction #1
Instruction #2
Instruction #3

Instruction #n
In PC + D, the location of data is always
stated as relative to instruction.

Why PC + D allows flexible


addressing?
Data #1
Data #2
Data #3
Instruction #1
Instruction #2

PC

Instruction #3

Instruction #n
When the program is run, PC is incremented
as instructions are executed.

Why PC + D allows flexible


addressing?
Data #1
Data #2
Data #2 is located 2 memory
locations above PC

Data #3
Instruction #1
Instruction #2
Instruction #3

Instruction #n
When program running,
data location referred to PC.

Data #1 is located 3 memory


locations above PC

Wherever you put the program, PC-relative addressing


always refers to the right location.
Address Space

Address Space

Address Space

Data #1
Data #2
Data #3

Data #1 is located 3 memory


locations above PC

Instruction #1
Instruction #2

Data #1

Instruction #3

Data #2

Data #3

Instruction #1

Instruction #n

Instruction #2

Data #1

Instruction #3

Data #2

Data #3

Instruction #1

Instruction #n

Instruction #2

Data #1 is located 3 memory


locations above PC

Data #1 is located
3 memory
locations above PC

Instruction #3

Instruction #n

*Assuming PC @ Instruction #1

PC + D Example
PC = $004000
D1 = $12345678
MOVE.B
$32(PC),D1
Memory

Effective Address:
PC =
+ D =

$004000
$
32

EA =

$004032

$4031
$4032
$4033
$4034
$4035
$4036

$12
$23
34
$45
$56
$67

D1 =

1 2 3 4 5 6 2 3

PC + I (Program Counter with


Index)
Similar to ARI + I, but An replaced with PC.
Allows similar flexibility as PC + D.
Format:

d(PC,Dn)
d(PC,

An)
PC is program counter.
d is 8-bit displacement value.

PC + I (Program Counter with


Index)

8-bit displacement:

-128 ($80) d 127 ($7F)

Displacement must be properly calculated


during program design.

PC + I Example
D1 = $00000388
PC = $00003000
MOVE.B
#$A2,$FA(PC,D1.W)

*$FA = -6 (2s complement)

Effective Address (PC + D + I):


$3380

+
-

PC
$00003000
D1.W $
0388
D
$
06

$3381

EA

$3385

$00003382

$3382
$3383
$3384
$3386

$A2

Immediate Data
Used to transfer constant values into
registers/memory locations.
Consists of 2 parts:

The

constant value.
The register/memory location to store it in.

Symbol # must be put in front of the


constant value.

Types of Constant
Symbol

Data Type

Example

Binary

#%01101010

Octal

#@123

<none>

Decimal

#45

Hexadecimal

#$35

Character

#B

Example: Moving Decimal Value to Data Register

D0 = $FFFFFFFF
MOVE.B
#12,D0
Constant value: #12 = #$0C
D0 =

Final D0 = F

Example: Moving Hex Value to Memory


MOVE.W
#$1234,$004000
Constant value: $1234 (hex)
Target: memory address $4000.
Memory Address:
$3FFE

$3FFF

$4000
$12
$4001
$34
$4002

Example: Moving Hex Value to Address Register

A3 = $00000000
MOVEA.L#$00400400,A3
Constant variable: 00400400 (hex)
Target: A3.
A3 = $00000000
A3 = $00400400

Example: Moving Character Value to


Memory
MOVE.L
#BUKU,D0
B = $42, U = $55, K = $4B, U = $55
Target: D0.

D0 =

2
B

5
U

B
K

5
U

Example: Moving Binary Value to Memory


MOVE.B
#%10101011,D0
%1010 = $A, %1011 = $B
Target: D0.

D0 =

Quick Immediate Data


Similar to ID, but can only transfer 1 byte.
Byte is sign-extended to 32-bits.
Used together with MOVEQ instruction.
Can only be used for Dn.

Example: Quick Immediate Data


D1 = $00000000
MOVEQ #$05,D1
Constant variable: 05 (hex)
Target: D1.
D1 = $00000000
D1 = $00000005
D1 = $00000005

$05 = 0000 0101


(MSB is 0)

Sign-extended to 32-bits

Example: Quick Immediate Data


D0 = $00000000
MOVEQ #$EA,D0
Constant value: EA (hex)
Target: D0.
D0 = $000000EA
D0 = $000000EA
D0 = $FFFFFFEA

$EA = 1110 1010


(MSB is 1)

Sign-extended to 32-bits

Example: Quick Immediate Data


D1 = $FFFFFFFF
MOVEQ #$05,D1
Constant value: 05 (hex)
Target: D1.
D1 = $FFFFFFFF
D1 = $FFFFFF05
D1 = $00000005

$05 = 0000 0101


(MSB is 0)

Sign-extended to 32-bits

Implied Addressing (IA)


Uses mnemonics to refer to M68ks internal
registers.
Examples:

SR

Status Register
USP User Stack Pointer.
SSP Supervisor Stack Pointer.
CCR Condition Code Register
TRAPV Trap exception if V-bit set.

IA Example Set Trace Mode

ORI.W #$8000,SR
T

I2

I1

I0

X N

V C

OLD SR= 0

OR

NEW SR= 1

Sets trace mode to on (T = 1), other bits unchanged.

IA Example Clear CCR

ANDI.B #$00,CCR
OLD CCR =

AND

Z V C

NEW CCR = 0

Clears all bits in CCR

Conclusion

We have covered these addressing


modes
Effective Address: <ea>
Dn and An
DRD
ARD

D0 D7
A0 A7

Immediate data: <id>


ID
#($ % @ )

ARI
ARI+PI
ARI+PD
ARI+D
ARI+I
PC+D
PC+I
ALA
ASA
IA

(An)
(An)+
-(An)
D(An)
D(An,Dn/An.s)
D(PC)
D(An,Dn/An.s)
$001001
$FFAA
CCR, SR

Conclusion
Addressing modes allow flexibility in
accessing registers memory locations.
Try to understand how to calculate EA:

ARI

(PI, PD, D, I)
PC (D, I)

The rest are straightforward.

Rotate and Shift

Rotate and Shift


Contains instructions to rotate and shift
bits around.
Logical Shift Left / Right (LSL / LSR):

Shifts

bits to left or to right by inserting zeros.

Rotate Left / Right (ROL / ROR):


Rotates

bits to left or to right

LSL (Logical Shift Left)

LSR (Logical Shift Right)

Example: LSL
Initial set-up:
D0 = $000000AA
D1 = $00000008
LSL.W D1, D0

1) source: D1.W = $0008 = 8 bits


2) target: D0.W = $00AA = %0000 0000 1010 1010
3) action: "shift 8 bits from D0.W to the left by
inserting 8 bits from the right"

%0000 0000 1010 1010


D0.W =

Pushed out

new D0.W =

New D0 = $0000AA00

%0000 0000 (8 bits)


0

Example: LSR
D0 = $000000AA
LSR.W #4,D0
0

D0.W =

% 0000
LSR

% 0000 0000 1010 1010

D0.W =

A
Pushed out

D0.W =

New D0 = $0000000A

ROL & ROR (Rotate Left/Right)


ROL: Pushes out MSB, and moves the
bits to the back.
ROR: Pushes out LSB, and moves the bits
to the front

ROL (Rotate Left)

MSB

ROR (Rotate Right)

LSB

Example: ROL
1) source: D1.B = $03 = 3 digits (in binary)
2) target: D0.B = $AB = %1010 1011

D0 = $000000AB,
D1 = $00000003
ROL.B D1,D0
D0.B =

D0.B =

1
And sent to back

Pushed out

D0.B =

New D0 = $0000005D

Example: ROR
D0 = $000000AB
ROR.B #4,D0
D0.B =

And sent to front

D0.B =

Pushed out

D0.B =

New D0 = $000000BA

You might also like