MP Lecture 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

Assembly Language

Programming

by
Redae K
1
Introduction
• There are three language levels that can be used
to write a program for a microcomputer.
‒ Machine language: no need of translator
‒ Assembly Language: translator is Assembler
‒ High Level Languages: translator is Compiler

2
Continued…

3
Continued…

Flow of program development

4
Continued…

Assemble, Link and Run a Program


• There are 3 steps to create an executable
assembly language program

5
An Assembly level program Format (MASM)
DATA SEGMENT
--------
------------
-------------
DATA ENDS
CODE SEGMENT
ASSUM ------- ; assumed segments for keeping code & data
START : ----------- ; executable instruction start here
------------
------------
CODE ENDS
END START ; end of executable instruction 6
Continued…
• Notice: Different assembler may have
different syntax for the definition of the key
words !
• Assembly level program will have both
executable and non-executable instructions.
• Non-executable instructions some times
called pseudo codes/directives which are
used to give some information to the
assembler.
7
Continued…
• For example, the data segment area in the
above syntax is non-executable part
whereas the code segment area is the
executable part.
Assembler Directives and operators
• ASSUME :- The ASSUME directive is used
to tell the assembler the name of the logical
segment it should use for a specified
segment.
8
Continued…
• DB--Define Byte:- The DB directive is
used to declare a byte-type variable, or to
set aside one or more storage locations of
type byte in memory.
• TEM-STORAGE DB 100 DUP(?) ; Set
aside 100 bytes of storage in memory and
give it the name TEM_STORAGE, but
leave the 100 bytes uninitialized. Program
instructions will load values into these
locations. 9
Continued…
• DW‐Define Word :- The DW directive is
used to tell the assembler to define a
variable of type word or to reserve storage
locations of type word in memory.
• DD‐Define Double word:- The DD
directive is used to declare a variable of
type double word or to, reserve memory
locations, which can be accessed as type
double word.
10
Syntax of Assembly Language
Statements
• Assembly language instructions are entered one
statement per line. Each instruction (statement)
follows the following format:
• [label:] mnemonic(opcode) [operands] [;comment]
• Opcode:- are the commands to the CPU, telling it what
to do with the operands (data items)
• Operands:- are the data items being manipulated.
• The fields in the square brackets are optional.
11
Addressing Modes Of 8086
• Addressing mode indicates a way of
locating (accessing) data or operands.
• The addressing modes describe the types of
operands and the way they are accessed for
executing an instruction.

12
Continued…

1. Immediate Addressing Mode:


‒ The operands are either inside the
microprocessor or tagged along with the
instruction
‒ The source operand is an immediate number
provided in the instruction
‒ Ex: MOV AX, 5678H

13
Continued…
• Immediate addressing mode can be used to
load information into any of the registers
except the segment registers and flag
registers.
• To move information to the segment
registers, the data must first be moved to a
general purpose registers and then to the
segment register.
‒ Ex: MOV AX, 5678H
MOV DS, AX 14
Continued…
2. Direct Addressing Mode:
– The data is in side the memory (i.e
outside of the CPU)
– One of the operands offset address is
provided directly in the instruction
– Ex: MOV AX, [5000H]
MOV [5000H], AX

15
Continued…
3. Register Addressing Mode:
‒ memory is not accessed when this
addressing mode is executed.
‒ the operands are available at the processor
registers.
‒ all the registers, except IP, may be used in
this mode.
‒ the source and destination registers must
match in size. 16
Continued …
‒ Ex: MOV AX, BX
MOV ES, AX
MOV AL, DH
MOV AL, DX  wrong

17
Continued…
4. Register Indirect Addressing Mode:
‒ The address of the memory location where
the operand resides is held by a register
‒ The registers used for this purpose are SI,
DI and BX
‒ The default segment is DS or ES
‒ Ex: MOV AX, [BX]
MOV CL, [SI]
18
Continued …
5. Register (Base) Relative Addressing Mode:
− One of the operands offset address is calculated
by adding the content of base registers, BX and
BP with the constant specified in the
instruction.
− The default segments used for the calculation of
the physical address are DS for BX and SS for
BP
– Ex: MOV AL, 50H[BX]
MOV AL, [BP]+50H
MOV CX, [BX+50H] 19
Continued…
6. Indexed Addressing Mode:
‒ One of the operands offset address is
specified through one of the indexed
registers
‒ it is a special case of the above discussed
register indirect addressing mode.
‒ DS and ES are the default segments for
index registers SI and DI respectively.
‒ Ex: MOV AL, [SI] => AL  DS:SI
MOV AL, [DI] => AL  ES:DI 20
Continued…
7. Based Indexed Addressing Mode:
‒ One of the operands offset address is computed by
base registers (BX or BP) content with indexed
registers (SI or DI) content
‒ DS and SS are the default segments for index
registers BX and BP respectively.
‒ Ex: MOV AL, [BX][SI]
MOV AH, [BX][DI]
MOV CL, [BP][SI]
MOV CH, [BP][DI]
MOV AX, [SI][DI] is wrong 21
Continued…
8. Relative Based Indexed Addressing Mode:
‒ One of the operands offset address is
computed by adding contents of any one of
the base registers (BX or BP) and any one of
the index registers (SI or DI) along with
constant specified in the instruction.
‒ DS and SS are the default segments for index
registers BX and BP respectively.
‒ Ex: MOV AL, 40H[BX][SI]
22
8086 Instruction Set
• An instruction is a binary pattern designed
in side the microprocessor to perform a
specific function.
• The entire group of instructions that a
microprocessor supports is called
Instruction set.
• Program is a list of statement or instruction
telling the computer what operation to
perform.
23
Classification of instruction set

• Data transfer instructions


• Arithmetic instructions
• Bit manipulation instructions
• Program execution transfer instructions
• String instructions
• Processor control instructions

24
Data transfer instructions
• These instructions are used to transfer data from
source to destination
• The operands can be a constant, memory location,
register or I/O port address.
General-Purpose Byte or Word Transfer Instructions:
MOV XCHG
PUSH XLAT
POP

25
Continued…
Simple Input Output Port Transfer Instructions:
IN
OUT
Special Address Transfer Instructions:
LEA LES
LDS
Flag Transfer Instructions:
LAHF PUSHF
SAHF POPF
26
Arithmetic Instructions

• It adds/subtracts a byte to byte or a word to


word
• It affects AF, CF, OF, PF, SF and ZF flags
Addition Instructions:
ADD ADC INC
AAA DAA

27
Continued…
Subtraction Instructions:
SUB SBB
CMP DAS
DEC NEG
AAS
Multiplication Instructions:
MUL IMUL
AAM

28
Continued….
Division Instructions:
DIV IDIV
AAD CBW CWD
BIT Manipulation instructions
• These instructions are used at bit level.
• These instructions can be used for:
− Testing a zero bit
− Set or reset a bit
− Shift bits across registers
29
Continued….
Logical Instructions:
NOT AND
OR XOR TEST
Shift Instructions:
SHL/SAL SHR
SAR
Rotate Instructions:
ROL ROR
RCL RCR 30
String Instructions

REP
REPE/REPZ
REPNE/REPNZ
MOVS/MOVSB/MOVSW
COMPS/COMPSB/COMPSW
SCAS/SCASB/SCASW
LODS/LODSB/LODSW
STOS/STOSB/STOSW
31
Program Execution Transfer
Instructions:
• These instructions are used to tell the 8086
to start fetching instructions from some new
address, rather than continuing in sequence.
Unconditional Transfer Instructions:
CALL RET JMP
Conditional Transfer Instructions:
JA/JNBE JB/JNAE
JAE/JNB JBE/JNA 32
Continued…

JC JE/JZ JG/JNLE
JGE/JNL JLE/JNG JNC JNO
JNP/JPO JNS JO
JP/JPE JS
Iteration Control Instructions:
LOOP LOOPE/LOOPZ
LOOPNE/LOOPNZ JCXZ
33
Continued…
Interrupt Instructions:
INT INTO IRET
Processor Control Instructions
Flag Set/clear Instructions:
STC CLC
CMC STD
CLD STI
CLI 34
Continued…

External Hardware Synchronization


Instructions:
HLT WAIT
ESC LOCK
No Operation Instruction:
NOP

35
Detailed Discussions on Instructions
& Programming
• MOV destination, source
− Copy byte or word from specified source to
specified destination.
− Source operand can be constant, register
and memory location.

36
Continued…
− Destination can be register or memory
operand.
− Both source and destination can not be
memory location at the same time.
• Ex: MOV CX, 037AH => CX 037A
− MOV BL, [3000H] => BL  [3000H]
− MOV AX, BX => AX BX

37
Continued…

− MOV Result[BP], AX => SS:[Result + BP]AX


− MOV CS:Result[BP], AX => CS:[Result+BP]AX
Segment override prefix
− MOV [DI], AL => ES:[DI]  AL
− MOV DS:[DI], AL =>DS:[DI]  AL

38
Continued…
• ADD/ADC destination, source
− destination  destination + source
− ADC => addition with carry
• Ex: ADD DX,BX => DX  DX + BX
− ADD BX, [SI] => BX  BX + [DS +SI]&[SI+1]
− ADD BL, [SI] => BL  BL + [DS:SI]
− ADD BX, [0000H] => BX  BX +
[0000H]&[0001H] => BL  BL + [0000H],
BH  BH + [0001H] 39
Continued…

− ADC AL, Price[BX] => AL AL + [Price +


BX] + CY
• Ex1: write a program to add two 16 numbers

40

You might also like