7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
1/122
TOPIC 3
ASSEMBLY LANGUAGE PROGRAMMING
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
2/122
At the end of the class youshould :
Understand assembly language programming
Compare high, assembly and machine language
Describe important terminologies
Determine four fields of an assembly languageinstruction
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
3/122
Differentiate between Machine, Assemblyand High Level Languages
Machine Language Assembly Language High Level Language
Basic programminglanguage
Used for thecommunication betweencomputer and human
Used for thecommunication betweencomputer and human
Were developed inwhich single statementscould be written toaccomplish substantialtasks
The translator programsthat convert middle levelprograms into machinelanguage are calledassembler
The translator programsthat convert high levelprograms into machinelanguage are calledcompilers
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
4/122
Differentiate between Machine, Assemblyand High Level Languages
Machine Language Middle Language High Level Language
Machine language,programmer s writeinstructions using binarycode
Middle language allowprogrammers to writeinstructions usingmnemonic
High level languagesallow programmers towrite instructions thatlook like everyday
English and containcommonly usedmathematical notations
Binary (0,1) Assembly language Ex : Basic, Pascal, C etc
Programmer need to
know specifically thearchitecture of CPU
Programmer need to
know basic architectureof CPU such as registers
Programmer do not
need to know thearchitecture of CPU torealize the high levellanguage programming
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
5/122
Important Terminologies :
Machine code
the binary language understood by themicroprocessor
Mnemonic Source files
contains all the instruction mnemonics needed to
execute a program
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
6/122
Important Terminologies :
Object files, list file and hex file object file The source file is converted into an object file containing
the actual binary information the machine will understand
by a special program called an assembler list file contains all the original source file text plus the additional
code generated by the assembler
hex file contains printable object code in a text format
Hex files (called S-record files by Motorola) are useful fordownloading over serial lines.
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
7/122
Important Terminologies :
Assembler
Single-line assembler
Work with one source line at a time and arerestricted in operation.
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
8/122
Important Terminologies :
Cross-assembler
Cross-assemblers are programs written in onelanguage, such as C, that translate source
statements into a second language: the machinecode of the desired processor
Pseudo-opcode
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
9/122
Assembly Language Format
Syntax : [label[:]] [mnemonic[operands]][;comments]
LABEL Labels (or symbols) are optional and are used to
identify a particular statement so that it can bereferred to from other parts of the program
MNEMONIC The mnemonic defines the function of a particular line
and it can either be one of the 68000 instructionmnemonics or it may be an assembler directive or
pseudo-instruction.
LABEL MNEMONIC OPERANDS COMMENTS
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
10/122
Assembly Language Format
OPERANDS
Operand may be a 68000 register name, anaddress or data or a label referring to either an
address or data When two operands are required, the first
operand specifies the destination for the result ofthe operation and the second operand specifies
the source of the data for the operation
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
11/122
Assembly Language Format
COMMENTS
Comments are used to describe the function ofthe program
Any text preceded by a semi-colon(;) or asterisk(*) is ignored by the assembler but will beincluded as a comment in the listing of theprogram
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
12/122
Assembler Directives
Assembler directives statements give theassembler information that affects the assemblyprocess, but do not necessarily cause object codeto be generated
ORG sets the ORIGIN address this is the firstmemory location to be used for storage of theprogram
EQU used to define (or EQUATE) the value of alabel
DC Define Constant used to store a byte (orb tes of data in memor
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
13/122
Assembler Directives
DS Define Storage used to reserve memorylocations for use by the program
ALIGN make the program counter even
EVEN - make the program counter even
TCALL allows calls to the operating systemusing the 68000 TRAP instruction
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
14/122
At the end of the class youshould :
Understand instruction set and addressingmodes
Identify groups of instruction sets of the 68000
microprocessor. Write assembly language using each group of
instruction set.
Use "DEBUG" program to debug assembly
language programs.
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
15/122
4 groups of instruction sets of68000 microprocessor
Data Movement/Data Transferinstruction set
Arithmetic instruction setLogic instruction set
Shift and Rotate instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
16/122
Data Movement/Data Transferinstruction set
Move instru ct ion
Use to copy/transfer data from source todestination
Transfer can be done between register andmemory
Instruction formatMOVE.s source, destination
s is data size. For MOVE the size are: B(byte)
W(word)
L(longword)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
17/122
Data Movement/Data Transferinstruction set
Move instru ct ion
Use to copy/transfer data from source todestination
Transfer can be done between register andmemory
Instruction formatMOVE.s source, destination
s is data size. For MOVE the size are: B(byte)
W(word)
L(longword)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
18/122
Data Movement/Data Transfer
instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
19/122
Data Movement/Data Transfer
instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
20/122
Data Movement/Data Transfer
instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
21/122
Data Movement/Data Transfer
instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
22/122
Data Movement/Data Transfer
instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
23/122
Data Movement/Data Transfer
instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
24/122
Data Movement/Data Transfer
instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
25/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
26/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
27/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
28/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
29/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
30/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
31/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
32/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
33/122
Data Movement/Data Transferinstruction set
/ f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
34/122
Data Movement/Data Transferinstruction set
D M /D T f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
35/122
Data Movement/Data Transferinstruction set
D t M t/D t T f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
36/122
Data Movement/Data Transferinstruction set
D t M t/D t T f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
37/122
Data Movement/Data Transferinstruction set
D t M t/D t T f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
38/122
Data Movement/Data Transferinstruction set
D t M t/D t T f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
39/122
Data Movement/Data Transferinstruction set
D t M t/D t T f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
40/122
Data Movement/Data Transferinstruction set
D t M t/D t T f
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
41/122
Data Movement/Data Transferinstruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
42/122
Addressing Modes
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
43/122
Addressing Modes
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
44/122
Arithmetic instruction set
ADDITION ADD EA, Dn (EA) + Dn Dn
allow Byte, Word and Long Word
data is accessed via register D0 D7, memory,
absolute data and I/O Port
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
45/122
Arithmetic instruction set
SUBTRACTION SUB EA, Dn Dn - (EA) Dn
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
46/122
Arithmetic instruction set
MULTIPLICATION (MULU, MULS) S (16 bit) x D (16 bit) D (32 bit)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
47/122
Arithmetic instruction set
DIVISION (DIVU, DIVS) D (32 bit) / S (16 bit) D [16 bit / 16 bit]
= D [Remainder / Quotient]
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
48/122
Logic instruction set
similar to the logic gate, logical operationsinvolve AND, OR and NOT
the content of the register and memory arecompared
the operation is implemented in ALU
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
49/122
Logic instruction set
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
50/122
Shift and Rotate instruction set
Shift Instructions :Arithmetic (signed) (0 to 127, -1 to -128)
Logical (unsigned) (0 to 255)
Rotate Instructions :
Ordinary Rotate
Rotate through X Flag
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
51/122
Shift and Rotate instruction set
Shif d R i i
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
52/122
Shift and Rotate instruction set
Shift d R t t i t ti t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
53/122
Shift and Rotate instruction set
Shift d R t t i t ti t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
54/122
Shift and Rotate instruction set
Shift d R t t i t ti t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
55/122
Shift and Rotate instruction set
Shift d R t t i t ti t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
56/122
Shift and Rotate instruction set
Shift d R t t i t ti t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
57/122
Shift and Rotate instruction set
Shift d R t t i t ti t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
58/122
Shift and Rotate instruction set
Shift d R t t i t ti t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
59/122
Shift and Rotate instruction set
At the end of the class you
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
60/122
At the end of the class youshould :
List types of addressing modes.
Code assembly language instructions usingeach addressing mode.
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
61/122
What is Addressing Modes?
Addressing Modes is :
is the techniqueused to fetch the desiredoperand during the execution of an instruction.
the location of the operand(s) on which toperform the function
The MC68000 supports 14
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
62/122
The MC68000 supports 14different addressing modes Data register direct Address register direct
Absolute short
Absolute long
Register indirect
Post-increment register indirect Pre-decrement register indirect
Register indirect with offset
Register indirect with index and offset
PC-relative with offset
PC-relative with index and offset Immediate
Immediate quick
Implied register
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
63/122
Addressing Mode summary
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
64/122
Data reg ister d irect
Description
data register (D0-D7) is the source or destination ofdata
Example since .B is appended to MOVE, only the low byte
of the destination data register is affectedINSTRUCTION MOVE.B D0,D3MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORED0
D3
1020 4FFF
1034 F88A
AFTERD0
D3
1020 4FFF
1034 F8FF
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
65/122
Add ress register direct
Description
address register (A0-A7) is the destination of data
only word or longword operands may be specified
A word operand is sign-extended to fit the register Example
The contents of A3 are copied onto A0INSTRUCTION MOVEA.B A3,A0
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFOREA0
A3
0020 0000
0004 F88A
AFTERA0
A3
0004 F88A
0004 F88A
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
66/122
Absolute sho r t
Description source or destination is a memory location whose address is
specified in one extension word of the instruction
bits 16-23 of the full address are obtained by sign-extension of the16-bit short address
Example
The source is immediate, destination is absolute short address
Since operation is .w, source is sign-extended to two bytes
INSTRUCTION MOVE.W #$1E, $800
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE000800
000801
12
34
AFTER
000800
00080100
1E
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
67/122
Absolute long
Description
source or destination is a memory location whose address is specified in twoextension word of the instruction
bits 16-23 of the full address are obtained by sign-extension of the 16-bit short address
Example
The source is immediate, destination is absolute long address Since operation is .b, only one byte of memory is changed
short address
Note
Often, the distinction between abs-short and abs-long is transparent to the
programmer due to the use of labelsINSTRUCTION MOVE.B #$1E, $8F00
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE 08F000 FF
AFTER08F000
1E
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
68/122
Reg ister ind irect
DescriptionAn address registercontains the address of the
address of the source or destination operand
Example The instruction moves a longword stored in D0 to
the memory location specified by he address in A0INSTRUCTION MOVE. L D0, (A0)
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE
001000
001001
001002
001003
55
02
3F
00
A0
D0
00001000
1043834F
AFTER
001000
001001
001002
001003
10
43
83
4F
A0
D0
00001000
1043834F
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
69/122
Pos t-inc rement reg ister ind irect
Description Indicated by a + sign after (Ai)
afterreading or writing data the address register is incrementedby the number of bytes transferred
Byte : [Ai] [Ai] + 1 Word : [Ai] [Ai] + 2 Longword : [Ai] [Ai] + 4
INSTRUCTION MOVE. W (A5)+, D0
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE
001000001001
001002
001003
4567
89
AB
A5
D0
00001000
0000FFFF
AFTER
001000
001001
001002
001003
45
67
89
AB
A5
D0
00001002
00004567
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
70/122
Pre-decrement regis ter ind irect
Description Indicated by a - sign before (Ai)
before reading or writing data the address register isdecremented by the number of bytes transferred
Byte : [Ai] [Ai] - 1 Word : [Ai] [Ai] - 2 Longword : [Ai] [Ai] - 4
INSTRUCTION MOVE. W D0,-(A7)
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE
001000001001
001002
001003
1012
83
47
A7
D0
00001002
00000143
AFTER
001000
001001
001002
001003
01
43
83
47
A7
D0
00001002
00000143
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
71/122
Reg ister ind irect w ith o ffset
Description A variation of register indirect that includes a 16-bit signed offset
(displacement) as an extension word in the instruction
The sign-extended offset is added to the address register to form
the effective address of the source or destination Example
Effective address is 6 plus address register
Value stored in the address register does not changeINSTRUCTION MOVE. W 6 (A0),D0
MEMORY REGISTERADDRESS CONTENTS NAME CONTENTS
BEFORE001026
001027
07
BF
A0
D0
00001020
00000000
AFTER001026
001027
07
BF
A0
D0
00001020
000007BF
Register indi rect w i th index and
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
72/122
Register indirect w i th index and
offset
Description Another variation of register indirect. An index is used as well as
an 8-bit signed offset
The effective address is formed by adding the sign-extended
offset, the contents of the index register and the contents of theaddress register
Example
=$10+$100A+$2=$101CINSTRUCTION MOVEA $10(A0,D0.L),A1
MEMORY REGISTERADDRESS CONTENTS NAME CONTENTS
BEFORE00101C
00101D
EF
10
A0
A1
D0
0000100A
00000000
00000002
AFTER00101C
00101D
EF
10
A0
A1
D0
0000100A
FFFFEF10
00000002
PC l t i i th f f t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
73/122
PC-relat ive w ith o ffset
Description a 16-bit offset is added to PC to form effective address
only source operand can be addressed this way
this mode provides position-independent code
assembler computes offset by subtracting PC from label
INSTRUCTION MOVE.W COUNT(PC),D5
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE001026
001027
AB
CD
PC
D5
00001000
12345678
AFTER001026
001027
AB
CD
PC
D5
00001004
1234ABCD
00001000 1 ORG $1000
00001000 3A3A 0FFE 2 MOVE.W COUNT (PC), D5
00002000 3 ORG $2000
00002000 ABCD 4 COUNT DC.W $ABCD
00002002 5 END
$0FFE = $2000-$1000-$2
PC l t i i th i d d ff t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
74/122
PC-relat ive w ith index and o ffset
Description an 8-bit signed offset plus and index register are used to
compute the address relative to the PC
INSTRUCTION MOVE.B TABLE(PC,D0.W),D0
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE 001015 19PC
D0
0000100A
ABCD0005
AFTER 001015 19PC
D0
0000100E
00000019
00001000 1 ORG $1000
00001000 303C 0005 2 MOVE.W #5,D0
00001004 6100 0004 3 BSR SQUARE00001008 4E75 4 RTS
0000100A 103B 0004 5 SQUARE MOVE.B TABLE (PC,D0.W),D0
0000100E 4E75 6 RTS
00001010 000104091019 7 TABLE DC.B 0,1,4,9,16,25
00001016 8 END
I d i t
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
75/122
Immediate
Description Immediate address uses two extension words to hold the source
operand
Data may be expressed in :
decimal (& prefix or none) hexadecimal ($ prefix)
octal (@ prefix)
binary (% prefix)
ASCII (string within )INSTRUCTION MOVE.B #$1FFFF,D0
MEMORY REGISTER
ADDRESS CONTENTS NAME CONTENTS
BEFORE D0 12345678
AFTER D0 0001FFFF
I d i t i k
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
76/122
Immed iate qu ick
Description Immediate quick addressing is an optimized case of immediate
addressing whose binary code fits in one word (including theoperand)
Immediate operand is sign-extended to fit the 32-bit destination Available with the following instructions
MOVEQ (operand must be a 8-bit signed integer)
ADDQ (operand must be in the range 1 to 8)
SUBQ (operand must be in the range 1 to 8)INSTRUCTION MOVEQ #$1F,D0
MEMORY REGISTERADDRESS CONTENTS NAME CONTENTS
BEFORE D0 12345678
AFTER D0 000001F
At the end of the class you
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
77/122
yshould :
Explain the operation of stacks and queuesaccording to LIFO and FIFO concept.
Flag Register / Stat s register
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
78/122
Flag Register / Status register
One of the registers in the MC68000microprocessor is status register.
It is a 16 bits register. Each bit has it own functionwith some of the bits unused.
The status register has 16 bits and is divided intothe system byte and user byte.
The user byte contains five condition flags. Theremaining 3 bits in the user byte are not used and
remain zero. The condition flags contain information on the result
of the last processor operation.
Flag Register / Status register
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
79/122
Flag Register / Status register
Carry Flag (C)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
80/122
Carry Flag (C)
C = 1 when out o f the MSBC = 1 when bo rrow to MSB
C = 0 when no ou t or borrow to MSB
Ex1 : move.b #$EE,d0move.b #$70,d1
add.b d0,d1
Ex2 : move.b #$F0,d0move.b #$EE,d1sub.b d0,d1
Negative Flag (N)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
81/122
Negative Flag (N)
N = 1 when MSB = 1N = 0 when MSB = 0
Ex1 : move.w #$7000,d0
move.w #$4000,d1add.w d0,d1
Ex2 : move.w #$2000,d0move.w #$4000,d1add.w d0,d1
Zero Flag (Z)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
82/122
Zero Flag (Z)
Z = 1 when resu lt is zeroZ = 0 when result is non -zero
Ex1 : move.b #$40,d0
move.b #$70,d1sub.b d0,d1
Ex2 : move.b #$70,d0move.b #$70,d1sub.b d0,d1
Overflow Flag (V)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
83/122
Overflow Flag (V)
V = 1 when abnormalV = 0 when normal
Ex1 : move.b #$70,d0
move.b #$40,d1
add.b d0,d1
Ex2 : move.b #$20,d0
move.b #$40,d1
add.b d0,d1
Ex3 : move.b #$70,d0
move.b #$D0,d1
sub.b d0,d1
Extend Flag (X)
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
84/122
Extend Flag (X)
X = C
except for : i) movement (move)
ii) logic (AND)
iii) comparison (CMP)
Example :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
85/122
Example :
If initial data for Status Register (SR) = $A71Fand
D2 = 6677 8899, what are the end data in SR
andD2 after ADDI.B #$5E,D2 instruction been
executed.
Show how each bit of flags obtainedBits SR T 0 S 0 0 I2 I1 I0 0 0 0 X N Z V CInitial A71F 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1End ? 1 1 1 0 0 1 1 1 0 0 0 ? ? ? ? ?
At the end of the class you
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
86/122
yshould :
Explain the operation of stacks and queuesaccording to LIFO and FIFO concept.
What is STACK ?
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
87/122
What is STACK ?
Stack is a memory-based structures
a STACK is an area in memory reserved forreading and writing special data items such as
return addresses and register values.
Ex :
i) move.l D3,-(A7)
Entire contents of D3 are written into the stack areapointed to by A7. A7 is automatically decremented by4 during execution.
Item previously pushed onto the stack
Cont
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
88/122
Cont
Ex : ii) move.l (A7)+,D3
Stack memory is read out into D3 and A7 isautomatically incremented by 4 during execution.
items popped off the stack (read out of memor)
STACK Characteristic
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
89/122
STACK Characteristic
One characteristic of a stack is that the last itempushed is always the first item popped.
Stack are commonly referred to as a LIFO (last
in, first out) structure.
LIFO ILLUSTRATION
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
90/122
LIFO ILLUSTRATION
A stack of trays.
The last tray to beplaced on top is
also the first to betaken off the top.
QUEUE
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
91/122
QUEUE
Stack is a memory-based structures
In a QUEQU, the first item loaded is the firstitem to be removed.
Queues are referred to as FIFO (first in, firstout).
FIFO ILLUSTRATION
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
92/122
FIFO ILLUSTRATION
Personsstanding inline.
Personsleave thequeue in theorder they
arrive.
Stack Pointer
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
93/122
Stack Pointer
Stack PointerA stack pointer is used to point to thelast stack location used.
Cont
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
94/122
Cont
PUSH (input) Data PUSH into stack (Stack SAVE) as data are pushed into the stack, the stack grows
towards the lower address
POP (output) Data POP from stack
moving towards higher address
Examples :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
95/122
Examples :
Question 1 :
Stack Pointer contains 00003000H
move.w #$1234, -(SP)
SOLUTION :
location 0000 2FFEH contains12H
location 0000 2FFFH contains34H
Stack Pointer = 0000 2FFEH
SP Address Stack
SP-2 00002FFE
12
SP-1 0000 2FFF 34
SP 0000 3000 X
Examples :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
96/122
Examples :
Question 1 :
Stack Pointer (A7)=0000 4200H,
D0 = 1122 3344
move.w (SP)+,D0
Has the following contents
SOLUTION :
After D0 = 1122 5634 Stack Pointer = 0000 4202H
SP Address Stack
0000 41FE 9A
0000 41FF 78
0000 4200 56
0000 4201 34
0000 4202 12
SP Address Stack
0000 41FE 9A
0000 41FF 78
0000 4200 56
SP 0000 4201 34
SP + 1 0000 4202 12
Exercise :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
97/122
Exercise :
Question 1 :
Stack Pointer contains 00001000H
move.b #$AABB CC DD, -(SP)
ANSWER
Exercise :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
98/122
Exercise :
Question 2 :
Stack Pointer (A7)=0000 1000H
D0 = AABB CCDD
move.l (SP)+,D0
Has the following contents
SP Address Stack
0000 0FFF DE
0000 1000 BC
0000 1001 9A
0000 1002 78
0000 1003 56
0000 1004 34
ANSWER
At the end of the class you
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
99/122
should :
Draw the flowchart for a simple problemusing branching and subroutines.
Apply assembly-level structured
programming techniques usingbranching and subroutines.
Concept of Programming
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
100/122
Concept of Programming
Definition of problem Logical design
Programming
Test run and Analysis the program Documentation of the program
Steps :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
101/122
Steps :
1. Definition ofproblem
Inpu t data of two num bers. These two numbersare added, and the produ ct is stored in a
memory locat ion.
2. Logical designSTART
Data1 D0
Data2 D1
D0 + D1 D1
D1Memory
END
Steps :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
102/122
Steps :
3. Programming ORG $7000
MOVE.B #12,D0MOVE.B #34,D1ADD.B D0,D1MOVE.B D1,$7050
END
Steps :ORG $7000 ; The program initiled at address$7000
This is a assembler directive to store the opcodes of this program in the memory
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
103/122
Steps :
4. Test run andAnalysis theprogram
ADD.B D0,D1 ;D0 + D1 D1
From instruction list, the operation of this
instruction:
ADD.B EA,Dn >> (EA) + Dn Dn
ADD.B D0,D1 >> D0 + D1 D1
D0 + D1 = $0C + $22 = $2E
D1 = $xxxxxx2E
s s a asse b e d ect e to sto e t e opcodes o t s p og a t e e o y
started at address $7000.MOVE.B #12,D0 ; decimal data 12stored in D0; end program execution.
From instruction list, the operation of this
instruction:
MOVE.B SRC,DST >> (SRC EA) DST EA
MOVE.B #12,D0 >> #12 D0Since: 1210 = 0CH; thus, D0 = $xxxxxx0C
MOVE.B #34,D1 ; decimal data 34stored in D1
MOVE.B #34,D1 >> #34 D1Since: 3410 = 22H; thus, D1 = $xxxxxx22
D0 XXXXXXXX XXXXXX0C
Before After
D1 XXXXXXXX XXXXXX22
Before After
D0 XXXXXX0C XXXXXX0CBefore After
D1 XXXXXX22 XXXXXX2E
MOVE.B D1,$7050 ; product ofaddition stored in memory at location $7050
From instruction list, the operation of thisinstruction:
MOVE.B SRC,DST >> (SRC EA) DST EA
MOVE.B D1,$7050 >> D1 M($7050)
$2EM($7050)
D1 XXXXXX2E XXXXXX2E
Before After
7050 XXXXXXXX XXXXXX2E
END ; end program execution.
Before After
D0 XXXXXXXX XXXXXX0C
Steps :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
104/122
Steps :
5. Documentation ofthe program
ORG $7000 ; The program initiled at address$7000
MOVE.B #12,D0 ; decimal data 12 stored in D0MOVE.B #34,D1 ; decimal data 34 stored in D1ADD.B D0,D1 ; D0 + D1 D1MOVE.B D1,$7050 ; product of addition stored in
memory atlocation $7050
END ; end program execution.
Flowchart
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
105/122
Flowchart
A flowchart is really a shorthand technique fordescribing the processes within a system
Flowcharts can also be used to describecomputer programs
Flowchart basic symbols :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
106/122
Flowchart basic symbols :
Symbols Descriptions
Process
Input / Output
Decision
Flowchart basic symbols :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
107/122
Flowchart basic symbols :
Symbols Descriptions
Start / Stop
Connector
Program flow
direction
Branch Instruction
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
108/122
Branch Instruction
There are 9 branch commands in the 68000instruction set
Instruction Example
Bcc BNE $5
BRA BRA $FFFE
BSR BSR $FFEE
DBcc DBEQ D1,$009
JMP JMP (A1)
JSR JSR $11010
RTE RTE
RTR RTR
RTS RTS
Branch Instruction cont
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
109/122
Branch Instruction cont
Bcc instruction is for branching based on acondition
BRA - instruction is for the branching always
BSR - instruction is for the branching to asubroutine
DBcc - instruction is for testing a condition,decrementing and branching
JMP instruction is for jumping
JSR istruction is for jumping to a subroutine
Branch Instruction cont
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
110/122
Branch Instruction cont
RTE instruction is for returning from anexception
RTS instruction is for returning from a
subroutine RTR - instruction is for returning from a
subroutine and restoring condition codes
Types of programming structure
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
111/122
ypes o p og a g s uc u e
Construct of Sequential
Types of programming structuret
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
112/122
cont
Construct of Loop
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
113/122
Types of programming structurecont
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
114/122
cont
Construct of decision making byBranch/JumpOne-Choice Two-Choices
Subroutines
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
115/122
The subroutine concept is an approach tosimplify a very long or complex program.
There are several instructions in a programtend to be repeated to perform the similarfunction, or with a little alteration. Thus it ismore practical to group these few instructions
into a sub-program, and to be called whenevernecessary by calling its name or label.
Subroutines cont
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
116/122
This sub-program is normally referred assubrout inein M68000. In other words, asubroutine is a special segment of programthat can be called for execution from any pointin a program.
Subroutine concept
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
117/122
p
Nested Subroutine concept
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
118/122
p
Ex :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
119/122
Ex :
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
120/122
Subroutines Instructions
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
121/122
Jump to Subroutine (JSR) The JSR instruction transfers program execution
to a subroutine
Return From Subroutine (RTS)
The RTS instruction restores the program counterfrom the point at which it left the main program
Subroutines Instructions
7/28/2019 Chapter 3 ASSEMBLY LANGUAGE PROGRAMMING
122/122
Branch to Subroutine Instruction (BSR) This instruction has a similar function to JSR
except that the address of the subroutine to becalled is specified by a relative address (offset)
rather than an absolute address. Like the BRAinstruction, BSR is restricted to 8-bit or 16-bitoffsets