FALLSEM2023-24 CSE2006 ETH VL2023240104104 2023-08-23 Reference-Material-I
FALLSEM2023-24 CSE2006 ETH VL2023240104104 2023-08-23 Reference-Material-I
FALLSEM2023-24 CSE2006 ETH VL2023240104104 2023-08-23 Reference-Material-I
Directives
1
Instruction Set Of 8086/8088
• The categories of 8086/8088 instructions are.
4
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.
5
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]
6
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 .
8
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
9
Data copy/Transfer Instruction
• LAHF: Load AH from lower byte of flag
– This instruction loads the AH register with the lower byte of the flag register.
– This command may be used to observe the status of all the condition code flags
(except overflow) at a time.
• 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 12
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.
15
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.
16
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.
17
Arithmetic instructions
• DIV: Unsigned Division
– This instruction performs unsigned division.
– It divides an unsigned words or double word by a 16bit or 8bit operand.
– The dividend must be in AX for 16-bit operation and divisor may be specified
using any one of the addressing modes except immediate.
– The result will be in AL while AH will contain the remainder.
– In case of double word dividend (32-bit) the higher word should be in DX and
lower word should be in AX.
– The divisor may be specified as already explained.
– The quotient and the remainder, in this case will be in AX and DX
respectively.
– This instruction does not affect any flag.
• 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] 19
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]
23
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.
25
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.
27
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
28
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.
29
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
30
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.
31
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.
32
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.
33
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.
38
Control transfer or Branching Instruction
39
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;
40
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
41
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. 42
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 DW 1234H,0250H,03ABH
– LABEL1 DQ 1234567887654321H
– PLAYERS_RANKS DT 01H,02H,03H, 31H,22H,03H 21H,32H,07H 10H,
43
Assembler Directives and Operators
• Assume: Assume logical segment name
– It is used to indicate the start of a logical segment
– ASSUME DS: DATA it implies that the start of a new segment named
DATA and its base address is to be initialized to DS.
• END - END directive 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.
• ENDP - ENDP directive is used along with the name of the
procedure to indicate the end of a procedure to the
assembler
• Example:
– SQUARE_NUM PROCE ; It start the procedure
– ;Some steps to find the square root of a number
– SQUARE_NUM ENDP ;Hear it is the End for the procedure
44
Assembler Directives and Operators
• ENDS - This ENDS directive is used with name of
the segment to indicate the end of that logic
segment.
– Example:
– CODE SEGMENT ;Hear it Start the logic
– ;segment containing code
– ; Some instructions statements to perform the logical operation
– CODE ENDS ;End of segment named as CODE
45
Assembler Directives and Operators
• EQU - This EQU directive is used to give a name to some
value or to a symbol. Each time the assembler finds the
name in the program, it will replace the name with the value
or symbol you given to that name.
– Example:
• FACTOR EQU 03H ; you has 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 ADDAL, 03H
46
Assembler Directives and Operators
• EVEN - This EVEN directive instructs the assembler to
increment the location of the counter to the next even address
if it is not already in the even address.
– 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
– ; Location counter will point to 0009 after assembler reads next
statement
– 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
47
Assembler Directives and Operators
• EXTERN: 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.
• 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
• 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.
• LENGTH: Byte Length of a label
– This directive is not available in MASM. This is used to refer to the
length of data array or string.
– MOV CX, LENGTH ARRAY
48
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 referred to by its declared
name.
• 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 49
Assembler Directives and Operators
• 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.
• 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.
50
Assembler Directives and Operators
• PUBLIC
– The PUBLIC directive is used along with the Extern 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.
• 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
51
Assembler Directives and Operators
• TYPE
– The TYPE operator directs the assembler to decide the data type of
the specified label and replaces the ‘TYPE’ label by the decided data
type.
• 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.
• FAR PTR
– This directive indicates the assembler that the label following FAR
PYTR 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.
• 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.
52