You are on page 1of 92

CHAPTER 3

INSTRUCTION SET AND


ASSEMBLY LANGUAGE
PROGRAMMING

DFT 1113
COMPUTER
ORGANISATION

NOR ZAMIRA
BINTI OTHMAN
JTMK

LEARNING OUTCOMES
3.1
UNDERSTAND INSTRUCTION SET AND ASSEMBLY
LANGUAGE.
3.1.1 Define Instruction set, machine and assembly language.
3.1.2 Describe the addressing modes using proper instruction
format
3.1.3 Describe various types of addressing modes with example.

3.2
APPLY ASSEMBLY LANGUAGE.
3.2.1 Write a simple program in assembly language for:
a. arithmetic operation
b. logic operation
3.2.2 Use tools in analyzing and debugging assembly language
programs

INTRODUCTION
Capability of processor determine the capability of the
computer system. Therefore, processor is the key
element or heart of a computer system.
Other than PC, microprocessors are used in various
computerized system such as industrial automation.

3.1. UNDERSTAND THE INSTRUCTION SET


AND ASSEMBLY LANGUAGE.
Message sent by programmers (program
instructions) understood by the computer and
vice versa.
Language that friendly to human also
understandable to computer systems.
Three level of language:
Low level machine language
Middle level assembly language
High level high-level language

3.1.1 DEFINE INSTRUCTION SET, MACHINE


AND ASSEMBLY LANGUAGE
INSTRUCTION SET :
The complete collection of instructions that are understood
by a CPU
Known also as Machine Code/Machine Instruction
Binary representation
Usually represented by assembly codes

MACHINE LANGUAGE:
Machine language is a binary program (or binary code).
Binary code is a sequence of instruction and operand in
binary that list the exact representation instruction as
they appear in computer memory.

Natural language of a particular computer system.


Strings of numbers or binary codes (0 or 1).

Machine-dependent (differ from one P to other P.


Program written in any other language must be
translated to binary representation of instruction
before they can be executed by computer.

Programmers

need

architecture of CPU.

to

know

specifically

the

MACHINE LANGUAGE:

LIMITATIONS IN PROGRAMMING USING


MACHINE LANGUAGE
The
program
is long
and
arduous

The
program is
difficult to
understan
d

The
program did
not specify
the tasks
we perform
require a
computer.

The program
is slow to keyin to the
computer due
to the input
one bit by one
bit at a time

To overcome these problems, the use of


easily remembered code (mnemonic) was
introduced. It is apply with the use of
Assembly Language

ASSEMBLY LANGUAGE
Assembly language is a symbolic representation of a
machine language of specific processor. Assembly language

is a form that is very dependent on the underlying


architecture.
Using english-like abbreviations (MUL), ( ADD), (SUB)

Assembler as translator.
Assembler - Translate ordinary mnemonics such as MOVE
Data, Acc, into their corresponding machine language

(the only form of instruction that computer can


executed)

ASSEMBLY LANGUAGE (AL)


Assembly Process - Process of translating an assembly
language programs into a machine language programs.
The assembly process is straightforward (one -to-one
mapping of assembly languages statements to their

machine language counterpart) and rather simple

Programmer need to know the basic architecture.

WHY ASSEMBLY LANGUAGE STUDY IS


IMPORTANT?
It clarifies the execution of instruction.
It shows how the data represent in memory.
It show how a program interacts with the operating system
(OS), processor and, input and output (I/O) system.

It clarify how a program access external devices


Sometimes difficult to access hardware drive and system
tool if using (register transfer languages) RTL

ADVANTAGES USING ASSEMBLY


LANGUAGES
Easy to make compilers, debuggers and other device tools.
Allow accessing information that is not accessible
(restricted) from high level languages.
More function library that can be used in programming

development.
Possibility to make library function that compatible with
different compiler and operating system

DISADVANTAGES USING ASSEMBLY


LANGUAGES
(Development) Requires longer development time
(Reliability & Security) Easy to makes error
(Debug) More possibility for errors
(Maintain) Difficult to modify because it allows
unstructured code.
(Portability) Difficult to porting to different

platforms.

HIGH LEVEL LANGUAGE (HLL)


Using everyday English and common mathematical
notation. (x = I + j)
Overcome problems : assembly language require many
instruction to accomplish a simple task.
Single instruction in HLL = several AL instructions.
Compiler as translator.
Programmers do not need to know the architecture of CPU.

THREE DIFFERENT LEVELS OF LANGUAGE

COMPARISON OF INSTRUCTIONS OF
THREE DIFFERENT LANGUAGES

COMPARISON OF INSTRUCTIONS OF
THREE DIFFERENT LANGUAGES

MICROPROCESSOR OVERVIEW
The term microprocessor typically refers to the central processing unit
(CPU) of a microcomputer, containing the arithmetic logic unit (ALU)

and the control units (CU).


Performs the main tasks for the computer system
data transfer between (registers) and memory or I/O
arithmetic and logic operations. Example: ADD, SUB, AND, OR, etc
program flow via simple decisions. Example: Zero, Sign, Carry, etc

DATA SIZES
BIT
binary digit
hold only two values: 0 or 1
Smallest unit of data

1 bit : the smallest.


4 bit : nibble.
8 bit : byte; indicated (.B) notation
16 bit : word; indicated (.W) notation
32 bit : long word; indicated (.L) notation

DATA SIZES
For the data which has more bits, it is divided into dual half portions
upper (MSD section)
lower portions (LSB section).

1 Byte consists of two nibbles;


upper nibble
lower nibble.

1 Word consists of two bytes;


upper byte
lower byte.

1 Long Word consists of two word;


upper word
lower word.

DATA SIZES
Can store either
Longword
1 0 0 1 1 0 0 1 1 1 0101110101011101000101

Word
0 1 0 1 0 1 1 1 0 1 0 0 0 1 0 1

Byte
01000101

NOTATIONS FOR INFORMATION


REPRESENTATION
64 bits
8 bytes
2 words
1 doubleword

Q: How do we number these various units of information in a consistent manner?

9 6 2 1 7 6 6
Most Significant Digit (MSD)

Big End-ian Numbering

Least Significant Digit (LSD)

0 1 2 3 4 5 6
6 5 4 3 2 1 0 Little End-ian Numbering

NOTATIONS FOR INFORMATION


REPRESENTATION

DATA SIZE
A single cell sized 1 bit can store either logic -0 or logic-1.
Thus the range of data is 0 1 .
Data size: n = 1
Data capacity : 2n = 21 = 2
Range : 0 1
A single cell sized 4 bit (Nibble) can store 16 possible
situations
Data size: n = 4
Data capacity : 2n = 24 = 16
Range : 0 - 15

DATA SIZE
Data
size
N
( in bit)

Data type

Data capacity
2n

Range

Bit

01

Nibble

16

0 - 15

Byte

256

0 255

16

Word

65536

0 65535

32

Long Word

4,294,967,296

04,294,967,295

REGISTER SET OF MC68000


Internal Register the
working element of p.
MC68000 an internal 32-

bit processor
Register Set
8 data register (D0-D7)
7address register(A0-A6)
2 stack pointer(USP&SSP)

1 program counter (PC)


1 status register (SR)

DATA T YPE : BINARY

DATA T YPE : OCTAL

DATA T YPE : HEXADECIMAL

DATA T YPE : DECIMAL

ASCII CHARACTER

DATA T YPE

% binary number
@ octal number
$ hexadecimal number
No sign decimal number
Character ASCI code

3.1.2 DESCRIBE
ADDRESSING MODES
USING PROPER
INSTRUCTION FORMAT

INSTRUCTION FORMAT
There are many MC68000 instructions. However, theses
instructions comply to a standard instruction format:

INSTRUCTION FORMAT

<Label> <Operator>

<Operand> Comments

<Label> <Opcode>

<Operand> Comments

<Label> <Mnemomic> <Operand> Comments

INSTRUCTION FORMAT
LABEL pointer to the instructions memory location
OPERATOR -operation code ( ie;MOVE,ADD)
OPERAND Depends on the operator, may has more
than one operand.
COMMENT Explanation about the execution of
instruction.
Data format - Byte | Word | Longword | Sign-extended

INSTRUCTION FORMAT

LABEL
Label are optional
Label a user-defined symbol representing the
address associated with the instruction.
Up to 8 alphanumeric, begin with letter, terminated by
a space.

OPCODE/OPERATION CODE / MNEMONIC


Opcode is an operation code that will be executed by
microprocessor

It is abbreviation of English-like word for an operation


For example
MOVE Transfer data from source to destination
ADD Add two binary numbers stored in registers
Associates with the size of data to be execute
Can be either
Byte: .B

Word: .W
Longword: .L

OPERANDS

COMMENT
Comment is optional
Comment provide readability for future reference
Explain the operation of each instruction
Any line beginning with an * is a comment.
Any line beginning with a ; (semi-colon) is a comment.
Example ;

org $1000 ; set the location counter to $1000

DATA FORMAT
mnemonics are postfixed with symbol ".B", ".W", ".L".

DATA FORMAT

DATA FORMAT

DATA FORMAT

DATA FORMAT

DATA FORMAT

3.1.3 DESCRIBE
VARIOUS TYPES OF
ADDRESSING MODES
WITH EXAMPLE.

CLASSIFICATION OF INSTRUCTION SETS


Data transfer Group

Arithmetic Group
Logical Group

Shift and rotate Group


Branch & Subroutine

Stack & Queue

DATA TRANSFER GROUP


These instructions transfer or move data between its
internal registers, between an internal register and a
storage location in memory, or between two locations
in memory.

memory to register
register to memory
register to register
memory to memory

DATA TRANSFER GROUP

MOVE (SOURCE TO DESTINATION)


Byte(B), Word(W) and Long word(L).
R R , R M , M M , EA SR , USP An
Example 1:
MOVE.B #$72,D1
Before : D1 = $00200500
After : D1 = $00200572

# indicate actual
value
$ indicate
hexadecimal value

Example 2:

MOVE.B D0,D1
Before : D1 = $00200500 , D0 = $00002222
After : D1 = $00200522 , D0 = $00002222

Example 3:
MOVE.W $3000,D1
Before : D1 = $00200500 ,
After : D1 = $00203243 ,

$3000

32

$3001

43

$3002

98

MOVE (SOURCE TO DESTINATION)


Example 4:

before $4000

MOVE.W D6,$4000
Before: D6 = $AB206541 ,
After : D6 = $AB206541
After

Example 5:

MOVE.W D2,(A1)
Before : D2 = $AB206541 ,
A1 = $00000501
After : D2 = $AB206541 ,
A1 = $00000501

3254

$4000

32

$4002

4377

$4001

54

$4004

9868

$4002

43

$4000

6541

$4000

65

$4002

4377

$4001

41

$4004

9868

$4002

43

After

Before
$501

FF

$501

65

$502

FF

$502

41

$503

FF

$503

FF

MOVEA (MOVE ADDRESS)


Provides a means of initializing an address register.
Only a word or longword operand is allowed to be
transferred into the specified address register.
For a word operation, the source operand is sign extended before being loaded into the address
register.
Example 5:

MOVEA.W D6,A2
Before : D6 = $AB206541 , A2 = $ABCD1234
After : D6 = $AB206541 , A2 = $00006541

MOVEA (MOVE ADDRESS)


Example 6:
MOVEA.W #$8C00,A0
Before : A0 = $FFFFFFFF
After : A0 = $FFFF8C00

Example 7:
MOVEA.W D6,A2
Before : D6 = $AB20A541 , A2 = $ABCD1234
After : D6 = $AB20A541 , A2 = $FFFFA541

Example 8:
MOVEA.L D6,A2
Before : D6 = $AB206541 , A2 = $ABCD1234
After : D6 = $AB206541 , A2 = $AB206541

MOVEQ (MOVE QUICK)


A short form of the move instruction for transferring
an immediate operand to a data register.
The immediate operand is limited to the range of
-128 to 127.

The operation size is implied to be longword.


Therefore the 8-bit immediate operand is signextended to 32 bits before being loaded into the
destination, which must be a data register.

MOVEQ (MOVE QUICK)

MOVEQ (MOVE QUICK)


Example 1:
MOVEQ #$04,D3
Before : D3 = $AB206541
After : D3 = $00000004

Example 2:
MOVEQ #$80,D3
Before : D3 = $AB206541
After : D3 = $FFFFFF80

Example 3:
MOVEQ #-3,D2
Before : D2 = $AB206541
After : D2 = $FFFFFFFD

MOVEM (MOVE MULTIPLE REGISTERS


Transfer words or longwords between a register list
and consecutive memory locations.
In the case of a word transfer to the registers, each
memory word is sign-extended before being loaded
into the respective register.

MOVEM (MOVE multiplr registers)

Example 1:

MOVEM.W D2/D5/D0,$200
Before : D0 = $11112222 , $200 6541
D2 = $33334444 , $202 4377
D5 = $55556666 , $204 9868
After : D0 = $11112222 , $200 4444
D2 = $33334444 $202
,
6666
D5 = $55556666 $204
,
2222

Example 2 :

Predecrement (tolak dulu)


B:1,W:2,L:4
Dlm contoh ada 3 daftar
maka 3x4=12 ,
50C C = 500

MOVEM.L D2/D5/D0,-(A4)
$500
Before : D0 = $11112222 , D2 = $33334444 ,$504
D5 = $55556666 , A4 = $0000050C$508
$50C
After : D0 = $11112222 ,
$500 33334444
D2 = $33334444 ,
$504 55556666
D5 = $55556666 ,
$508 11112222
A4 = $00000500

451287A4
54A2F221
846AC1DD
45123211

MOVEM (MOVE multiplr registers)


Example 3 :

MOVEM.W $200,D2/D5/D0
Before : D0 = $11112222 ,
D2 = $33334444 ,
D5 = $55556666 ,
After : D0 = $FFFF9868 ,$200
D2 = $00006541 $202
,
D5 = $00004377 $204
,

Example 4 :

$200

6541

$202

4377

$204

9868

6541
4377
9868

Posincrement (tmbh kmdn)


B:+1,W:+2,L:+4
Dlm contoh ada 3 daftar
maka 3x4=12 ,
500 + C = 50C

MOVEM.L (A4)+,D2/D5/D0
$500
Before : D0 = $1111222 , D2 = $33334444 , $504
D5 = $55556666 , A4 = $00000500$508
$50C
After : D0 = $846AC1DD ,
$500 451287A4
D2 = $451287A4 , $504 54A2F221
D5 = $54A2F221 , $508 846AC1DD
A4 = $0000050C

451287A4
54A2F221
846AC1DD
45123211

EXG (EXCHANGE)
EXG interchanges the contents of two
registers.
operand size for EXG is longword
Example 1:
EXG D1,D5
Before : D1 = $11223344 , D5 = $66778899
After : D1 = $66778899 , D5 = $11223344

SWAP
SWAP exchanges the lower word of the specified
data register with its upper word.
the operand size for SWAP is word.
Example 1
SWAP D1
Before : D1 = $11223344
After : D1 = $33441122

Example 2
SWAP D1
Before : D1 = $1234567
After : D1 = $45670123

LEA (LOAD EFFECTIVE ADDRESS)


Transfers the source operand address rather than its content
to the destination address register.
Therefore:
MOVEA .L #OPER,A1 is equivalent to
LEA OPER,A1

OPER EQU $2001


LEA OPER,A1

CLR

Loads the target with 0


Example 1:
CLR.B D1
Before : D1 = $11223344
After : D1 = $ 11223300

ARITHMETIC GROUP
Similarly as the operation of mathematic,
addition (+), subtraction (-), multiplication (x),
division ()
Binary arithmetic instructions performs signed
and unsigned operations.

ARITHMETIC GROUP

ADD (ADDITION)
68000 MP allows addition of 3 data size i.e 8 bit (.B),
16 bit (.W), and 32 bit (.L)
Data is accessed via register (D0 to D7), memory,
absolute data and I/O port.
< Destination> + <Source>

<Destination>

Both can be data registers


At least one must be a data register

ADD (ADDITION)
$3000

32

$3001

43

$3002
ADD.L D0,D1
Before : D1 = $00200500 , D0 = $00002222
After : D1 = $00202722 , D0 = $00002222

98

Example 1:

Example 2:
ADD.W $3000,D1
Before : D1 = $00200500 ,
After : D1 = $00203743 ,

ADD (ADDITION)
Example 3:
ADD.B D0, D1
.B
HH
22
+ 44
66

ADD (ADDITION
Example 4:
ADD.W D6,$4000
Before: D6 = $AB206541 ,
After : D6 = $AB206541 ,

$4000

3254

$4000

32

$4002

4377

$4001

54

$4004

9868

$4002

43

$4000

9795

$4000

97

$4002

4377

$4001

95

$4004

9868

$4002

43

ADDI (ADD IMMEDIATE)


Add an immediate value
Cannot be done using ADD as one operand must
be a data register
Example :ADDI.W #$1234,(A0)

Example 1:

ADDI.W

#$72,D1 and
ADDI.W #$72,D1
ADD.W #$72,D1
Before : D1 = $00200500 are equivalent
After : D1 = $00200572
But ADDI # can add to a
memory location

ADDA (ADDITION)
ADDA destination of add is an address register
Example 1:
ADDA.W D6,A2
Before : D6 = $AB206541 , A2 = $ABCD1234
After : D6 = $AB206541 , A2 = $ABCD7775

Example 2:
ADDA.L D6,A2
Before : D6 = $AB20A541 , A2 = $ABCD1234
After : D6 = $AB206541 , A2 = $56EDB775 , C = 1

Example 3:
ADDA.W #$8122,A0
Before : A0 = $FFFFFFFF
After : A0 = $FFFF8122

ADDQ (ADDITION

ADDQ Add a constant (1 to 8) to a memory


location, or register. It is executed faster.
ADDQ #4, D1
Speed is faster than

1 until 8

ADD #4, D1

Example 1:
ADDQ.B #$04,D3 (ADDQ #XXX,Dn)
Before : D3 = $AB206541
After : D3 = $AB206545

ADDX (ADD EXTENDED)


Add source and destination plus contents of the X
bit of the condition code register
Both source and destination must be data
registers
Example 1:
ADDX.B D2,D3
Before : D2 = $AB206541 , D3 = $ABCD1234 , X = 1
After : D2 = $AB206541 , D3 = $AB206576 , X = 1

SUB (SUBTRACTION)
< Destination> - <Source>

<Destination>

SUB, SUBA, SUBQ, SUBI, SUBX the


subtractions equivalents of ADD
Example 1:
SUB.W #$80,D3
Before : D3 = $AB206541
After : D3 = $AB2064C1

SUB
Example 2:
SUBI.L #10,D2
Before : D2 = $AB206541
After : D2 = $AB206537

Example 3:
SUBQ.W #@11,D3 (SUBQ #XXX,Dn)
Before : D3 = $AB206541
-128 (80) until 127 (7F)
After : D3 = $AB200038

MULU
Example 1:
MULU #2,D2 ;16 bit x 16 bit = 32 bit
Before : D2 = $AB206541
After : D2 = $0000CA82
@ octal no.

Example 2:
MULU #2,D2
Before : D2 = $AB20FFFF
After : D2 = $0001FFFE

% binary no.
No signdecimal no.

DIVU
Example 1:
DIVU #2,D3

;32 bit / 16 bit = 16 bit lower (result)


16 bit upper ( remainder)
Before : D3 = $00006541
After : D3 = $000132A0
6541F = 25921
32A016 = 1296010

Example 2:
DIVU #$1234,D0 ;32 bit / 16 bit = 16 bit lower(result)
16 bit upper ( remainder)
Before : D0= $00005678
After : D0 = $0DA80004

NEG

NEG Take the 2s complement of target


Example 1:
NEG.W D3
Before : D3 = $AB206541
After : D3 = $AB209ABF

NEG = 2s complement
$41
Invert

$BF

01000001
10111110
+
1
10111111

LOGICAL GROUP

Similar to the logic gate, logical operations


involve AND, OR, and NOT.
The content of the register and memory are
compared.
The operation is implemented in ALU.

LOGICAL GROUP

AND
Example 1:
AND.B #$3E, D3
[D3(B) AND $3E D3 (B) ]
(8 bit data in data register D3 is AND-ed with 8 bit
absolute data, and the product is stored in D3)

OR
Example 1:
OR.B D0, D1

[D1 (B) or D0 (B)

D1(B) ]

8 bit data in data register D1 is OR-ed with 8 bit data in data


register D0, and the product is stored in D1)

NOT
Example 1:
NOT.B D1
[D1(B) NOT D1 ]
(content in D1 is NOT, and the product is stored back
to D1)

You might also like