Академический Документы
Профессиональный Документы
Культура Документы
Chapter – 2:
8086 Instruction Set and Assembler
Directives
1
Machine Language Instruction Format
• A machine language instruction format has one or more
number of fields associated with it.
2
• Byte 2 has three fields
– Mode field (MOD)
– Register field (REG) used to identify the register for the first operand
– Register/memory field (R/M field)
3
Machine Language Instruction Format
• The opcode usually appears in the first byte, but in the few
instruction, a register destination is in the first byte and few
other instruction may have their three bits of opcode in the
second byte.
• The opcodes have the single bit indicator.
• Their definition and significance are given as follows:
– W-bit :
• This indicates whether the instruction operates over an
8-bit 16-bit data /operands.
• If W bit is zero, the operand is of 8-bit and if W is 1, the
operand is of 16-bits.
– D-bit:
• This is valid in case of double operand instruction.
• One of the operands must be a register specified by
REG field.
• The register specified by REG is source operand if
D=0, else, it is a destination operand.
4
Machine Language Instruction Format
• S-bit :
− This bit is called sign extension bit. The S bit is used
along with W-bit to show the type of operation.
− 8-bit operation with 8-bit immediate operand is
indicated by S=0, W=0;
− 16-bit operation with 16 bit immediate operands is
indicated by S=0, W=1 and
− 16 bit operation with a sign extended immediate data is
given by S=1, W=1
• V-bit:
− This is used in case of shift and rotate instruction.
− This bit is set to 0, if shift count is one and is set to 1,if
CL contains the shift count.
5
Machine Language Instruction Format
• Z-bit:
− This bit is used by REP instruction to control the loop.
− If Z-bit is equal to1,the instruction with REP prefix is executed until
the zero flag matches the Z-bit.
– Please note that usually all the addressing modes have DS as the default
data segment.
– However, the addressing modes using BP and SP have SS as the default
segment register.
2. Register to Register:
− This format is 2 bytes long.
− The first byte of code specifies the operation code and width of the
operand specified by w bit.
− The second byte of the code shows the register operands and R/M
field.
8
Machine Language Instruction Format
Register to Register:
9
Machine Language Instruction Format
• The MOD, R/M, REG and the W field are given as:-
10
Machine Language Instruction Format
• Assignment of codes with different registers
11
Software
• The sequence of commands used to tell a microprocessor what to do is
called a program,
• Each command in a program is called an instruction
• A program written in machine language is referred to as machine code
ADD AX, BX
12
Addressing Modes Of 8086 [1]
• Addressing mode indicate a way of locating data or operands.
• Depending upon data types used in the instruction and the memory
addressing modes, any instruction may belong to one or more addressing
modes, or some instruction may not belong to any of the addressing
mode.
• Thus the addressing modes describes the types of operand and the way
they are accessed for executing an instruction.
• According to the flow of instruction execution, the instruction may be
categorized as
– Sequential control flow instruction and
– Control transfer instruction.
• Sequential execution, transfer control to the next instruction appearing
immediately after it (in the sequence) in the program.
• The control transfer instruction, on the other hand, transfer control to
some predefined address or the address somehow specified in the
instruction after their execution.
13
Addressing Modes Of 8086 [2]
1. Immediate In this type of addressing, immediate data is a
part of instruction ,and appears in the form of the
successive byte.
Example: MOV AX,0005H
17
Addressing Modes Of 8086 [6]
18
Addressing Modes Of 8086 [7]
10.Intrasegment Indirect Mode:
In this mode, the displacement to which the control is to be transferred, is in
the same segment in which the control transfer instruction lies ,but it is
passed to the instruction indirectly.
Here the branch address is found as the content of a register or memory
location. This addressing mode may be used in the unconditional branch
instruction.
20
Instruction Set Of 8086/8088
• The categories of 8086/8088 instructions are.
24
MOV Example
• MOV AX,1000h
• MOV [100h],AX
• MOV BX,[0100h]
• MOV AX,2300h
• MOV DS,AX
25
MOV : 16 / 8 Bit register
28
Data Transfer Instructions - MOV
Mnemonic Meaning Format Operation Flags affected
MOV Move Mov D,S (S) (D) None
Destination Source
Memory Accumulator
Accumulator Memory
NO MOV
Register Register Memory Memory
Register Memory Immediate Segment Register
Memory Register Segment Register Segment Register
Register Immediate
Memory Immediate
Seg reg Reg 16
Seg reg Mem 16
Reg 16 Seg reg
Memory Seg reg
29
Data copy/Transfer Instruction
• XCHG: Exchange
– This instruction exchanges the contents of the specified source and
destination operands, which may be registers or one of them may be a
memory location.
– However, exchange of contents of two memory locations is not
permitted. Immediate data also not allowed in these instructions .
30
Data Transfer Instructions - XCHG
Mnemonic Meaning Format Operation Flags affected
Destination Source
Accumulator Reg 16
Example: XCHG START [BX]
Memory Register
Register Register
Register Memory NO XCHG
MEMs
SEG REGs
31
• XCHG Instruction - The Exchange instruction
XCHG op1, op2
• The both operands must be the same size and one of
the operand must always be a register .
• Example:
• XCHG AX, DX ;Exchange word in AX with word in DX
• XCHG BL, CH ;Exchange byte in BL with byte in CH
• XCHG AL, Money [BX] ;Exchange byte in AL with
byte in memory at EA.
32
Data copy/Transfer Instruction
IN Instruction - This IN instruction will copy data
from a port to the AL or AX register.
For the Fixed port IN instruction type the 8 – bit
port address of a port is specified directly in the
instruction.
Example:
IN AL,0C8H ;Input a byte from port 0C8H to AL
IN AX, 34H ;Input a word from port 34H to AX
33
Data copy/Transfer Instruction
For a variable port IN instruction, the port address
is loaded in DX register before IN instruction.
DX is a 16 bit register.
Port address range from 0000H – FFFFH.
Example:
35
OUT Instruction - The OUT instruction copies a byte
from AL or a word from AX or a double from the accumulato
to I/O port specified by op.
Two forms of OUT instruction are available :
(1) Port number is specified by an immediate byte
constant, ( 0 - 255 ).It is also called as fixed port form.
(2) Port number is provided in the DX register ( 0 –
65535 )
Example:
(1)
OUT 3BH, AL ; Copy the contents of the AL to port 3Bh
OUT 2CH,AX ; Copy the contents of the AX to port 2Ch
(2)
MOV DX, 0FFF8H ; Load desired port address in DX
OUT DX, AL ; Copy the contents of AL to FFF8h
OUT DX, AX ; Copy content of AX to port FFF8H
36
IN OUT
• IN AL,03H
• IN AX,DX
• MOV DX,0800H
IN AX,DX
• OUT 03H,AL
• OUT DX,AX
• MOV DX,0800H
OUT DX,AX
37
8086 Microprocessor
Instruction Set
1. Data Transfer Instructions
38
The XLAT Instruction
Mnemonic Meaning Format Operation Flags
XLAT Translate XLAT ((AL)+(BX)+(DS)0) (AL) None
Example:
39
Data copy/Transfer Instruction
• Push: Push to stack
– This instruction pushes the content of the specified register/memory location on to the
stack.
– The stack pointer is decremented by 2, after each execution of the instruction.
– The actual current stack-top is always occupied by the previously pushed data.
– Hence, the push operation decrements SP by two and then stores the two byte content
of operands on to the stack.
– The higher byte is pushed first and then the lower byte.
– Thus out of two decremented stack address the higher byte occupies the higher
address and the lower byte occupies the lower address.
• The actual operation takes place as SS:SP points to the stack top of 8086 system
as shown in figure and AH, AL contain data to be pushed.
40
Data copy/Transfer Instruction
• Push: Push to stack
– The sequence of operations are:
• Current stack top is already occupied so decrement SP by one then store
AH into address pointed to by SP.
• Further decrement SP by one and store AL in to the location pointed by
SP.
– Thus SP is decremented by 2 and AH-AL contents are stored in stack
memory as shown in figure
– Now contents of SP points to a new stack top.
– Example
• PUSH AX
• PUSH DS
• PUSH [5000H]; content of location 5000H and 5001 H in DS are pushed
on to stack.
• To read the contents for the stack memory POP instruction is used.
41
Data copy/Transfer Instruction
• POP: Pop from stack
– This instruction when executed, loads the specified register/ memory location with the
content of the memory location of which the address is formed using the current stack
segment and stack pointer as usual.
– The stack pointer is incremented by 2.
– The POP instruction serves exactly opposite to PUSH instruction.
• The sequence of operation is as below:
– Contents of stack memory location is stored in AL and SP incremented by one.
– Further contents of memory location pointed by SP are copied to AH and SP is again
incremented by 1.
– Effectively SP is incremented by 2 and points to next stack top.
• The example of these instructions are shown follows
– POP AX
– POP DS
– POP [5000H]
42
8086 Microprocessor
PUSH AX POP AX
PUSH DS POP DS
PUSH[5000] POP[5000]
44
Data copy/Transfer Instruction
• LEA: Load effective address
– The load effective address instruction loads the effective address
formed by destination operand into the specified source register.
• LEA BX, ADR; Effective address of label ADR i.e. offset of ADR
will be transferred to Register BX.
• LEA SI, ADR [BX]; offset label ADR will be added to content of BX
to form effective address and it will be loaded in SI.
• LDS/LES: Load pointer to DS/ES
– This instruction loads the DS or ES register and the specified destination
register in the instruction with the content of memory location specified as
source in the instruction.
– Ex: LDS BX, 5000H/LES BX, 5000H
45
Data Transfer Instructions – LEA, LDS, LES
Mnemo Meaning Format Operation Flags
nic affected
LEA Load LEA Reg16,EA EA (Reg16) None
Effective
Address
LDS Load LDS Reg16,MEM (MEM) (Reg16) None
Register
And DS (Mem+2) (DS)
47
Arithmetic instructions
• These instructions perform the arithmetic operations, like addition,
subtraction, multiplication and division along with the respective ASCII
and decimal adjust instructions.
• The increment and decrement also belong to this type of instructions.
• ADD: Add
– This instruction adds the immediate data or contents of the memory locations
specified in the instruction or a register (source ) to the contents of the
another register (destination) or memory location.
– The result is in the destination operand. However, both the source and
destination operands can not be memory operands.
– That means memory to memory addition is not possible. Also contents of
segment register can not be added using this instruction.
– Flag register entries will get affected due to this istruction.
– Examples:
• INC : Increment
– This instruction increases the contents of the specified register or
memory location by 1.
– All the condition code flags are affected except the carry flag CF.
– This instruction adds 1 to the contents of the operand.
– Immediate data cannot be operand of this instruction.
– INC AX Register
– INC [BX] Register Indirect
– INC [5000H] Direct 49
Arithmetic instructions
• DEC: Decrement :
– The decrement instruction subtracts 1 from the contents of the specified
register or memory location.
– All the condition code flags, except the carry flag, are affected depending
upon the results.
– Immediate data cannot be operand of this instruction.
– DEC AX Register
– DEC [5000H] Direct
• SUB: Subtract
– The subtract instruction subtracts the source operand from the destination
operand and result is left in destination operand.
– Source operand may be a register, memory location or immediate data and
the destination operand may be a register or a memory location, but source
and destination operands both must not be memory operands.
– Destination operand can not be an immediate data.
– All the condition code flags are effected by this instruction.
52
Arithmetic instructions
– If the lower digit of AL is between 0 and 9 and AF is set, 06 is added to AL.
– The upper 4 bits of AL are cleared and AH is incremented by one.
– If the value in lower nibble of AL is greater than 9 then the AL is incremented
by 06,AH is incremented by 1 the AF and CF flags are set to 1, and the higher
4 bits of AL are cleared to 0.
– The remaining flags are unaffected.
– The AH is modified as sum of previous content (usually 00) and carry from the
adjustment.
– This instruction does not give exact ASCII codes of the sum, but they can be
obtained by adding 3030H to AX.
– MOV AL , 04 ; AL ← 04
– MOV BL, 09 ; BL ← 09
– MUL BL ; AH-AL ← 24H(9*4)
– AAM ; AH ←03; AL ← 06
54
Arithmetic instructions
• AAD:ASCII Adjust before Division
– Though the names of these two instruction (AAM and
AAD) appear to be similar there is a lot of difference
between their function.
– The AAD instruction converts two unpacked BCD digits in
AH and AL to the equivalent binary number in AL.
– This adjustment must be made before dividing the two
unpacked BCD digits in AX by an unpacked BCD byte.
– PF, SF, ZF are modified while AF, CF, OF are undefined,
after the execution of instruction AAD.
– In the instruction sequence this instruction appears before
DIV instruction unlike AAM appears after MUL.
– The examples explains the execution of the instruction.
– Let AX contents as 0508H
– AAD; Result in AX will be 003AH
55
Packed BCD & Unpacked BCD
• The Intel Instruction set can handle both packed (two digits
per byte) and unpacked BCD (one decimal digit per byte)
• We will first look at unpacked BCD
• Unpacked BCD can be either binary or ASCII.
• Consider the number 4567
Bytes then look like 34h 35h 36h 37h OR: 04h 05h 06h 07h
• In packed BCD where there are two decimal digits per byte
• 99h = 99d 4567d= 4567d
• Unpacked BCD representation contains only one decimal
digit per byte. The digit is stored in the least significant 4 bits;
the most significant 4 bits are not relevant to the value of the
represented number.
• Packed BCD representation packs two decimal digits into a
single byte.
56
• BCD takes more space and more time than
standard binary arithmetic
• It is used extensively in applications that deal with
currency because floating point representations are
inherently inexact
• Database management systems offer a variety of
numeric storage options; “Decimal” means that
numbers are stored internally either as BCD or as
fixed-point integers
• BCD offers a relatively easy way to get around size
limitations on integer arithmetic
57
Unpacked BCD Arithmetic
• With unpacked BCD we wish to add strings such as
'989' and '486' and come up with the string '1475'.
• With BCD you can use the standard input and
output routines for strings to get numbers into and
out of memory without converting to binary
• BCD arithmetic uses the standard binary arithmetic
instructions and then converts the result to BCD
using BCD adjustment instructions.
58
BCD Adjustment Instructions
• Four unpacked adjustment instructions are available:
AAA (ASCII Adjust After Addition)
•AAS (ASCII Adjust After Subtraction) AAM (ASCII
Adjust After Multiplication) AAD (ASCII ADjust
BEFORE Division)
•Except for AAD the instructions are used to adjust
results after performing a binary operation on
ASCII or BCD data
•AAD (in spite of the mnemonic) is used after a
DIV instruction
59
Arithmetic instructions
• DAA: Decimal Adjust Accumulator
– This instruction is used to convert the result of addition of two packed
BCD number to a valid BCD number.
– The result has to be only in AL, if the lower nibble is greater than 9,
after addition or if AF is set, it will add 06 to the lower nibble in AL.
– After addition 06 in the lower nibble of AL if the upper nibble of AL is
greater than 9 or if carry flag is set, DAA instruction adds 60H to AL.
– The instruction DAA affects AF, CF ,PF and ZF flags. The OF is
undefined.
• DAS: Decimal Adjust after Subtraction
– This instruction converts the result of subtraction of two packed BCD
numbers to a valid BCD number.
– The subtraction has to be in AL only, if the lower nibble of AL is
greater than 9 this instruction will subtract 06 from lower nibble of AL.
– If the result of subtraction sets the carry flag or if upper nibble is
greater than 9 it subtracts 60H from AL.
– This instruction modifies the AF, CF, SF, PF and ZF flags. The OF is
undefined after DAS instruction.
60
Arithmetic instructions
• NEG: Negate
– The negate instruction forms 2’s complement of the specified destination in
the instruction.
– For obtaining 2’s complement it subtracts the component of destination from
zero.
– The result is stored back in the destination operand which may be a register
or a memory location.
– If OF is set it indicates that the operation could not be completed successfully.
This instruction affects all the condition code flags.
61
Arithmetic instructions
• IMUL: Signed multiplication
– This instruction multiplies a signed byte in source operand by a signed byte in
AL or a signed word in source operand by a signed word in AX.
– The source can be a general purpose register, memory operand, index
register or base register, but it cannot be immediate data.
– In case of 32 bit result , the higher order word (MSW) is stored in DX and
lower order word is stored in AX.
– The AF, PF, SF and ZF flags are undefined after IMUL.
– If AH and DX contain parts of 16 and 32 bit result respectively CF and OF
both will be set.
– The AL and AX are the implicit operand in case of 8bits and 16bits
multiplication respectively.
– Example
– IMUL BH
– IMUL CX
– IMUL [SI]
• CBW: Convert Signed Byte or Word
– This instruction converts a signed byte to a signed word.
– In other words, it copies the sign bit of a byte to be converted to all bits in the
higher byte of the result word.
• CWD: Convert Signed Word to Double word
– This instruction copies the sign bit of AX to all the bits of DX register.
62
Multiplication and Division
63
Multiplication and Division
64
MUL op
Example:
AL = 21h (33 decimal)
BL = A1h(161 decimal )
MUL BL ; AX =14C1h (5313 decimal),
since AH≠0,CF and OF will set to 1.
DIV BH ;AX / BH
AX = Quotient = 5EH = 94 decimal
AH = Remainder = 65H = 101 decimal
IDIV Instruction - This instruction is used to divide a
signed word by a signed byte or to divide a signed double
word by a signed word.
Example:
IDIV BL ;Signed word in AX is divided by signed byte in BL
Example
IDIV BP ;divide a Signed double word in DX and
AX by signed word in BP
IDIV BYTE PTR[BX] ; divide AX by a byte at offset [BX] in DS
72
Logical instructions
• These types of instructions are used for carrying out the bit by bit shift ,
rotate or basic logical operation. All the condition code flags are affected
depending upon the result. Basic logical operation available with 8086
instruction set are AND, OR, NOT, and XOR.
• AND: Logical AND
– This instruction bit by bit ANDs the source operand that may be an immediate,
a register or a memory location to the destination operand that may be
register or memory location.
– The result is stored in the destination operand. both the operands cannot be
memory location or immediate operands.
– Example
– AND AX, 0008H
– AND AX, BX
• OR: Logical OR
– The OR instruction carries out the OR operation in the same way as described
in case of AND operation.
– The limitation on source and destination operands are also the same as in the
case of AND operation.
– Examples
– OR AX, 0098H
– OR AX, BX
– OR AX, [5000H]
73
Logical instructions
• NOT: Logical Invert
– The NOT instruction complements the content of an operand register or a
memory location, bit by bit.
• NOT AX
• NOT [5000H]
76
Logical instructions
77
Logical instructions
• ROL: Rotate Left without Carry
• This instruction rotates the content of destination operand to the left by the specified
count (bit-wise) excluding carry.
• The most significant bit is pushed in to the carry flag as well as the least significant
bit position at each operation.
• The remaining bits are shifted left subsequently by the specified count position.
• The PF, SF and ZF flags are left unchanged in this rotate operation.
• The operand may be a register or a memory location.
78
Logical instructions
ROR: Rotate Right
without Carry
79
String Manipulation Instruction
• A series of data bytes or words available in memory at consecutive
location, to be referred to collectively or individually, are called as byte
string or word string.
• For example a string of a character may be or located in consecutive
memory location where each character may be represented by its ASCII
equivalent.
• For referring to string, two parameters are required (a) starting or end
address of string (b) length of string.
• The length of string usually stored as a count in CX register.
• The incrementing or decrementing of pointer in case of 8086 string
instruction depends upon the direction flag status.
• If it is a byte string operation the index register are updated by one.
• If it is word string operation the index register are updated by two.
• The counter in both cases, is decremented by one.
– After the MOVS instruction is executed once, the index register are
automatically updated and CX is decremented.
– The incrementing or decrementing of pointer i.e. SI and DI depend upon the
direction flag DF.
– If DF is 0, the index register are incremented, otherwise they are
decremented in case of all the string manipulation instruction.
81
String Manipulation Instruction
1. Example for MOVSB 2. Example for MOVSB
– MOV AX, 2000H – MOV AX, 5000H
– MOV DS, AX – MOV DS, AX
– MOV AX, 3000H – MOV AX, 3000H
– MOV ES,AX – MOV ES,AX
– MOV CX, 00FFH – MOV CX, 00FFH
– MOV SI, 1000H – LEA SI, SOURCE_STRING
– MOV DI, 2000H – LEA DI, DESTIN_STRING
– CLD – CLD
– REP MOVSB – REP MOVSB
82
String Manipulation Instruction
• CMPS: Compare String Byte or String
– The CMPS instruction can be used to compare two string of bytes or
words.
– The length of the string must be stored in a register CX.
– If both the byte or word string are equal, zero flag is set.
– The flags are affected in same way as CMP instruction.
– The DS:SI point to the two string.
– The REP instruction prefix is used to repeat the operation till CX
becomes zero
• SCAS: Scan String Byte or String Word
– This instruction scans string of byte or words for an operand byte or
word specified in the register AL or AX.
– The string is pointed to by ES; DI register pair.
– The length of the string is stored in CX.
– The DF controls the mode for scanning of the string.
– Whenever a matched to the specified operand, is found in the string,
execution stops and zero flag is set.
– If no match is found, the zero flag is reset.
83
String Manipulation Instruction
• Example for CMPSB 2. Example for SCAS
– MOV AX, 2000H
– MOV DS, AX – MOV AX, 5000H
– MOV AX, 3000H – MOV ES, AX
– MOV ES,AX – MOV CX, 00FFH
– MOV CX, 00FFH – LEA DI, DESTIN_STRING
– MOV SI, 1000H – CLD
– MOV DI, 2000H – REPNE SCASW
– CLD
– REP CMPSB
84
String Manipulation Instruction
• LODS: Load String Byte or String Word
– The LODS instruction loads the AL /AX register by the content of a string pointed to by
DS;SI register pair.
– The SI is modified automatically depending upon DF.
– The DF plays exactly to same role as in case of MOVSB/MOVSW instruction.
– If it is a byte transfer (LODSB), the SI is modified by one and if it is a word transfer
(LODSW ), the SI is modified bye two.
– No other flags are affected by this instruction.
85
Control transfer or Branching Instruction
• The control transfer instructions transfer the flow of execution of the
program to a new address specified in the instruction directly or indirectly.
86
SUBROUTINE & SUBROUTINE HANDILING INSTRUCTIONS
Main program
Subroutine A
First Instruction
Call subroutine A
Next instruction
Return
Call subroutine A
Next instruction
87
A subroutine is a special segment of program that can be called for
execution from any point in a program.
An assembly language subroutine is also referred to as a
“procedure”.
Whenever we need the subroutine, a single instruction is inserted
in to the main body of the program to call subroutine.
To branch a subroutine the value in the IP or CS and IP must be
modified.
After execution, we want to return the control to the instruction
that immediately follows the one called the subroutine i.e., the
original value of IP or CS and IP must be preserved.
Execution of the instruction causes the contents of IP to be saved
on the stack. (this time (SP) (SP) -2 )
A new 16-bit (near-proc, mem16, reg16 i.e., Intra Segment) value
which is specified by the instructions operand is loaded into IP.
Examples: CALL 1234H 88
Control transfer or Branching Instruction
• Unconditional Branch Instructions
– CALL Unconditional Call
• This instruction is used to call a subroutine from a main program.
• There are again two types of procedures depending upon whether it
is available in the same segment (Near CALL, i.e. +/- 32K
displacement) or in other segment (FAR CALL, i.e. anywhere
outside the segment).
• The modes for them are called as intrasegment and intersegment
addressing modes respectively.
• On execution, this instruction stores the incremented IP (i.e.
address of the next instruction) and CS onto the stack and loads the
CS and IP registers, respectively, with the segment and offset
addresses of the procedure to be called.
• In case of NEAR CALL it pushes only IP register and in case of
FAR CALL it pushes IP and CS both onto the stack.
• The NEAR and FAR CALLS are discriminated using opcode.
89
CALL Instruction - This Instruction is used to transfer
execution to a subprogram or procedure. There are two
basic types of CALL ’s : Near and Far.
A Near CALL is a call to a procedure which is in the
same code segment as the CALL instruction .
When 8086 executes the near CALL instruction it
decrements the stack pointer by two and copies the offset
of the next instruction after the CALL on the stack.
This offset saved on the stack is referred as the return
address, because this is the address that execution will
returns to after the procedure executes.
A near CALL instruction will also load the instruction
pointer with the offset of the first instruction in the
procedure. 90
A Far CALL is a call to a procedure which is in a
different segment from that which contains the
CALL instruction .
When 8086 executes the Far CALL instruction it
decrements the stack pointer by two again and
copies the content of CS register to the stack.
It then decrements the stack pointer by two again and
copies the offset contents offset of the instruction
after the CALL to the stack.
Finally it loads CS with segment base of the segment
which contains the procedure and IP with the offset
of the first instruction of the procedure in segment. 91
• Inter Segment
Operand
Near-proc
Far – proc
Memptr 16
Regptr 16
Memptr 32
93
Control transfer or Branching Instruction
• RET: Return From the Procedure
– At each CALL instruction, the IP and CS of the next instruction is
pushed onto stack, before the control is transferred to the procedure.
– At the end of the procedure, the RET instruction must be executed.
– When it is executed, the previously stored content of IP and CS along
with flags are retrieved into the CS, IP and flag register from the stack
and execution of the main program continues further.
– The procedure may be a near or far procedure. In case of a FAR
procedure, the current contents of the SP points to IP and CS at the
time of return.
– While in case of a NEAR procedure, it points to only IP.
94
Control transfer or Branching Instruction
• INT: Interrupt Type N
– In the interrupt structure of 8086/8088, 256 interrupts are
defined corresponding to the types from 00H to FFH.
– When an INT N instruction is executed, the TYPE byte N
is multiplied BY 4 and the contents of IP and CS of the
interrupt service routine will be taken from the
hexadecimal multiplication (N * 4) as offset address and
0000 as segment address.
95
Control transfer or Branching Instruction
• JMP: Unconditional Jump:
– This instruction unconditionally transfers the control of execution to the
specified address using an 8 bit or 16 bit displacement (intrasegment
relative, short or long) or CS:IP (intersegment direct far).
– No flags are affected by this instruction.
– Corresponding to the methods of specifying jump addresses, the JUMP
instruction may have the following three formats.
97
Control transfer or Branching Instruction
98
Control transfer or Branching Instruction
99
Flag Manipulation and Processor Control Instructions
• These instructions control the functioning of the available hardware inside
the processor chip.
• These are categorized into two types;
100
Control transfer or Branching Instruction
Flag manipulation Instructions
Sr. No. Instruction Function
1 CLC Clear carry flag
2 CMC Complement carry flag
3 STC Set carry flag
4 CLD Clear direction flag
5 STD Set direction flag
6 CLI Clear interrupt flag
7 STI Set interrupt flag
101
Programming with an Assembler
• A programmer called ‘assembler’ is used to convert the mnemonics of
instructions along with the data into their equivalent object code modules.
• These object code modules may further be converted in executable code using
the linker and load programs.
• This type of programming is called assembly level programming.
• In assembly language programming, the mnemonics are directly used in the user
programs.
• The assembler performs the task of coding.
• The advantages of assembly language over machine language are as follows:-
102
Programming with an Assembler
• Basically, the assembler is a program that converts an assembly input
file also called as source file to an object file that can further be
converted into machine codes or an executable file using linker.
• The recent versions of assembler are designed with many facilities like
macro assemblers, numerical processor assembler, procedure,
functions and so on.
• In book( RAY), the assembly language programming using MASM
(Microsoft Macro Assembler).
• There are a number of assemblers available like MASM, TASM and
DOS assembler.
• MASM is one of popular assemblers used along with a LINK program
to structure the code generated by MASM in the form of an executable
file.
• MASM reads the source programs its input and provides an object
file.
• The LINK accepts the object file produced by MASM as input and
produces EXE file.
103
Assembler Directives and Operators
• To make the programming on microprocessor simpler,
assemblers are used. The main role of the assembler is to
convert the assembly language program in to the machine
language program and in 8086 since the hand coding is a
tedious job mostly people follow the assembler for
programming the microprocessor.
• For completing the task of the assembly, assembler needs
some hints from the programmer i.e. the required storage
of particular constant or variable, logical names of the
segments, types of different subroutines or modules, end
of file etc.
• Such hints are given by the assembler directives.
• Another type of hint which helps the assembler to assign a
particular constant with a label or initialize particular memory
locations or labels with constants is an operator. 104
Assembler Directives
105
Naming Data and addresses
Programs work with three general categories of data
Constants ,Variables and Addresses
106
Assembler directives are specific for a
particular assembler
However all the popular assemblers like
the Intel 8086 macro assembler, the turbo
assembler and the IBM macro assembler
use common assembler directives
107
Assembler Directives and Operators
• DB: Define Byte
– It is used to reserve byte or bytes of memory locations in the available memory
space.
• DW: Define Word (1 – word = 2 – bytes)
– It is used to reserve word or words of memory locations in the available memory
space.
• DD: Define Double Word (1 – double word = 4 – bytes)
– It is used to reserve word or words of memory locations in the available memory
space.
• DQ: Define Quad Word (1 – quad word = 8 – bytes)
– It is used to reserve two words of memory locations in the available memory
space.
• DT: Define Ten Bytes
– It is used to reserve ten bytes of memory locations in the available memory
space.
• Usage:
– RANKS DB 01H,02H,03H
– WORDS DB 1234H,0250H,03ABH
– LABEL1 DQ 1234567887654321H
– PLAYERS_RANKS DT 01H,02H,03H, 31H,22H,03H 21H,32H,07H 10H,
108
Assemble Directives ---for variables
Define Byte
Range : 00H – FFH for unsigned value; 00H – 7FH for positive value
and 80H – FFH for negative value
109
• Multiple definitions can be abbreviated
Example:
message DB ’B’
DB ’y’
DB ’e’
DB 0DH
DB 0AH
can be written as
message DB ’B’,’y’,’e’,0DH,0AH
• More compactly as
message DB ’Bye’,0DH,0AH
8086 Microprocessor
Assemble Directives
Define Word
113
Arrays
• Any consecutive storage locations of the same size
can be called an array
X DW 40CH,10B,-13,0
Y DB 'This is an array'
Z DD -109236, FFFFFFFFH, -1, 100B
• Components of X are at X, X+2, X+4, X+8
• Components of Y are at Y, Y+1, …, Y+15
• Components of Z are at Z, Z+4, Z+8, Z+12
Assembler Directives(contd.)
Assume:logical segment name
• Used to tell the assembler the names of the logical
segments to be assumed for different segments used in
program.
• You must tell the assembler that what to assume for any
segment you use in the program.
• Example, ASSUME CS: CODE tells the assembler that the
instructions for the program are in segment named CODE,
and hence the CS register is to be loaded with the segment
address allotted by the operating system for label CODE.
116
Assembler Directives and Operators
END – End the program
•To tell the assembler to stop fetching the instruction and end
the program execution
•END is placed after the last statement of a program to tell the
assembler that this is the end of the program module.
•The assembler will ignore any statement after an END
directive. Carriage return is required after the END directive
•ENDS - This ENDS directive is used with name of the
segment to indicate the end of that logic segment.
•ENDP - ENDP directive is used along with the name of the
procedure to indicate the end of a procedure to the assembler
117
Assembler Directives and Operators
Example:
CODE SEGMENT ;Here it Starts the logic
segment containing code
Some instructions statements to perform the logical
operation
CODE ENDS ;End of segment named as CODE
Example:
SQUARE_NUM PROCE ; It start the procedure
Some steps to find the square root of a number
SQUARE_NUM ENDP ;Here it is the End for the
procedure
118
Assemble Directives
SEGMENT : Used to indicate the beginning of a code/ group of
data/ group of instructions to be put together in a particular
segment and is called logical segment .
The started segment is also assigned a name i.e label by this
statement.
General form:
Segnam SEGMENT
…
… Program code
… or
… Data Defining Statements
…
…
Segnam ENDS
• If the word is at even address 8086 can read a memory in 1 bus cycle.
– If the word starts at an odd address, the 8086 will take 2 bus cycles to
get the data.
– A series of words can be read much more quickly if they are at even
address.
– When EVEN is used the location counter will simply incremented to
next address and NOP instruction is inserted in that incremented
location.
• Example:
DATA1 SEGMENT
SALES DB 9 DUP(?) ;declare an array of 9 bytes
EVEN ; increment location counter to 000AH
RECORD DW 100 DUP( 0 ) ;Array of 100 words will start from an even
address for quicker read
DATA1 ENDS
120
Assembler Directives and Operators
• EQU - This EQU directive is used to assign a label with a
value or a symbol. Each time the assembler finds the name
in the program, it will replace the name with the value or
symbol you give to that name.
– Example:
• FACTOR EQU 03H ; you have to write this statement at the
starting of your program and later in the program you can use this
as follows
• ADD AL, FACTOR ; When it codes this instruction the assembler
will code it as ADD AL, 03H
121
EQU Directive
EQU – Equate
•It is just to reduce the recurrence of the numerical values
or constants in a program code
•name EQU expression
– expression can be string or numeric
– Use < and > to specify a string EQU
– these symbols cannot be redefined later in the
program
sample EQU 7Fh;assigns 7FH with label
sample
aString EQU <1.234>
message EQU <This is a message>
Assembler Directives and Operators
• EXTRN: External and PUBLIC: public
– The directive EXTERN informs the assembler that the
names, procedures and labels declared after this directive
have already been defined in some other assembly
language modules.
MODULE 1 SEGMENT
PUBLIC FACTORIAL FAR
MODULE 1 ENDS
MODULE 2 SEGMENT
EXTRN FACTORIAL FAR
MODULE 2 ENDS
GROUP: Group the Related Segment
– This directive is used to form logical groups of segments
with similar purpose or type.
– This directive is used to inform the assembler to form a
logical group of the following segment names.
• PROGRAM GROUP CODE, DATA, STACK 123
Assembler Directives and Operators
• PUBLIC
– The PUBLIC directive is used along with the EXTRN directive.
– This informs the assembler that the labels, variables constants, or
procedures declared PUBLIC may be accessed by other assembly
modules to form their codes, but while using the PUBLIC declared
labels, variables, constants or procedures the user must declare them
externals using the extern directive.
124
Assembler Directives and Operators
• LOCAL
– The labels, variables, constants or procedures declared LOCAL in a
module are to be used only by the particular module.
– LOCAL a, b, DATA, ARRAY, ROUTINE
• NAME: Logical Name of a Module
– The NAME directive is used to assign a name to an assembly
language program module.
• The module, may now be referred to by its declared name.
• ORG: Origin
– The ORG directives directs the assembler to start the memory
allotment for the particular segments, block or code from the declared
address in the ORG statement.
– If an ORG 200H address in code segment is present at the starting of
the code segment of that module then the code will start from 200H
address in code segment
125
Assembler Directives and Operators
• .
• PROC: Procedure
– The PROC directives marks the start of named procedure in the
statement.
– Also the types NEAR or FAR specify the type the procedure i.e.
whether it is to be called by the main program located within 64k of
physical memory or not.
• PTR: Pointer
– The POINTER operator is used to declare the type of a label, variable
or memory operand.
– The operator PTR prefixed by either BYTE or WORD.
– If prefix is byte then the particular label, variable or memory operand is
treated as an 8-bit quantity, while if word is the prefix, then it is treated
as a 16-bit quantity.
MOV AL, BYTE PTR [SI]
INC BYTE PTR [BX]
MOV BX,, BYTE PTR [2000H]
INC WORD PTR [3000H]
126
Assembler Directives and Operators
• GLOBAL
– The labels, variables, constants or procedures declared GLOBAL may
be used by other modules of the program. Once a variable is declared
GLOBAL, it can be used by any module in the program.
ROUTINE PROC GLOBAL
• FAR PTR
– This directive indicates the assembler that the label following FAR
PTR is not available within the same segment and the address of the
label is of 32- bits i.e. 2-bytes offset followed two byte segment
address.
JMP FAR PTR LABEL
CALL FAR PTR ROUTINE
• NEAR PTR
– This directive indicates that the label following NEAR PTR is in the
same segment and needs only 16-bit i.e. 2-byte offset to address it.
JMP NEAR PTR LABEL
CALL NEAR PTR ROUTINE
127
Assembler Directives and Operators
•SHORT
– The SHORT operator indicates to the assembler that only one byte is
required to code the displacement for a jump (i.e. displacement is
within -128 to +127 bytes from address of the byte next to the jump
opcode).
• JMP SHORT LABEL
•OFFSET: Offset of a Label
– When the assembler comes across the OFFSET operator along with a
label, it first computes the 16-bit displacement (also called as offset
interchangeably) of the particular label, and replaces the string
OFFSET LABEL by the computed displacement. This operator is used
with arrays, strings, labels and procedures to decide their offset in
their default segments.
• CODE SEGMENT
• MOV SI, OFFSET LIST
• CODE ENDS
• DATA SEGMENTS
• LIST DB 10H
• DATA ENDS
128
Assembler Directives and Operators
• LABEL: label
– The Label directive is used to assign a name to the
current content of the location counter.
– When the assembly process starts, the assembler
initializes a location counter to keep track of memory
location assigned to the program.
129
Assemble Directives
EXAMPLES
procname ENDP
RET
ADD64 ENDP
RET
CONVERT ENDP
132
8086 Microprocessor
Assemble Directives
Reserves one memory location for 8-bit
SHORT signed displacement in jump instructions
Example:
133
8086 Microprocessor
Assemble Directives
MACRO Indicate the beginning of a macro
MACRO
ENDM ENDM End of a macro
General form:
macroname ENDM
134
Program Example
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; This is an example program. It prints the ;
; character string "Hello World" to the DOS standard output ;
; using the DOS service interrupt, function 9. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT BYTE STACK 'STACK' ;Define the stack segment
DB 100h DUP(?) ;Set maximum stack size to 256 bytes (100h)
hellostk ENDS