CH3 EC304.pdf

156
Chapter 3 Introduction To Assembly Language (Instruction Sets)

Transcript of CH3 EC304.pdf

Page 1: CH3 EC304.pdf

Chapter 3 Introduction To Assembly Language (Instruction Sets)

Page 2: CH3 EC304.pdf

Language

LANGUAGE is a medium of communication between two or more individuals or parties.

The most important criteria in the communication is the message it brings must be effectively delivered, this is achieved only if the language is understood by the both parties.

What if the language is not compatible to both parties? Then a translator is needed. For instance, a Japanese requires a Japanese-Malays translator to communicate to a Malaysian

Page 3: CH3 EC304.pdf

Introduction

Microprocessor run the operation based on the program that write by the programmer

Three types of program language High Level Language – C,C++, BASIC, PASCAL, FOTRAN, JAVA

Low Level Language –Assembly Language

Machine Language

How ever microprocessor does not understand what language write by the programmer Microprocessor only understand machine language – binary

number; High Level Language - (compiler)

Low Level Language - (assembler)

Page 4: CH3 EC304.pdf
Page 5: CH3 EC304.pdf

Block Diagram

Japanese-Malay

translator

Japanese

Malay

Compiler

High-level Language

Machine Language

Assembler

Assembly Language

Machine Language

High level

Middle level

Low level

Programmers

Computer

BASIC

COMMUNICATION

COMPUTER SYSTEM

COMMUNICATION

Language

Language

Translator

Page 6: CH3 EC304.pdf

High level language

(Basic, Pascal and C)

Programming instructions in the

normal English words, very friendly

to programmers..

Middle level language (Assembly

language)

Low level language

(Machine language)

ASSEMBLER

COMPILERS

Implemented by computer system

Programming instructions in the

binary codes, understand directly by

computer system.

Programming instructions in the

English like abbreviation, still not so

user friendly.

Human being (Programmers)

Page 7: CH3 EC304.pdf

High Level Language

Programming language close to human language (such as

English) is called high-level language. Examples of high-level language such as FORTRAN, COBOL, BASIC, PASCAL, C + +, JAVA

High level language similar to English. Instruction program written using English words, such as print, and other inputs.

But every high-level language has its own rules or grammar for writing instruction program. Regulations referred to as syntax.

Program written in the levels should be translated into machine code prior to running.

Every high level language has its own translator program.

Page 8: CH3 EC304.pdf

Assembly Language programming

Assembly Language program consists of series of lines of Assembly language instructions.

Deals directly with the internal structure of CPU

Instruction consists of a mnemonic and two operands.

Mnemonics (abbreviations) are used for the instructions of the processor

Names for memory locations

Labels

Symbols

Design of processor must be known

Page 9: CH3 EC304.pdf

Sample of assembly language

program

Page 10: CH3 EC304.pdf

Machine Language

Programs consist of 0s and 1s are called machine language.

Assembly Languages provided mnemonics for machine code

instructions.

Mnemonics refer to codes and abbreviations to make it easier

for the users to remember.

Page 11: CH3 EC304.pdf

Machine language

+1300042774

+1400593419

+1200274027

Assembly Language

LOAD BASEPAY

ADD OVERPAY

STORE GROSSPAY

High-Level Language

grosspay = basepay +

overTimepay

Instructions are

binary codes.

Understand directly

by computer

system.

Cumbersome for

human.

Instructions are

normal English

words. Most

friendly to human

(Programmers).

Instructions are

English- like

abbreviation. More

friendly to human

(Programmers).

shows the instructions of three different

languages that perform the same task.

Page 12: CH3 EC304.pdf
Page 13: CH3 EC304.pdf

Programming in Assembler

Page 14: CH3 EC304.pdf

Try and translate this:

00110000001111000000000000010010

Page 15: CH3 EC304.pdf

Try and translate this:

00110000001111000000000000010010

MOVE.W #$12,D0

00 11 000 000 111 100 0000 0000 0001 0010

(Data register direct

addressing mode, D0)

Page 16: CH3 EC304.pdf

A Simple Example

Write the instructions to add 50 and 72 and

save the answer in register C. High Level - Java, C

X = 50 + 72

Assembly Language – Intel 8085

MVI A, 50

MVI B, 72

ADD B

MOV C, A

Motorola 68000

MOVE.W #$50,D0

MOVE.W #$72,D1

MULU D1,D0

Page 17: CH3 EC304.pdf

A Simple Example (cont)

Machine Language HEX BINARY OPERAND

3E 0011 1100 MVI A

32 0011 0010 50

06 0000 0110 MVI B

48 0100 1000 72

80 1000 0000 ADD B

4F 0100 1111 MOV C, A

76 0111 0110 HLT

Page 18: CH3 EC304.pdf

The Assembler

Machine Language:

00110000001111000

01100100011110000

00000000110100

Assembly Language:

MOVE.W #$12,D0

MOVE.W #$34,D1

MULU D1,D0

Assembler

Page 19: CH3 EC304.pdf

Data Representation Method

M68k can accept many types of data:

Binary

Octal

Hexadecimal

Decimal

Character

Page 20: CH3 EC304.pdf

Binary

Binary: start with %

Example:

Move binary value 10010101 to D0.

MOVE.B #%10010101,D0

D0 = 0 0 0 0 0 0 9 5

10010101B = 95H

Page 21: CH3 EC304.pdf

Octal

Octal: start with @

Example:

Move octal value 45 to D0.

MOVE.B #@45,D0

D0 = 0 0 0 0 0 0 2 5

45O = 25H

Page 22: CH3 EC304.pdf

Hexadecimal

Hexadecimal: start with $

Example:

Move hexadecimal value FE to D0.

MOVE.B #$FE,D0

D0 = 0 0 0 0 0 0 F E

Page 23: CH3 EC304.pdf

Decimal

Decimal: no need to put any symbols.

Example:

Move decimal value 10 to D0.

MOVE.B #10,D0

D0 = 0 0 0 0 0 0 0 A

10D = 0AH

Page 24: CH3 EC304.pdf

ASCII Characters

Characters: Enclose character in single

quotes (‘ ’).

Example:

Move ASCII character ‘A’ to D0.

MOVE.B #’A’,D0

D0 = 0 0 0 0 0 0 4 1

A = 41H

Page 25: CH3 EC304.pdf

Easy68k Interface

LABEL

INSTRUCTION

VARIABLES/PARAMETERS

ANYTHING PAST THIS IS

IGNORED, TREATED AS COMMENT.

PROGRAM DESCRIPTION

Page 26: CH3 EC304.pdf
Page 27: CH3 EC304.pdf

Programming in Easy 68k

Easy68k divides program into 4 columns: Label:

Marks memory locations using characters.

Easy reference.

Op-Code - Instruction: What instruction to execute.

Operand(s): Variables/Parameters: Usually specifies source & destination.

May specify parameters as well.

Comment: Used to describe flow of program,

Page 28: CH3 EC304.pdf

Simulation Window

Shows status of internal

M68k registers.

Memory address

where instruction

is stored.

Machine code generated

by assembler.

Press here to execute

programs step-by-step.

Press here to restart

program execution.

Page 29: CH3 EC304.pdf

MOVE.B #9,D0 instruction:

Is on line 11 in M68k source file.

Is stored in memory address $1000.

Its machine code is $103C0009 (00010000001111000000000000001001).

Page 30: CH3 EC304.pdf

Specify Start/End of Program

M68k needs to know where to start executing

instructions, where to stop.

Specified using ORG (Origin), END (End).

Value of ORG loaded into PC, execution

starts there.

Page 31: CH3 EC304.pdf

Format: Start/End Program

ORG $1000

END $1000

Program Instructions

PC loaded with $1000,

starts executing from here.

END specifies ending of program.

Page 32: CH3 EC304.pdf

Use Labels

Any memory location may be given labels.

Easier to refer to specific locations:

Useful in for loops, subroutines, branch

commands.

Page 33: CH3 EC304.pdf

Using Labels - Example

START ORG $1000

MOVE.B #$2000,A0

MOVE.B #$10, D1

CLR.B D0

LABEL1 ADD.B (A0)+, D0

SUB.B #1, D1

BNE LABEL1

END START

Page 34: CH3 EC304.pdf

Assembler format

Page 35: CH3 EC304.pdf

Concept of programming

A program is a means to perform tasks or solve problems.

Thus in the process of program building consists several essential steps:

Definition of problem (Penyelesaian Masalah)

Logical design (Pseudo code/Flow chart)

Programming.

Test run the program.

Documentation of the program.

Page 36: CH3 EC304.pdf

Flowchart

Page 37: CH3 EC304.pdf

Flowchart

Graphical method to plan flow of our

programs.

Shows program’s step-by-step operation.

Easy to understand and analyze.

Can be used to write organized programs.

Page 38: CH3 EC304.pdf

Flowchart

Basic shapes:

Terminator.

Process.

Decision.

Input/Output.

Connectors.

Page 39: CH3 EC304.pdf

Basic Shapes – Terminator

Indicates beginning and end of flowchart.

Once at beginning, once at end.

Examples:

START FINISH

Page 40: CH3 EC304.pdf

Basic Shapes - Process

Describes actions to be done.

Represented as rectangles.

Short description of process in rectangle.

Example:

A = A + B Reset Ports Clear D0

Page 41: CH3 EC304.pdf

Basic Shapes - Decision

Shows alternative program flow based on

condition.

Represented as diamond shape.

Should have 2 arrows, representing TRUE

and FALSE program flows.

Can be used in “if…else”, “while”, and “for”

situations.

Page 42: CH3 EC304.pdf

Basic Shapes - Decision

Examples:

D0 = 3?

TRUE FALSE

Port is active? TRUE

FALSE

Page 43: CH3 EC304.pdf

Basic Shapes – Input/Output

Shows the process of inputting or outputting

data.

Represented using rhombus.

Examples:

Input D0 Show calculation

results

Page 44: CH3 EC304.pdf

Basic Shapes - Connectors

Used to link large process flows together.

Represented using circles, with numbers

inside.

Numbers indicate connection.

Examples:

1 3

Page 45: CH3 EC304.pdf

Example: Connector

START

Input D0

Input D1

1

D0 = D0 x D1

1

FINISH

Page 46: CH3 EC304.pdf

Writing the Program

Once flowchart is complete, write code to

implement program.

Follow program flow closely.

Check and fix problems if necessary.

Page 47: CH3 EC304.pdf

Example: Calculate Area of Rectangle

D0

D1 START

Input D0

Input D1

1

D0 = D0 x D1

1

FINISH

Page 48: CH3 EC304.pdf

Translation to Assembly

START

Input D0

Input D1

D0 = D0 x D1

FINISH

ORG $1000

MOVE.W #$12,D0

MOVE.W #$34,D1

MULU D1,D0

END

Page 49: CH3 EC304.pdf

Complete Program

START ORG $1000

MOVE.W #$12,D0

MOVE.W #$34,D1

MULU D1,D0

END START

Page 50: CH3 EC304.pdf

Example: Add 10 Numbers Together

START

Input numbers

into memory

locations.

D0 = 10

Load starting

address into A0

Clear D1

FINISH

D0 = 0?

D1 = D1 + (A0)

FALSE

Go to next

memory location

D0 = D0 - 1

TRUE

Page 51: CH3 EC304.pdf

START

Input numbers

into memory

locations.

Load starting

address into A0

ORG $1000

MOVE.W #$11,$1100

MOVE.W #$22,$1102

MOVE.W #$33,$1104

MOVE.W #$44,$1106

MOVE.W #$55,$1108

MOVE.W #$66,$110A

MOVE.W #$77,$110C

MOVE.W #$88,$110E

MOVE.W #$99,$1110

MOVE.W #$11,$1112

MOVEA.L #$1100,A0

Page 52: CH3 EC304.pdf

D0 = 10

Clear D1

D0 = 0?

D1 = D1 + (A0)

FALSE

D0 = D0 - 1

MOVE.B #10,D0

CLR.B D1

Go to next

memory location

LOOP…BNE LOOP, CMP.B #$0,D0

ADD.W (A0),D1

ADDA.L #$2,A0

FINISH

SUB.B #$1,D0

END

Page 53: CH3 EC304.pdf

Complete Program

START ORG $1000

MOVE.W #$11,$1100

MOVE.W #$22,$1102

MOVE.W #$33,$1104

MOVE.W #$44,$1106

MOVE.W #$55,$1108

MOVE.W #$66,$110A

MOVE.W #$77,$110C

MOVE.W #$88,$110E

MOVE.W #$99,$1110

MOVE.W #$11,$1112

MOVEA.L #$1100,A0

MOVE.B #10,D0

CLR.B D1

LOOP ADD.W (A0),D1

ADDA.L #$2,A0

SUB.B #$1,D0

CMP.B #$0,D0

BNE LOOP

END START

Page 54: CH3 EC304.pdf

some important

terminologies

Page 55: CH3 EC304.pdf

Terminologies

Machine code

Mnemonic

Source File

Object Files, List File, Hex file

Assembler

Single-line assembler

Cross Assembler

Pseudo-opcode

Page 56: CH3 EC304.pdf

Machine code

Programming code written in a machine language that can be executed directly by a machine (computer) without any conversion or translation.

A low-level code interpreted and converted from high-level source code and understood only by the machine.

Machine code is transported to the system processor when a specific task, application or program executes even the smallest process.

Page 57: CH3 EC304.pdf
Page 58: CH3 EC304.pdf

A mnemonic (pronounced "nemonic") is a pattern that can be used as an aid for memorizing information. Most often, this pattern consists of letters or words. Mnemonic is an abbreviation of the words used in an assembly

language program to represent an instruction.

Depending on the microprocessor types, for example in Motorola mnemonic is different from Intel

Contains the machine language instructions, translated into machine code

MOV Move

MVI Move Immediate

LDA Load Accumulator Directly from Memory

Mnemonic

Page 59: CH3 EC304.pdf

Source code

In computer science - source code is any collection of

computer instructions (possibly with comments) written

using some human-readable computer language, usually

as text

The source code of a program is specially designed to

facilitate the work of computer programmers, who

specify the actions to be performed by a computer

mostly by writing source code.

The source code is often transformed by a compiler

program into low-level machine code understood by the

computer

Page 60: CH3 EC304.pdf

Object Files

Compilers and assemblers create object files

containing the generated binary code and

data for a source file.

Containing object code, meaning relocatable

format machine code that is usually not

directly executable

Page 61: CH3 EC304.pdf

List File

A list file is simply a text file that contains a

list of other files.

Page 62: CH3 EC304.pdf

Hex File

The Intel hex (ihex) generally known as hex file, is a

format used to store machine language code in

hexadecimal form.

It is widely used format to store programs to be

transferred to microcontrollers, ROM and EEPROM.

The compilers convert the programs written in assembly,

C etc into corresponding hex files, which are dumped

into the controllers using burners/programmers.

Page 63: CH3 EC304.pdf

Hex File…

The microcontroller understands machine

language consisting of zeroes and ones. It’s

difficult rather practically impossible for humans

to write codes in zeros and ones.

Hence we use some high level languages like C,

C++, Java, etc. And later a compiler is used to

convert these codes into machine language

which are stored in a hex file format.

A hex file is a text file with the extension .hex.

Page 64: CH3 EC304.pdf

Assembler

A computer program which translates from assembly

language to an object file or machine language format

A program that takes basic computer instructions and

converts them into a pattern of bits that the computer's

processor can use to perform its basic operations.

Some people call these instructions assembler language

and others use the term assembly language.

Page 65: CH3 EC304.pdf

Single-line assembler

single-line mean a program that doesn't

assemble an entire file but only one

statement.

That is generally called "DEBUG.EXE" WIN XP debug:

C:\>debug

-a100

117E:0100 rol eax,3

^ Error

117E:0100

-q

Page 66: CH3 EC304.pdf
Page 67: CH3 EC304.pdf

Cross assembler

An assembler that generates machine language for a

different type of computer than the one the assembler is

running in.

It is used to develop programs for computers on a chip

or microprocessors used in specialized applications that

are either too small or are otherwise incapable of

handling the development software.

Common methods involve transmitting an exact byte-by-

byte copy of the machine code or an ASCII

representation of the machine code in a portable format

(such as Motorola or Intel hexadecimal) through a

compatible interface to the target system for execution.

Page 68: CH3 EC304.pdf

pseudo-opcode

An assembler directive or pseudo-opcode is a command

given to an assembler "directing it to perform operations

other than assembling instructions."

Sometimes the term pseudo-opcode is reserved for

directives that generate object code

Page 69: CH3 EC304.pdf
Page 70: CH3 EC304.pdf
Page 71: CH3 EC304.pdf

Summary of the terminologies

Page 72: CH3 EC304.pdf

Assembly Language (Easy68k)

Assembler to generate machine code.

Object file (Motorola: S-file).

Contains machine code.

Linker sometimes used for big projects:

Links together multiple S-files.

Creates single S-file from combined files.

Page 73: CH3 EC304.pdf

Assembly

File #1

Assembly

File #2

Assembly

File #3

Assembly

File #4

ASSEMBLER

Object

File #1

Object

File #2

Object

File #3

Object

File #4

LINKER

Final Object

File

Listing File

Page 74: CH3 EC304.pdf

Source Code (X68)

Page 75: CH3 EC304.pdf

Object File (S-File)

Page 76: CH3 EC304.pdf

Listing File

Page 77: CH3 EC304.pdf

Exercise 1:

Give the term

Assembler

Source code

Object code

Cross Assembler

IDE (integrated Development Environment)

Page 78: CH3 EC304.pdf

ANSWER

Page 79: CH3 EC304.pdf
Page 80: CH3 EC304.pdf
Page 81: CH3 EC304.pdf
Page 82: CH3 EC304.pdf
Page 83: CH3 EC304.pdf
Page 84: CH3 EC304.pdf
Page 85: CH3 EC304.pdf

Exercise 2

Give the term

Label

Op-code

Operands

Comment

Linker

Page 86: CH3 EC304.pdf

ANSWER

Label

Op-code

Operands

Comment

Linker

Page 87: CH3 EC304.pdf

END

Page 88: CH3 EC304.pdf

MC6800 Instruction Group

Page 89: CH3 EC304.pdf

Addressing Mode

Page 90: CH3 EC304.pdf
Page 91: CH3 EC304.pdf
Page 92: CH3 EC304.pdf
Page 93: CH3 EC304.pdf
Page 94: CH3 EC304.pdf
Page 95: CH3 EC304.pdf
Page 96: CH3 EC304.pdf
Page 97: CH3 EC304.pdf
Page 98: CH3 EC304.pdf
Page 99: CH3 EC304.pdf
Page 100: CH3 EC304.pdf
Page 101: CH3 EC304.pdf
Page 102: CH3 EC304.pdf
Page 103: CH3 EC304.pdf
Page 104: CH3 EC304.pdf
Page 105: CH3 EC304.pdf

Machine Code Generation

Page 106: CH3 EC304.pdf
Page 107: CH3 EC304.pdf
Page 108: CH3 EC304.pdf
Page 109: CH3 EC304.pdf
Page 110: CH3 EC304.pdf
Page 111: CH3 EC304.pdf
Page 112: CH3 EC304.pdf
Page 113: CH3 EC304.pdf
Page 114: CH3 EC304.pdf
Page 115: CH3 EC304.pdf
Page 116: CH3 EC304.pdf
Page 117: CH3 EC304.pdf
Page 118: CH3 EC304.pdf
Page 119: CH3 EC304.pdf
Page 120: CH3 EC304.pdf
Page 121: CH3 EC304.pdf
Page 122: CH3 EC304.pdf
Page 123: CH3 EC304.pdf
Page 124: CH3 EC304.pdf
Page 125: CH3 EC304.pdf
Page 126: CH3 EC304.pdf
Page 127: CH3 EC304.pdf
Page 128: CH3 EC304.pdf
Page 129: CH3 EC304.pdf
Page 130: CH3 EC304.pdf
Page 131: CH3 EC304.pdf
Page 132: CH3 EC304.pdf
Page 133: CH3 EC304.pdf
Page 134: CH3 EC304.pdf
Page 135: CH3 EC304.pdf
Page 136: CH3 EC304.pdf
Page 137: CH3 EC304.pdf
Page 138: CH3 EC304.pdf
Page 139: CH3 EC304.pdf
Page 140: CH3 EC304.pdf
Page 141: CH3 EC304.pdf
Page 142: CH3 EC304.pdf
Page 143: CH3 EC304.pdf
Page 144: CH3 EC304.pdf
Page 145: CH3 EC304.pdf
Page 146: CH3 EC304.pdf
Page 147: CH3 EC304.pdf
Page 148: CH3 EC304.pdf
Page 149: CH3 EC304.pdf

END CH 3

Page 150: CH3 EC304.pdf

Assembler

Page 151: CH3 EC304.pdf

Single-line assembler

Page 152: CH3 EC304.pdf

Cross-assembler

Page 153: CH3 EC304.pdf

Pseudo-opcode

Page 154: CH3 EC304.pdf

Determine four fields of an

assembly language instruction.

Page 155: CH3 EC304.pdf

The instruction set and

addressing modes

Page 156: CH3 EC304.pdf

Identify groups of instruction sets of the

68000/8086

microprocessor.