0% found this document useful (0 votes)
24 views

MP Lecture 2

Uploaded by

zerayhaile6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

MP Lecture 2

Uploaded by

zerayhaile6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

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
• Ex: write a program to add two 16 bit numbers
FFFFH + FFFFH
Data segment
word1 dw 0FFFFH
word2 dw 0FFFFH
Result dw ?
Data ends

40
Continued….
Code segment
Assume CS:Code, DS: Data
START: MOV AX, 2000H
MOV DS, AX
MOV AX, word1
ADD AX, word2
MOV Result, AX
MOV AL, 00H
ADC AL, 00H
MOV Result + 2, AL
INT3
Code ends
41
END START
Continued…

• INC/DEC instructions
INC BL => BL  BL +1
DEC Prices[BX] => [Prices + BX]-1 
[Prices + BX]
• Loop: is not a stand alone instruction, but need to be used
with an address and the body of the loop.
− it executes the body of the loop until CX becomes zero.
− CX (counter register should be initialized)

42
Continued…

MOV AX, 0000H MOV AX, 0000H


MOV CX, 000AH MOV CX, 000AH
UP: INC AX UP:INC AX
DEC CX LOOP UP
JNZ UP

43
Continued…
• 8 bit multiplication
MUL BL => AX  AL*BL
• 16 bit multiplication
MUL CX => (DX)(AX)  AX*CX
Ex: write a program to multiply two 16 bit
numbers available in the data segment. Store
the result in the memory locations of the same
segment.

44
Continued…

Data segment
num1 dw 1234H
num2 dw 5678H
result dw 2 dup <0>
Data ends

45
Continued…
Code segment
Assume CS:Code, DS: Data
START: MOV AX, 3000H
MOV DS, AX
MOV AX, num1
MOV BX, num2
MUL BX
MOV result, AX
MOV result + 2, DX
INT 3
Code ends
End start
46
Continued…

• Ex1: write a program to add ten 8 bit


number available in the data segment . Store
the result in the next location.
Data segment
array1 db 1, 2, 3,4, 5, 6, 7, 8, 9, 0AH
count dw 000AH
sum db 1 dup <0>
Data ends 47
Continued…
Code segment
Assume CS:Code, DS: Data
START: MOV AX, 3000H
MOV DS, AX
MOV CX, count
MOV AL, 00H
MOV SI, offset array1 or LEA SI, array1
UP: ADD AL,[SI]
INC SI
Loop UP
MOV sum, AL
INT 3
Code ends 48
End start
Continued…
• Ex2: write a program to count the number of
occurrences of a byte 0ABH in array of 5
bytes available in data segment. Store the
result in the next location.
Data segment
bytes db 0FFH, 0FBH, 0ABH, 0FDH, 0ABH
size dw 0005H
count db 1 dup <0>
Data ends
49
Continued…
Code segment
Assume CS:Code, DS: Data
START: MOV AX, 2000H
MOV DS, AX
MOV CX, size
LEA BX, bytes
MOV AH, 00H
UP: MOV AL, [BX]
CMP AL, OABH
JNZ down
INC AH
down: INC BX
50
Continued…
Loop UP
MOV count, AH
INT 3
code ends
end start

51
Continued…
• Ex3: write a program to find the largest
byte available in array of 10 bytes available
in data segment. Store the result in the next
memory location.
Data segment
array db 5, 6, 8, 2, 4, 3, 1, 7, 3, 2H
size dw 000AH
largest db 1 dup <0>
Data ends
52
Continued…
Code segment
Assume CS:Code, DS: Data
START: MOV AX, 3000H
MOV DS, AX
MOV CX, size
LEA SI, array
DEC CX
MOV AL, [SI]
UP: INC SI
CMP AL, [SI]
JNC down
MOV AL, [SI]
53
down: Loop UP
Continued…

MOV largest, AL
INT 3
code ends
end start

54
Continued…

• Exercises
1. write a program to add 10 bytes available
in the memory location starting at
3000:0100H. Store the result in the next
location.
2. write a program to add two matrices of
size 3x3. store the result in the next location.

55
Continued…

3. write a program to sort an array in an


ascending order using bubble sorting method.

56

You might also like