[go: up one dir, main page]

0% found this document useful (0 votes)
27 views106 pages

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)
27 views106 pages

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/ 106

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
Continued..
• XLAT No operands
− AL  DS:[BX + AL]
− It translates a byte available in AL register
with the corresponding code (byte)
available in table pointed by BX.
− Before using the instruction, BX should
contain the offset address of a table of
codes.
− This instruction is useful for code
translation 57
Continued…
• Ex1: write a program which determines the ASCII
code of a digit. (using look up table)
Data segment
table db 30, 31, 32, 33, 34, 35, 36, 37,38, 39H
source db 05H
ASCII_Code db ?
Data ends

58
Continued…
Code segment
Assume CS:Code, DS: Data
START: MOV AX, Data
MOV DS, AX
MOV BX, offset table
MOV AL, source
XLAT
MOV ASCII_Code, AL
INT 3
code ends
end start

59
Continued…

• Exercise: write a program to count number


of binary 1’s in an 8 bit number. Assume,
input range 00F – 0F. The program has to
use a look up table. (use XLAT instruction)

60
Continued…
• POP destination
− The destination can be a register, segment register
or memory
− Get 16 bit value from the stack to the destination
− SP = SP + 2 (incremented SP by 2)
• PUSH source
− The destination can be a register, segment register
or memory
− Store 16 bit value in the stack from the source
− SP = SP - 2 (decremented SP by 2)
61
Continued…
• Ex1: MOV AX, 1234H
PUSH AX => AL  SS:SP-1, AH  SS:SP-2
POP DX => DH SS:SP, DL SP+1 now,
INT 3 SP = SP+2
Now, DX = 1234H
Ex2: PUSH AL Illegal, must push a word

62
Continued…
• XCHG destination, source
• The destination, source can be:
− register, memory
− memory, register
− register, register
− But not memory, memory
− Exchange values of two operands
• EX: MOV AL, 5
MOV AH, 2
XCHG AL, AH ; AL = 2, AH = 5
XCHG AL, AH ; AL = 5, AH = 2
63
INT 3
Continued…

• IN AL/AX, 8/16 bit addressable port


− Always brings the 8 bit content of the port to
AL/AX
− 8086 can have both 8 bit and 16 bit
addressable ports.
− Ex. IN AL, 53H IN AX, 34H
− IN AL, 0378H IN AX, 5600H

64
Continued…
• In order to access a port indirectly, 8086 can
use DX register as a port pointing register.
• Ex: MOV DX, 5000H
IN AL DX => AL  [DX]
• OUT Port, Accumulator AL or AX
EX: OUT 32H, AL => AL  port 32H
OUT 5678H, AX
OUT DX, AX
65
Continued…
• AND destination, source
REG, memory
memory, REG
REG, REG
memory, immediate
REG, immediate

66
Continued…

67
Continued..
• The same principle is for OR & XOR as
AND
• SAL/SHL destination, count

− As a bit is shifted out of the LSB position, a


0 is put In the LSB position.
− The MSB will be shifted into CF.
68
Continued…
• For shifts of more than 1 bit position, the
desired number of shifts is loaded into the CL
register, and CL is put in the count position of
the instruction.
• Ex:

69
Continued…
• SHR destination, Count

Ex:

70
Continued…
• ROL destination, count

• Ex:

71
Continued…
• Ex:

72
Continued..
• ROR destination, count

• Ex:

73
Continued…
• RCL destination, count

• Ex:

74
Continued…
• RCR destination, count

• Ex:

75
String Instruction
• MOVS: movsb, movsw
• MOVSB/W:- copy byte/word at DS:[SI] to
ES:[DI] update SI and DI
• Algorithm
ES:[DI] = DS:[SI]
if DF = 0
SI = SI + 1/2
DI = DI +1/2
else SI = SI – 1/2
DI = DI – 1/2 76
Continued…

• LODSB/W: load byte /word at DS:[SI] in


to AL/AX.
• Algorithm
AL/AX = DS:[SI]
if DF = 0
SI = SI + 1/2
else
SI = SI – 1/2
77
Continued…

• STOSB/W: stores a byte/word in AL/AX


into ES:[DI]
• Algorithm
ES:[DI] = AL/AX
if DF = 0
DI = DI +1/2
else
DI = DI – 1/2
78
Continued…
• SCASB/W: compare bytes/words; AL/AX
from ES:[DI]
• Algorithm
AL/AX - ES:[DI]
if DF = 0
DI = DI +1/2
else
DI = DI – 1/2
79
Continued…
• CMPSB/CMPSW: compare bytes/words;
ES:[DI] from DS:[SI]
• Algorithm
DS:[SI] - ES:[DI]
if DF = 0
SI = SI + 1/2
DI = DI +1/2
else
SI = SI -1/2
DI = DI – 1/2 80
Continued…
• REP: repeat following MOVSB/W, LODSB/W,
STOSB/W, instructions CX times. It is prefix.
• Algorithm
check CX
if CX > 0 then
do following chain instructions
CX = CX – 1
go back to CX
else
exit from REP cycle 81
Continued…

82
Continued…
• Ex:

83
Soft ware interrupts
• In X86 family there are 256 software
interrupts.
• A software interrupt is invoked by giving
INT instruction. INT 00 … INT FF
• An INT instruction is very similar to a
CALL instruction.
• The address given by INT is prefixed.
• In CALL, the address of the subroutine is
some where in the memory.
84
Continued…
• Vector is a starting address of a subroutine.
• Interrupts are auto vectored. That means,
the address of the ISR corresponding to an
interrupt number is prefixed.
• INT 0, INT 1, INT 2, INT 3 and INT 4 have
a prefixed functionalities.
• INT 5 … INT FF system/ user defined ISR

85
Continued…
DOS/Windows interrupts
• INT 21H – dos interrupt
• Has 256 functions or services
• Before invoking/calling INT 21H’s ISR, the
programmer has to provide the service number
through AH register.
To display a string on the screen
• Use service number 9 of the dos interrupts
• Before calling the interrupt the DX register should
have the offset address of the first character of the
string.
86
Continued…
• All string should terminate with ‘$’
To terminate a program in the dos environment
• Use the service number 4CH of INT 21H. (note: INT 3
can be used to terminate a program with the debugger
environment.)
• The service number 4CH, terminate a program and
give the control back to the operating system. it
displays the command prompt after the execution
termination.
87
Continued…
To read from ASCII keyboard
• Use service number 01H of the DOS
interrupt.
• This service waits until a key is pressed on
the keyboard and on pressing the key it
returns the ASCII code of the key to the AL
register.

88
Continued…
To clear the screen
• Use the BIOS Videos interrupt INT 10H but
before invoking AX should contain 0003H
Ex: a write a program to read a text message
from the keyboard and display it on the
screen. Also store this message in the data
segment.

89
Continued…
Data segment
Text db 80 dup <$>
msg1 db 0AH, 0DH, ‘Enter your message’, 0AH, 0DH, ‘$’
msg2 db 0AH, 0DH, ‘Your message is’, 0AH, 0DH, ‘$’
Data ends
Code segment
Assume CS:Code, DS: Data
START: MOV AX, data
MOV DS, AX
MOV AX, 0003H
INT 10H 90
Continued…
LEA SI, Text
LEA DX, msg1
MOV AH, 09H
INT 21H
Top:MOV AH, 01H
INT 21H
CMP AL, 0DH
JZ Stop
MOV [SI], AL
INC SI
JMP Top
91
Continued…
Stop: LEA DX, msg2
MOV AH, 09H
INT 21H
LEA DX, Text
MOV AH, 09H
INT 21H
MOV AH, 4CH
INT 21H
Code ends
end start
92
Continued…
• Exercises: write a program to inverse a
string. Read the string from the keyboard
and display the reversed string on the
screen.

93
Continued…
Handling Procedures and Macros
• CALL Address
• If the address is a 16 bit offset address with
in the current code segment we call it as
intra segment call (NEAR CALL)
• But if the address of the procedure is
specified as CS:offset, then before calling
the subroutine both CS and IP are modified
with the address of the procedure. Then it is
called inter segment call (FAR CALL)
94
Continued…
• If it is near call
− Push IP in to the stack
− Load the offset address of the procedure to IP
• If it is far call
− Push both CS and IP content to stack
− Load CS and IP with the address of the
procedure

95
Continued..

• Syntax
Procedure name proc
---------
---------
---------
Procedure name endp

96
Continued…
Data segment
msg1 db 0AH, 0DH, ‘Enter your message’, 0AH, 0DH, ‘$’
msg2 db 0AH, 0DH, ‘Your message is’, 0AH, 0DH, ‘$’
str1 db 20 dup<‘$’>
Data ends
Code segment
Assume CS:Code, DS: Data
display proc
MOV AH, 09H
INT 21H
display endp
97
Continued…
START: MOV AX, data
MOV DS, AX
MOV AX, 0003H
INT 10H
LEA DX, msg1
CALL display
LEA BX, str1
Top:MOV AH, 01H
INT 21H
CMP AL, 0DH
JZ Stop
98
Continued…
MOV [BX], AL
INC BX
JMP Top
Stop: LEA DX, msg2
CALL display
LEA DX, str1
CALL display
MOV AH, 4CH
INT 21H
Code ends
end start 99
Continued…
Macros
• Macros are program segments with a name.
they are similar to procedures but when
macros are called, its body will be copied in
to the main program.
• For calling a macro, there is no need to
write CALL instruction. Just the name of
the macro is sufficient to made the macro
call.
100
Continued…
• The only disadvantage of macro is, it needs
more memory. Since it copy the body of the
macro definition every time when called.
• Syntax
• Macro_name Macro [variable]
------- optional
--------
--------
endm
101
Continued…
Ex: Write a program to define a password and later
verify it by reading from keyboard. Display
appropriate message
Data segment
msg1 db 0AH, 0DH, ‘Enter password’, 0AH, 0DH, ‘$’
password db ‘Tigray1’
size1 dw 0007H
str1 db 20 dup<‘$’>
msg2 db 0AH, 0DH, ‘password is correct’, 0AH, 0DH, ‘$’
msg3 db 0AH, 0DH, ‘password is wrong’, 0AH, 0DH, ‘$’
Data ends
102
Continued…
PRINT Macro X
LEA DX, X
MOV AH, 09H
INT 21H
endm
Code segment
Assume CS:Code, DS: Data
MOV AX, data
MOV DS, AX
MOV ES, AX
MOV AX, 0003H
103
INT 10H
Continued…
PRINT msg1
LEA BX, str1
Top : MOV AH, 01H
INT 21H
CMP AL, 0DH
JZ Stop
MOV [BX], AL
INC BX
JMP Top
Stop: LEA SI, password
LEA DI, str1
104
MOV CX, size1
Continued…
CLD
REPE CMPSB
JNZ Stop1
PRINT msg2
JMP Stop2
Stop1: PRINT msg3
Stop2: MOV AH, 4CH
INT 21H
code ends
end start
105
Continued…

Thank you!

106

You might also like