Vous êtes sur la page 1sur 122

TOPIC 3

ASSEMBLY LANGUAGE PROGRAMMING

At the end of the class you should :

Understand assembly language programming


Compare

high, assembly and machine language Describe important terminologies Determine four fields of an assembly language instruction

Differentiate between Machine, Assembly and High Level Languages


Machine Language Basic programming language Assembly Language Used for the communication between computer and human High Level Language Used for the communication between computer and human Were developed in which single statements could be written to accomplish substantial tasks The translator programs that convert middle level programs into machine language are called assembler The translator programs that convert high level programs into machine language are called compilers

Differentiate between Machine, Assembly and High Level Languages


Machine Language Machine language, programmer s write instructions using binary code Middle Language Middle language allow programmers to write instructions using mnemonic High Level Language High level languages allow programmers to write instructions that look like everyday English and contain commonly used mathematical notations

Binary (0,1) Programmer need to know specifically the architecture of CPU

Assembly language

Ex : Basic, Pascal, C etc

Programmer need to Programmer do not know basic architecture need to know the of CPU such as registers architecture of CPU to realize the high level language programming

Important Terminologies :

Machine code
the

binary language understood by the microprocessor

Mnemonic Source files


contains

all the instruction mnemonics needed to execute a program

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 contains all the original source file text plus the additional code generated by the assembler

list file

hex file
contains printable object code in a text format Hex files (called S-record files by Motorola) are useful for downloading over serial lines.

Important Terminologies :

Assembler Single-line assembler


Work

with one source line at a time and are restricted in operation.

Important Terminologies :

Cross-assembler
Cross-assemblers

are programs written in one language, such as C, that translate source statements into a second language: the machine code of the desired processor

Pseudo-opcode

Assembly Language Format


LABEL MNEMONIC OPERANDS COMMENTS

Syntax : [label[:]] [mnemonic[operands]] [;comments] LABEL

Labels (or symbols) are optional and are used to identify a particular statement so that it can be referred 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 instruction mnemonics or it may be an assembler directive or pseudo-instruction.

Assembly Language Format

OPERANDS
Operand

may be a 68000 register name, an address 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 of the operation and the second operand specifies the source of the data for the operation

Assembly Language Format

COMMENTS
Comments

are used to describe the function of the program Any text preceded by a semi-colon(;) or asterisk (*) is ignored by the assembler but will be included as a comment in the listing of the program

Assembler Directives

Assembler directives statements give the assembler information that affects the assembly process, but do not necessarily cause object code to be generated

ORG sets the ORIGIN address this is the first memory location to be used for storage of the program
EQU used to define (or EQUATE) the value of a label DC Define Constant used to store a byte (or

Assembler Directives
DS

Define Storage used to reserve memory locations for use by the program make the program counter even

ALIGN

EVEN

- make the program counter even

TCALL

allows calls to the operating system using the 68000 TRAP instruction

At the end of the class you should :

Understand instruction set and addressing modes


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.

4 groups of instruction sets of 68000 microprocessor


Data Movement/Data Transfer instruction set Arithmetic instruction set Logic instruction set Shift and Rotate instruction set

Data Movement/Data Transfer instruction set

Move instruction
Use

to copy/transfer data from source to destination Transfer can be done between register and memory Instruction format
MOVE.s

source, destination

s is data size. For MOVE the size are:


B(byte)

W(word) L(longword)

Data Movement/Data Transfer instruction set

Move instruction
Use

to copy/transfer data from source to destination Transfer can be done between register and memory Instruction format
MOVE.s

source, destination

s is data size. For MOVE the size are:


B(byte)

W(word) L(longword)

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Data Movement/Data Transfer instruction set

Addressing Modes

Addressing Modes

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

Arithmetic instruction set

SUBTRACTION

SUB

EA, Dn Dn - (EA) Dn

Arithmetic instruction set

MULTIPLICATION (MULU, MULS)

S (16 bit) x D (16 bit) D (32 bit)

Arithmetic instruction set

DIVISION (DIVU, DIVS)

D (32 bit) / S (16 bit) D [16 bit / 16 bit] = D [Remainder / Quotient]

Logic instruction set

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

Logic instruction set

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

Shift and Rotate instruction set

Shift and Rotate instruction set

Shift and Rotate instruction set

Shift and Rotate instruction set

Shift and Rotate instruction set

Shift and Rotate instruction set

Shift and Rotate instruction set

Shift and Rotate instruction set

Shift and Rotate instruction set

At the end of the class you should :


List

types of addressing modes. Code assembly language instructions using each addressing mode.

What is Addressing Modes?


Addressing Modes is : is the technique used to fetch the desired operand during the execution of an instruction. the location of the operand(s) on which to perform the function

The MC68000 supports 14 different 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

Addressing Mode summary

Data register direct

Description

data register (D0-D7) is the source or destination of data since .B is appended to MOVE, only the low byte of the destination data register affected MOVE.B is D0,D3 INSTRUCTION
MEMORY REGISTER

Example

ADDRESS
BEFORE

CONTENTS

NAME
D0 D3 D0 D3

CONTENTS
1020 4FFF 1034 F88A
1020 4FFF 1034 F8FF

AFTER

Address 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 A0


INSTRUCTION MEMORY ADDRESS BEFORE CONTENTS NAME
MOVEA.B A3,A0

REGISTER CONTENTS

A0 A3
A0 A3

0020 0000 0004 F88A


0004 F88A 0004 F88A

AFTER

Absolute short

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 the 16-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

BEFORE AFTER

MEMORY ADDRESS CONTENTS 000800 12 000801 34 000800 00 000801 1E

REGISTER NAME CONTENTS

Absolute long

Description

source or destination is a memory location whose address is specified in two extension 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 labels
INSTRUCTION
MOVE.B #$1E, $8F00

MEMORY ADDRESS CONTENTS BEFORE AFTER 08F000 08F000 FF 1E

REGISTER NAME CONTENTS

Register indirect

Description

An address register contains the address of the address of the source or destination operand The instruction moves a longword stored in D0 to the memory location specified by he address in A0
MOVE. L D0, (A0)

Example

INSTRUCTION

BEFORE

AFTER

MEMORY ADDRESS CONTENTS 001000 55 001001 02 001002 3F 001003 00 001000 10 001001 43 001002 83 001003 4F

REGISTER NAME CONTENTS A0 D0 00001000 1043834F

A0 D0

00001000 1043834F

Post-increment register indirect

Description

Indicated by a + sign after (Ai) after reading or writing data the address register is incremented by the number of bytes transferred Byte : [Ai] [Ai] + 1 Word : [Ai] [Ai] + 2 Longword : [Ai] [Ai] + 4
INSTRUCTION
MOVE. W (A5)+, D0

BEFORE

AFTER

MEMORY ADDRESS CONTENTS 001000 45 001001 67 001002 89 001003 AB 001000 45 001001 67 001002 89 001003 AB

REGISTER NAME CONTENTS

A5 D0

00001000 0000FFFF

A5 D0

00001002 00004567

Pre-decrement register indirect

Description

Indicated by a - sign before (Ai) before reading or writing data the address register is decremented by the number of bytes transferred Byte : [Ai] [Ai] - 1 Word : [Ai] [Ai] - 2 Longword : [Ai] [Ai] - 4
INSTRUCTION
MOVE. W D0,-(A7)

BEFORE

AFTER

MEMORY ADDRESS CONTENTS 001000 10 001001 12 001002 83 001003 47 001000 01 001001 43 001002 83 001003 47

REGISTER NAME CONTENTS

A7 D0

00001002 00000143

A7 D0

00001002 00000143

Register indirect with offset

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 change
INSTRUCTION
MOVE. W 6 (A0),D0

BEFORE AFTER

MEMORY ADDRESS CONTENTS 001026 07 001027 BF 001026 07 001027 BF

REGISTER NAME CONTENTS A0 00001020 D0 00000000 A0 00001020 D0 000007BF

Register indirect with 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 the address register

Example

<ae>=$10+$100A+$2=$101C
INSTRUCTION
MOVEA $10(A0,D0.L),A1

MEMORY ADDRESS CONTENTS BEFORE 00101C 00101D 00101C 00101D EF 10 EF 10

AFTER

REGISTER NAME CONTENTS A0 0000100A A1 00000000 D0 00000002 A0 0000100A A1 FFFFEF10 D0 00000002

PC-relative with offset

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
3A3A 0FFE ABCD 1 2 3 4 5 ORG MOVE.W ORG COUNT END $1000 COUNT (PC), D5 $2000 DC.W $ABCD $0FFE = $2000-$1000$2

00001000 00001000 00002000 00002000 00002002

INSTRUCTION

MOVE.W COUNT(PC),D5

BEFORE AFTER

MEMORY ADDRESS CONTENTS 001026 AB 001027 CD 001026 AB 001027 CD

REGISTER NAME CONTENTS PC 00001000 D5 12345678 PC 00001004 D5 1234ABCD

PC-relative with index and offset

Description

an 8-bit signed offset plus and index register are used to compute the address relative to the PC
00001000 00001000 00001004 00001008 0000100A 0000100E 00001010 00001016 303C 0005 6100 0004 4E75 103B 0004 4E75 000104091019 1 2 3 4 5 6 7 8 ORG MOVE.W BSR RTS MOVE.B RTS DC.B END $1000 #5,D0 SQUARE TABLE (PC,D0.W),D0 0,1,4,9,16,25

SQUARE TABLE

INSTRUCTION

MOVE.B TABLE(PC,D0.W),D0

MEMORY ADDRESS CONTENTS BEFORE AFTER 001015 001015 19 19

REGISTER NAME CONTENTS PC 0000100A D0 ABCD0005 PC 0000100E D0 00000019

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 )
MOVE.B #$1FFFF,D0

INSTRUCTION

MEMORY ADDRESS CONTENTS BEFORE AFTER

REGISTER NAME CONTENTS D0 D0 12345678 0001FFFF

Immediate quick

Description

Immediate quick addressing is an optimized case of immediate addressing whose binary code fits in one word (including the operand) Immediate operand is sign-extended to fit the 32-bit destination Available with the following instructions

INSTRUCTION

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)
MOVEQ #$1F,D0

MEMORY ADDRESS CONTENTS BEFORE AFTER

REGISTER NAME CONTENTS D0 D0 12345678 000001F

At the end of the class you should :


Explain

the operation of stacks and queues according to LIFO and FIFO concept.

Flag Register / Status register

One of the registers in the MC68000 microprocessor is status register. It is a 16 bits register. Each bit has it own function with some of the bits unused. The status register has 16 bits and is divided into the system byte and user byte. The user byte contains five condition flags. The remaining 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

Carry Flag (C)


C = 1 when out of the MSB C = 1 when borrow to MSB C = 0 when no out or borrow to MSB Ex1 : move.b #$EE,d0 move.b #$70,d1 add.b d0,d1 Ex2 : move.b #$F0,d0 move.b #$EE,d1 sub.b d0,d1

Negative Flag (N)


N = 1 when MSB = 1 N = 0 when MSB = 0 Ex1 : move.w move.w add.w Ex2 : move.w move.w add.w #$7000,d0 #$4000,d1 d0,d1 #$2000,d0 #$4000,d1 d0,d1

Zero Flag (Z)


Z = 1 when result is zero Z = 0 when result is non-zero Ex1 : move.b #$40,d0 move.b #$70,d1 sub.b d0,d1 Ex2 : move.b #$70,d0 move.b #$70,d1 sub.b d0,d1

Overflow Flag (V)


V = 1 when abnormal V = 0 when normal Ex1 : move.b #$70,d0 move.b #$40,d1 add.b d0,d1 move.b #$20,d0 move.b #$40,d1 add.b d0,d1

Ex2 :

Ex3 :

move.b #$70,d0 move.b #$D0,d1 sub.b d0,d1

Extend Flag (X)


X=C except for : i) movement (move) ii) logic (AND) iii) comparison (CMP)

Example :
If initial data for Status Register (SR) = $A71F and D2 = 6677 8899, what are the end data in SR and D2 after ADDI.B #$5E,D2 instruction been executed. Bits how SR T 0 S 0 of 0 flags I2 I1 obtained I0 0 0 0 X N Z V Show each bit
Initial End A71F ? 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 ? 1 ? 1 ? 1 ?

C 1 ?

At the end of the class you should :


Explain

the operation of stacks and queues according to LIFO and FIFO concept.

What is STACK ?

Stack is a memory-based structures a STACK is an area in memory reserved for reading 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 area pointed to by A7. A7 is automatically decremented by 4 during execution. Item previously pushed onto the stack

Cont

Ex :
ii) move.l (A7)+,D3 Stack memory is read out into D3 and A7 is automatically incremented by 4 during execution. items popped off the stack (read out of memor)

STACK Characteristic

One characteristic of a stack is that the last item pushed is always the first item popped. Stack are commonly referred to as a LIFO (last in, first out) structure.

LIFO ILLUSTRATION
A stack of trays. The last tray to be placed on top is also the first to be taken off the top.

QUEUE

Stack is a memory-based structures In a QUEQU, the first item loaded is the first item to be removed. Queues are referred to as FIFO (first in, first out).

FIFO ILLUSTRATION

Persons standing in line. Persons leave the queue in the order they arrive.

Stack Pointer

Stack Pointer A stack pointer is used to point to the last stack location used.

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 :
Question 1 : SOLUTION :
SP Address Stack

Stack Pointer contains 0000 3000H


move.w #$1234, -(SP)

SP-2
SP-1

0000 2FFE
0000 2FFF

12
34

SP 0000 3000 X location 0000 2FFEH contains 12H location 0000 2FFFH contains 34H Stack Pointer = 0000 2FFEH

Examples :
Question 1 :
Stack Pointer (A7)=0000 4200H, D0 = 1122 3344 move.w (SP)+,D0 Has the following contents SP Address Stack
0000 41FE 0000 41FF 0000 4200 0000 4201 0000 4202 9A 78 56 34 12 SP SP + 1 0000 41FE 0000 41FF 0000 4200 0000 4201 0000 4202 9A 78 56 34 12

SOLUTION :

SP

Address

Stack

After D0 = 1122 5634 Stack Pointer = 0000 4202H

Exercise :
Question 1 : ANSWER

Stack Pointer contains 0000 1000H


move.b #$AABB CC DD, -(SP)

Exercise :
Question 2 :
Stack Pointer (A7)=0000 1000H
D0 = AABB CCDD move.l (SP)+,D0 Has the following contents

ANSWER

SP

Address
0000 0FFF 0000 1000

Stack
DE BC

0000 1001
0000 1002 0000 1003 0000 1004

9A
78 56 34

At the end of the class you should :


Draw

the flowchart for a simple problem using branching and subroutines. Apply assembly-level structured programming techniques using branching and subroutines.

Concept of Programming

Definition of problem Logical design Programming Test run and Analysis the program Documentation of the program

Steps :
1. Definition of problem Input data of two numbers. These two numbers are added, and the product is stored in a memory location.

2. Logical design
START Data1 D0 Data2 D1

D0 + D1 D1

D1 Memory

END

Steps :
3. Programming ORG $7000 MOVE.B #12,D0 MOVE.B #34,D1 ADD.B D0,D1 MOVE.B D1,$7050

END

Steps :
4. Test run and Analysis the program

ORG $7000

$7000

; The program initiled at address

This is a assembler directive to store the opcodes of this program in the memory started at address $7000. MOVE.B #12,D0 ; decimal data 12 Before After stored in D0; end program execution. D0 XXXXXXXX XXXXXX0C From instruction list, the operation of this instruction:

MOVE.B SRC,DST >> (SRC EA) DST EA


MOVE.B #34,D1 stored in D1 MOVE.B #12,D0 ; decimal data 34 >> #12 D0 Before D1 XXXXXXXX After XXXXXX22

Since: 1210 = 0CH; thus, D0 = $xxxxxx0C MOVE.B #34,D1 >> #34 D1 ADD.B D0,D1 D0 + D1 D1 Since: 34 10 = 22H; thus, D1 = $xxxxxx22 ; Before D0 XXXXXX0C D1 XXXXXX22 From instruction list, the operation of this instruction: ADD.B EA,Dn >> (EA) + Dn Dn MOVE.B D1,$7050 ; product of addition stored in memory at location $7050 ADD.B D0,D1 >> D0 + D1 of this D1 From instruction list, the operation instruction: D0 + D1 = $0C + $22 = $2E MOVE.B SRC,DST >> (SRC EA) DST EA D1 = $xxxxxx2E END ; end program execution. MOVE.B D1,$7050 >> D1 M($7050) $2E M($7050) Before D0 XXXXXXXX After XXXXXX0C After XXXXXX0C XXXXXX2E

Before D1 XXXXXX2E 7050 XXXXXXXX

After XXXXXX2E XXXXXX2E

Steps :
5. Documentation of the program
ORG $7000 $7000 ; The program initiled at address MOVE.B #12,D0 ; decimal data 12 stored in D0 MOVE.B #34,D1 ; decimal data 34 stored in D1 ADD.B D0,D1 ; D0 + D1 D1 MOVE.B D1,$7050 ; product of addition stored in memory at location $7050 END ; end program execution.

Flowchart

A flowchart is really a shorthand technique for describing the processes within a system Flowcharts can also be used to describe computer programs

Flowchart basic symbols :


Symbols Descriptions

Process Input / Output


Decision

Flowchart basic symbols :


Symbols Descriptions

Start / Stop Connector


Program flow direction

Branch Instruction

There are 9 branch commands in the 68000 instruction set


Instruction Bcc BRA BSR DBcc JMP BNE $5 BRA $FFFE BSR $FFEE DBEQ D1,$009 JMP (A1) Example

JSR
RTE RTR RTS

JSR $11010
RTE RTR RTS

Branch Instruction cont

Bcc instruction is for branching based on a condition BRA - instruction is for the branching always BSR - instruction is for the branching to a subroutine 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

RTE instruction is for returning from an exception RTS instruction is for returning from a subroutine RTR - instruction is for returning from a subroutine and restoring condition codes

Types of programming structure

Construct of Sequential

Types of programming structure cont

Construct of Loop

Types of programming structure cont

Construct of decision making by Branch/Jump

Types of programming structure cont


Construct of decision making by Branch/Jump One-Choice Two-Choices

Subroutines

The subroutine concept is an approach to simplify a very long or complex program. There are several instructions in a program tend to be repeated to perform the similar function, or with a little alteration. Thus it is more practical to group these few instructions into a sub-program, and to be called whenever necessary by calling its name or label.

Subroutines cont

This sub-program is normally referred as subroutine in M68000. In other words, a subroutine is a special segment of program that can be called for execution from any point in a program.

Subroutine concept

Nested Subroutine concept

Ex :

Ex :

Subroutines Instructions

Jump to Subroutine (JSR)


The

JSR instruction transfers program execution to a subroutine

Return From Subroutine (RTS)


The

RTS instruction restores the program counter from the point at which it left the main program

Subroutines Instructions

Branch to Subroutine Instruction (BSR)


This

instruction has a similar function to JSR except that the address of the subroutine to be called is specified by a relative address (offset) rather than an absolute address. Like the BRA instruction, BSR is restricted to 8-bit or 16-bit offsets

Vous aimerez peut-être aussi