Вы находитесь на странице: 1из 156

Chapter 3

Introduction To Assembly Language (Instruction Sets)

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

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)

Block Diagram
BASIC COMMUNICATION COMPUTER SYSTEM COMMUNICATION Programmers Japanese Language Assembly Language Translator Japanese-Malay translator Language Malay Machine Language Machine Language Low level Compiler Assembler

High-level Language

High level

Middle level

Computer

Human being (Programmers)

COMPILERS

High level language (Basic, Pascal and C) Programming instructions in the normal English words, very friendly to programmers.. Middle level language (Assembly language) Programming instructions in the English like abbreviation, still not so user friendly. ASSEMBLER

Low level language (Machine language) Programming instructions in the binary codes, understand directly by computer system.

Implemented by computer system

High Level Language

Programming language close to human language (such as English) is called high-level language. Examples of highlevel 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.

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

Sample of assembly language program

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.

shows the instructions of three different languages that perform the same task.
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 English- like abbreviation. More friendly to human (Programmers).

Instructions are normal English words. Most friendly to human (Programmers).

Programming in Assembler

Try and translate this:

00110000001111000000000000010010

Try and translate this:


00110000001111000000000000010010

MOVE.W #$12,D0

00

11

000 000
(Data register direct addressing mode, D0)

111 100 0000 0000 0001 0010

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

A Simple Example (cont)

Machine Language
HEX 3E 32 06 48 80 4F 76 BINARY 0011 1100 0011 0010 0000 0110 0100 1000 1000 0000 0100 1111 0111 0110 OPERAND MVI A 50 MVI B 72 ADD B MOV C, A HLT

The Assembler

Assembly Language:

Machine Language:
00110000001111000 01100100011110000 00000000110100

MOVE.W #$12,D0 MOVE.W #$34,D1 MULU D1,D0


Assembler

Data Representation Method

M68k can accept many types of data:

Binary Octal Hexadecimal Decimal Character

Binary

Binary: start with % Example:

Move binary value 10010101 to D0.

MOVE.B

#%10010101,D0
10010101B = 95H

D0 =

Octal

Octal: start with @ Example:

Move octal value 45 to D0.

MOVE.B

#@45,D0
45O = 25H

D0 =

Hexadecimal

Hexadecimal: start with $ Example:

Move hexadecimal value FE to D0.

MOVE.B

#$FE,D0

D0 =

Decimal

Decimal: no need to put any symbols. Example:

Move decimal value 10 to D0.

MOVE.B

#10,D0
10D = 0AH

D0 =

ASCII Characters

Characters: Enclose character in single quotes ( ). Example:

Move ASCII character A to D0.


MOVE.B #A,D0
A = 41H

D0 =

Easy68k Interface
PROGRAM DESCRIPTION

ANYTHING PAST THIS IS IGNORED, TREATED AS COMMENT. LABEL INSTRUCTION

VARIABLES/PARAMETERS

Programming in Easy 68k

Easy68k divides program into 4 columns:

Label:

Marks memory locations using characters. Easy reference. What instruction to execute. Usually specifies source & destination. May specify parameters as well. Used to describe flow of program,

Op-Code - Instruction:

Operand(s): Variables/Parameters:

Comment:

Simulation Window
Press here to execute programs step-by-step. Press here to restart program execution.

Shows status of internal M68k registers.

Memory address where instruction is stored.

Machine code generated by assembler.

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).

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.

Format: Start/End Program


ORG $1000
PC loaded with $1000, starts executing from here.

Program Instructions

END $1000

END specifies ending of program.

Use Labels

Any memory location may be given labels. Easier to refer to specific locations:

Useful in for loops, subroutines, branch commands.

Using Labels - Example


START ORG MOVE.B MOVE.B CLR.B LABEL1 ADD.B SUB.B BNE END START $1000 #$2000,A0 #$10, D1 D0 (A0)+, D0 #1, D1 LABEL1

Assembler format

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.

Flowchart

Flowchart

Graphical method to plan flow of our programs. Shows programs step-by-step operation. Easy to understand and analyze. Can be used to write organized programs.

Flowchart

Basic shapes:

Terminator. Process. Decision. Input/Output. Connectors.

Basic Shapes Terminator

Indicates beginning and end of flowchart. Once at beginning, once at end. Examples:

START

FINISH

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

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 ifelse, while, and for situations.

Basic Shapes - Decision

Examples:

D0 = 3?

Port is active?
TRUE

FALSE

TRUE

FALSE

Basic Shapes Input/Output

Shows the process of inputting or outputting data. Represented using rhombus. Examples:

Input D0

Show calculation results

Basic Shapes - Connectors

Used to link large process flows together. Represented using circles, with numbers inside. Numbers indicate connection. Examples:

Example: Connector
START
1

Input D0

D0 = D0 x D1

Input D1 FINISH 1

Writing the Program

Once flowchart is complete, write code to implement program. Follow program flow closely. Check and fix problems if necessary.

Example: Calculate Area of Rectangle


START 1 D1

D0
Input D0 D0 = D0 x D1

Input D1 FINISH

Translation to Assembly
START ORG $1000

Input D0

MOVE.W

#$12,D0

Input D1

MOVE.W

#$34,D1

D0 = D0 x D1

MULU

D1,D0

FINISH

END

Complete Program
START ORG $1000

MOVE.W MOVE.W
MULU

#$12,D0 #$34,D1
D1,D0

END

START

Example: Add 10 Numbers Together


START
TRUE

Input numbers into memory locations. Load starting address into A0 D0 = 10

D0 = 0?
FALSE

D1 = D1 + (A0)

FINISH

Go to next memory location


D0 = D0 - 1 Clear D1

START

ORG

$1000 #$11,$1100 #$22,$1102 #$33,$1104 #$44,$1106 #$55,$1108 #$66,$110A #$77,$110C #$88,$110E #$99,$1110 #$11,$1112

Input numbers into memory locations.

MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W

Load starting address into A0

MOVEA.L

#$1100,A0

D0 = 10 Clear D1 D0 = 0?
FALSE

MOVE.B #10,D0 CLR.B D1

LOOPBNE LOOP, CMP.B #$0,D0


ADD.W (A0),D1

D1 = D1 + (A0)

Go to next memory location


D0 = D0 - 1

ADDA.L #$2,A0
SUB.B #$1,D0

FINISH

END

Complete Program
START ORG MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W MOVE.W $1000 #$11,$1100 #$22,$1102 #$33,$1104 #$44,$1106 #$55,$1108 #$66,$110A #$77,$110C #$88,$110E #$99,$1110 #$11,$1112

MOVEA.L #$1100,A0 MOVE.B #10,D0 CLR.B D1 LOOP ADD.W ADDA.L SUB.B CMP.B BNE END (A0),D1 #$2,A0 #$1,D0 #$0,D0 LOOP START

some important terminologies

Terminologies

Machine code Mnemonic Source File Object Files, List File, Hex file Assembler Single-line assembler Cross Assembler Pseudo-opcode

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.

Mnemonic

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

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

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

List File

A list file is simply a text file that contains a list of other files.

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.

Hex File

The microcontroller understands machine language consisting of zeroes and ones. Its 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.

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.

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

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-bybyte 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.

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

Summary of the terminologies

Assembly Language (Easy68k)

Assembler to generate machine code.

Object file (Motorola: S-file). Contains machine code. Links together multiple S-files. Creates single S-file from combined files.

Linker sometimes used for big projects:

Assembly File #1

Assembly File #2

Assembly File #3

Assembly File #4

ASSEMBLER

Listing File

Object File #1

Object File #2

Object File #3

Object File #4

LINKER

Final Object File

Source Code (X68)

Object File (S-File)

Listing File

Exercise 1:

Give the term

Assembler Source code Object code Cross Assembler IDE (integrated Development Environment)

ANSWER

Exercise 2

Give the term

Label Op-code Operands Comment Linker

ANSWER

Label Op-code Operands Comment Linker

END

MC6800 Instruction Group

Addressing Mode

Machine Code Generation

END CH 3

Assembler

Single-line assembler

Cross-assembler

Pseudo-opcode

Determine four fields of an assembly language instruction.

The instruction set and addressing modes

Identify groups of instruction sets of the 68000/8086 microprocessor.

Вам также может понравиться