Download - CH3 EC304.pdf
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
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
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)
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.
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.
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.
Programming in Assembler
Try and translate this:
00110000001111000000000000010010
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)
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 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
The Assembler
Machine Language:
00110000001111000
01100100011110000
00000000110100
Assembly Language:
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
D0 = 0 0 0 0 0 0 9 5
10010101B = 95H
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
Hexadecimal
Hexadecimal: start with $
Example:
Move hexadecimal value FE to D0.
MOVE.B #$FE,D0
D0 = 0 0 0 0 0 0 F E
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
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
Easy68k Interface
LABEL
INSTRUCTION
VARIABLES/PARAMETERS
ANYTHING PAST THIS IS
IGNORED, TREATED AS COMMENT.
PROGRAM DESCRIPTION
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,
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.
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
END $1000
Program Instructions
PC loaded with $1000,
starts executing from here.
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 $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
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 program’s 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 “if…else”, “while”, and “for”
situations.
Basic Shapes - Decision
Examples:
D0 = 3?
TRUE FALSE
Port is active? 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:
1 3
Example: Connector
START
Input D0
Input D1
1
D0 = D0 x D1
1
FINISH
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
D0
D1 START
Input D0
Input D1
1
D0 = D0 x D1
1
FINISH
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
Complete Program
START ORG $1000
MOVE.W #$12,D0
MOVE.W #$34,D1
MULU D1,D0
END START
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
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
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
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
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.
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
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. 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.
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-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.
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.
Linker sometimes used for big projects:
Links together multiple S-files.
Creates single S-file from combined files.
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
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.