You are on page 1of 62

Chapter 3

Assembly Language
Programming
Topics to Cover: (Ch-3)
 Software: The Microcomputer Program
 Assembly Language Programming
Development on the PC
 The Instruction Set
 The MOV Instruction
 Addressing Modes
SOFTWARE: THE
MICROCOMPUTER PROGRAM
A microcomputer does not know how to process
data. It must be told exactly
•what to do
•where to get data
•what to do with the data
•where to put the results when it is done

These are the jobs of the software


in a microcomputer system
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• The sequence of commands used to tell a
microcomputer what to do is called a program.

• Each command in a program is an instruction.

A program may be simple and


include just a few instructions, or it
may be very complex and contain
more than 100,000
instructions
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• The native language of the original IBM PC is
the machine language of the 8088
microprocessor.
• Programs must always be coded in this
machine language before they can be executed
by the microprocessor.
• A program written in machine language is often
referred to as machine code. When expressed
in machine code, an instruction is encoded
using 0s and 1s.
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• A single machine language instruction can take
up one or more bytes of code.

• Even though the 8088 understands only


machine code, it is almost impossible to write
programs directly in machine language. For this
reason, programs are normally written in other
languages, such as 8088 assembly language or
a high-level language such as C
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• An example of an instruction written in 8088
assembly language is
ADD AX, BX
• This instruction says, “Add the contents of
registers BX and AX together and put the sum
in register AX.” AX is called the destination
operand, because it is the place where the
result ends up, and BX is called the source
operand
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• An instruction can be divided into two parts: its
operation code (opcode) and its operands.
• The opcode is the part of the instruction that
identifies the operation that is to be
performed.
• Operands describe the data that are to be
processed as the microprocessor carries out the
operation specified by the opcode.
• A general format for an assembly language
statement is
LABEL: INSTRUCTION ;Comment
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• Assembly language programs cannot be directly
run on the 8088. They must still be translated
to an equivalent machine language program for
execution by the 8088.
• This conversion is done automatically by
running the source program through a program
known as an assembler.
• The machine language output produced by the
assembler is called object code.
SOFTWARE: THE
MICROCOMPUTER PROGRAM
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• High-level languages make writing programs
even easier. The instructions of a high-level
language are English-like statements.
• In a language such as C, high level commands,
such as FOR, IF, and WHILE, are provided.
These commands no longer correspond to a
single machine language statement.
• The program that converts high-level-language
statements to machine code instructions is
called a compiler.
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• If it is so much easier to write programs with a
high-level language,
A compiled whyimplementation
machine code is it importantof a to now
how to program
program that was the 8088
written in a in its assembly
high-level language
language?
results in many more machine language instructions
than a hand-written assembly language version of the
program.

• Programtwo is written in a high-level


key benefits language,
derived from such
writing as C, itinmust be
programs
compiled into machine code before itlanguage:
assembly can be run on the 8088.
• The general nature •take with up
which
lesscompilers
memory mustspacebe designed usually
results in less efficient machine
•it will code.
execute faster
• That is, the quality of the machine code produced for the program
depends on the quality of the compiler program in use
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• What else to outweigh the additional
effort necessary to write the program in
assembly language instead of a high-level
language?
• major uses of assembly language
programming is in real-time applications
• real-time applications: the task required by
the application must be completed before any
other input to the program can occur that will
alter its operation
e.g. PC hard-drive operation controller code
is written in Assembly.
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• Assembly language is not only important
not only for controlling the microcomputer
system’s hardware devices but also for
performing pure software operations.
• applications frequently require the
microcomputer to search through a large
table of data in memory looking for a special
string of characters, such as a person’s name.
• Writing a program in a high-level language
can allow the application to perform this type
of operation easily; however, for large tables
of data the search will take very long.
SOFTWARE: THE
MICROCOMPUTER PROGRAM
• Not all parts of an application require real-time
performance. For this reason, it is a common
practice to mix, in the same program:
• routines developed through a high-level
language and routines developed with
assembly language

assembly language to write those parts of the


application that must perform real-time
operations while high-level language to
write those parts that are not time critical
ASSEMBLY LANGUAGE PROGRAM
DEVELOPMENT ON THE PC
 An assembly language program is
written to solve a specific problem.
This problem is known as the
application.
 To develop a program that implements
an application, the programmer goes
through a multistep process.
ASSEMBLY LANGUAGE PROGRAM
DEVELOPMENT ON THE PC
 Program
Development
Cycle
Compiler translates it to assembly
program (low-level language)
An assembler translates the assembly
program into machine code (object).

A linker tool is used to link all the parts


of the program together for execution
(executable machine code).

A loader loads all of them into memory


and then the program is executed.
ASSEMBLY LANGUAGE PROGRAM
DEVELOPMENT ON THE PC
 Flowchart of a
block move
program
ASSEMBLY LANGUAGE PROGRAM
DEVELOPMENT ON THE PC
 THE MOV INSTRUCTION
 MOV D, S
ASSEMBLY LANGUAGE PROGRAM
DEVELOPMENT ON THE PC
 THE MOV
INSTRUCTION
 MOV D, S

(After Execution of Command)


ADDRESSING MODES
When the 8088 executes an instruction, it
performs the specified function on data. This
data, called operand(s):
 may be part of the instruction
 may reside in one of the internal registers of the
microprocessor
 may be stored at an address in memory
 may be held at an I/O port

An addressing mode is a method of specifying an


operand.
Addressing Modes
Opcode -> Machine Code of Operation
Addressing Modes:
Register Operand Addressing Modes
Addressing Modes:
Register Operand Addressing Modes
Addressing Modes:
Register Operand Addressing Modes
Addressing Modes:
Register Operand Addressing Modes
Addressing Modes
Addressing Modes:
Immediate Operand Addressing Modes
• If an operand is part of the instruction instead
of the contents of a register or memory
location, it represents immediate operand

• MOV AL, 15H


• Operand can be 8 bits (Imm8) or 16 bits
(Imm16) in length, is encoded as part of
the instruction.

• Data are encoded directly into the instruction


Addressing Modes:
Immediate Operand Addressing Modes
Addressing Modes:
Immediate Operand Addressing Modes
Addressing Modes:
Immediate Operand Addressing Modes
Addressing Modes
Addressing Modes:
Memory Operand Addressing Mode

To reference an operand in memory, the


8088 must calculate the Physical Address
(PA) of the operand and then initiate a read
or write operation of this storage location
Addressing Modes:
Memory Operand Addressing Mode

Physical address is computed from a


segment base address (SBA) and an
effective address (EA).

•SBA identifies the starting location of the


segment in memory

•EA represents the offset of the operand


from the beginning of this segment of
memory
Addressing Modes:
Memory Operand Addressing Mode
• SBA and EA are combined within the 8088
to form the logical address SBA:EA
• Physical Address is computed from these
two values
• value of the EA can be specified in a
variety of ways:
• e.g: encode the effective address of
the operand directly in the instruction
(direct addressing mode)
Addressing Modes:
Memory Operand Addressing Mode
• An effective address can be made up
from as many as three elements: the base,
index, and displacement:
• EA = Base + Index + Displacement
Addressing Modes:
Memory Operand Addressing Mode
• Not all these elements are always
used in the effective address
calculation.
• In fact, a number of memory addressing
modes are defined by using various
combinations of these elements.
Addressing Modes:
Memory Operand Addressing Mode

Direct Addressing Mode


Addressing Modes:
Memory Operand Addressing Mode

Direct Addressing Mode


20-bit physical address of the operand in
memory is normally obtained from logical
address DS:EA

By using a segment override prefix (SEG)


in the instruction, any of the four segment
registers can be referenced

Segment Override Prefix example:


MOV AX, ES:[1234]
Addressing Modes:
Memory Operand Addressing Mode
Direct Addressing Mode
Addressing Modes:
Memory Operand Addressing Mode
Direct Addressing Mode
Addressing Modes
Addressing Modes:
Memory Operand Addressing Mode

Register Indirect Addressing Mode


Addressing Modes:
Memory Operand Addressing Mode
Register Indirect Addressing Mode
Addressing Modes:
Memory Operand Addressing Mode
Register Indirect Addressing Mode
Addressing Modes
Addressing Modes:
Memory Operand Addressing Mode
Based Addressing Mode
• Effective
address of the
operand is
obtained by
adding a
direct or
indirect
displacement
to the contents
of either base
register BX or
base pointer
register BP
Addressing Modes:
Memory Operand Addressing Mode
Based Addressing Mode
Addressing Modes:
Memory Operand Addressing Mode
Based Addressing Mode
Addressing Modes
Addressing Modes:
Memory Operand Addressing Mode
Indexed Addressing Mode
• value of the displacement as a pointer to
the starting point of an array of data in
memory
• the contents of the specified register as an
index that selects the specific element in the
array that is to be accessed
Addressing Modes:
Memory Operand Addressing Mode
Indexed Addressing Mode
Addressing Modes:
Memory Operand Addressing Mode
Indexed Addressing Mode
Addressing Modes:
Memory Operand Addressing Mode
Indexed Addressing Mode
Addressing Modes
Addressing Modes:
Memory Operand Addressing Mode
Based - Indexed Addressing Mode
Combining the based addressing mode and the indexed addressing
mode results in a new, more powerful mode known as based-indexed
addressing mode
Can be used to access complex data structures such as two-dimensional
arrays

can be used to access elements


in an mxn array of data
Addressing Modes:
Memory Operand Addressing Mode
Based - Indexed Addressing Mode
• can be used to access elements in
an mxn array of data

• displacement, which is a fixed


value, locates the array in
memory

• base register specifies the m


coordinate of the array, and the
• index register identifies the n
coordinate

• Simply changing the values in the


base and index registers permits
access to any element in the array
Addressing Modes:
Memory Operand Addressing Mode
Based - Indexed Addressing Mode
Addressing Modes:
Memory Operand Addressing Mode
Based - Indexed Addressing Mode
Addressing Modes:
Memory Operand Addressing Mode
Based - Indexed Addressing Mode
Addressing Modes

You might also like