Ch3 Part2 Inssets 1
Ch3 Part2 Inssets 1
Ch3 Part2 Inssets 1
(Lecture Notes)
Compiled by: Bushra KM
1. MOV Instruction
This instruction copies a word or a byte of data from some source to a destination. Both source
and destination operands must be the same size, which can be a byte or a word.
Syntax: MOV destination, source
Operands: - Source operands can be immediate, register or memory location
- Destination operands can be register, memory location
Where:
- REG = AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP
- Memory: [BX], [BX+SI+7], 16 bit variable, etc...
- Immediate: 5, -24, 3Fh, 10001101b, symbolic constants, expressions etc...
Modified flag: no flags are modified by this instruction
Note:
- The MOV instruction cannot be used to set the value of the CS and IP registers.
- We can’t use segment registers with an immediate value.
-The MOV instruction cannot copy value of one segment register to another segment
register (should copy to general register first).
- The source and destination operands in a MOV instruction can’t both be memory
location.
Some Examples:
2. PUSH Instruction
This instruction is used to load data to the stack memory. That is, it copies a word from some
source to the location in the stack where the stack pointer (SP) points.
Whenever data is pushed on to the stack:
- The stack pointer (SP) is decremented by two. i.e., SP= SP-2
- The first (most significant) data byte moves into the stack segment memory location
addressed by sp-1
- The second (least significant) data byte moves into the stack segment memory
location addressed by sp-2
Syntax: PUSH Source
Where:
5. OUT Instruction
This instruction transfers a byte or a word from AL register or AX register,
respectively, to an out-put port number.
Syntax:
OUT destination, source
Where:
Destination operand is the port number, it may be specified either as an
immediate number or in the DX register for 16-bit port number.
Source operand can be either register AL (for 8-bit data) or register AX (for a
16-bit data).
Examples:
6. LEA Instruction
The LEA, LDS, and LES are Address Object Transfers instructions. They manipulate the
addresses of variables rather than the contents or values of variables. They are most useful
for determining the offset address of a variable, list processing, based variables, and string
operations.
LEA: Load Effective Address
This instruction determines the offset of the variable or memory location and loads this
address in the specified 16 bit register.
Syntax:
LEA destination, source
Where:
- Destination operand can be any 16-bit general register (except segment registers and
IP).
- The source operand must be a memory operand or a variable.
- Operation: destination =offset address of source
Note:
8-bit registers cannot be used as destination
LEA does not affect any flags.
7. LDS Instruction
Load register and DS or (load pointer using DS)
This instruction copies a word from two memory locations into the 16 bit register specified in the
instruction. It then copies a word from the next two memory location in to the DS register.
Syntax:
LDS destination, source
Where:
Destination operand can be any 16 bit register except segment registers and IP register.
Source operand must be a memory operand/variable.
Operation:
destination = first word data from source, and
DS = second word data from source
Note:
8. LES Instruction
Load register and ES.
The instruction LES is similar to the instruction LDS except that it load the Extra Segment (ES)
Register instead of Data Segment (DS) Register.
is the oldest and most elementary branch of mathematics
It consists in the study of numbers, especially the properties of the traditional operations
between them — addition, subtraction, multiplication and division.
Arithmetic is an elementary part of number theory.
In 8086/88 arithmetic operations may be performed on four types of numbers:
Unsigned binary,
Signed binary (integers),
Unsigned packed decimal, and
Unsigned unpacked decimal
Binary numbers:
Signed or Unsigned
may be 8 or 16 bits long
Unsigned binary numbers:
may be either 8 or 16 bits long
all bits are considered in determining a number's magnitude
The value range of an 8-bit unsigned binary number is 0-255
The value range of a 16-bit unsigned binary number is 0-65,535
Arithmetic operations available for unsigned binary numbers are Addition,
subtraction, multiplication and division.
Signed binary numbers (integers):
may be either 8 or 16 bits long
The high-order (leftmost) bit is interpreted as the number's sign: 0 = positive and 1 =
negative
Negative numbers are represented in standard two's complement notation
The value range of an 8-bit signed binary number is -128 to +127
The value range of a 16-bit signed binary number is - 32,768 to +32,767
The value zero has a positive sign
Multiplication and division operations are provided for signed binary numbers.
Addition and subtraction are performed with the unsigned binary instructions.
Conditional jump instructions, as well as an "interrupt on overflow" instruction, can
be used following an unsigned operation on an integer to detect overflow into the
sign bit.
Where:
Destination can be a register, or a memory/variable operand.
Source can be an immediate number, a symbolic constant, a register, or a memory/variable
operand.
Where:
Destination can be a register, or a memory/variable operand.
Source can be an immediate number, a symbolic constant, a register, or a memory/variable
operand.
CF: is the current value of Carry Flag(it is either 1 or 0)
For subtraction the carry flag CF acts as borrow flag
The NEG instruction evaluate the 2’complement of an operand
The CMP instruction compares a byte/word from the specified source with a byte/word from
the specified destination.
The comparison is done by subtracting the source byte or word from the destination
byte or word.
But the result is not stored in the destination or anywhere.
Source and destination remain unchanged, only flags are updated
Flags affected: AF, CF, OF, PF, SF, and ZF. (But for DEC instruction CF is not affected!)
Note:
Segment registers (CS, DS, ES, and SS) and IP register cannot be used.
The source and destination both must be a word or byte.
Immediate values or Symbolic constants cannot be used as destination operand.
The source and the destination in an instruction cannot both be memory locations.
For DEC Instruction, when the content of the destination operand is zero, then after DEC
instruction the content will be FFH for 8-bit, or FFFFH for 16 bit operand
Examples:
Solution:
AND: used to clear certain bits in the operand (this is called masking).
Example, clear the high nibbles of BL register
AND BL, 0FH ; (xxxxxxxx AND 0000 1111 = 0000 xxxx)
Example: Clear bit 5 of DH register
AND DH, DFH ; (xxxxxxxxAND1101 1111 = xx0xxxxx)
OR: used to set certain bits in the operand
Example: Set the lower three bits of BL register
OR BL, 07H ; (xxxxxxxxOR 0000 0111 = xxxx x111)
Example: Set bit 7 of AX register
OR AH, 80H ; (xxxxxxxxOR1000 0000 = 1xxxxxxx)
XOR:
Used to invert certain bits (toggling bits)
Used to clear a register by XORed it with itself
DBU, Department of Electrical and Computer Engineering, 2024 10
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM
TEST Instruction:
This instruction performs the AND operation. The difference is that the AND instruction changes the
destination operand, while the TEST instruction does not. A TEST instruction only affects the
condition of the flag register, which indicates the result of the TEST. These flags are effected: ZF, SF,
PF. Result is not stored anywhere.
-
- Where D is destination, it can be register or memory. Count is number of shift, it can be 1 or
CL register
- The SHL and SAL are identical: they shift the operand to left and fill the vacated bits to the
right with zeros.
DBU, Department of Electrical and Computer Engineering, 2024 11
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM
- The SHR instruction shifts the operand to right and fill the vacated bits to the left with zeros.
- The SAR instruction shifts the operand to right and fill the vacated bits to the left with the value
of MSB (this operation used to shift the signed numbers)
There are five basic/primitive instructions for processing strings. They are:
MOVS - This instruction moves 1 Byte or Word of data from memory location to another.
LODS - This instruction loads from memory. If the operand is of one byte, it is loaded into
the AL register, and if the operand is one word, it is loaded into the AX register.
STOS - This instruction stores data from register (AL or AX) to memory.
CMPS - This instruction compares two data items in memory. Data could be of a byte size or
word.
SCAS - This instruction compares the contents of a register (AL or AX) with the contents of
an item in memory.
Each of the above instruction has a byte and word version and string instructions can be repeated by
using a repetition prefix. These instructions use the ES: DI and DS: SI pair of registers, where DI and
SI registers contains valid offset addresses that refer to bytes stored in memory. SI is normally
associated with DS (data segment) and DI is always associated with ES (extra segment).
The DS: SI and ES: DI registers point to the source and destination operands respectively. The source
operand is assumed to be at DS: SI and the destination operand at ES: DI in memory. The following
table provides various versions of string instructions and the assumed space of the operands.
MOVS:
The MOVS instruction is used to copy a data item (byte, or word) from the source string to the
destination string. The source string is pointed by DS: SI and the destination string is pointed by ES:
DI.
a. MOVSB:
-
Copy byte at DS: [SI] to ES: [DI] and then update SI and DI.
-
Syntax: MOVSB (No operands)
-
Algorithm:
ES: [DI] = DS: [SI]
if DF = 0 then
SI = SI + 1
DI = DI + 1
Else
SI = SI – 1
DI = DI – 1
b. MOVSW:
- Copy word at DS: [SI] to ES: [DI] and then update SI and DI.
- Syntax: MOVSW (No operands)
- Algorithm:
ES: [DI] = DS: [SI]
if DF = 0 then
SI = SI + 2
DI = DI + 2
Else
SI = SI – 1
DI = DI – 1
LODS:
It loads strings to registers from memory. If the operand/data is of one byte, it is loaded into the AL
register, if the operand is one word; it is loaded into the AX register.
a. LODSB:
- Load byte at DS: [SI] into AL and then Update SI.
- Algorithm:
AL = DS: [SI]
if DF = 0 then
SI = SI + 1
Else
SI = SI – 1
b. LODSW:
DBU, Department of Electrical and Computer Engineering, 2024 14
ECEg-5172: Microprocessors and Interfacing
(Lecture Notes)
Compiled by: Bushra KM
The STOS instruction copies the data item from AL (for bytes - STOSB) or AX (for words - STOSW)
to the destination string, pointed to by ES: DI in memory.
CMPS:
The CMPS instruction compares two strings. This instruction compares two data items of one byte or
word, pointed to by the DS: SI and ES: DI registers and sets the flags accordingly. You can also use
the conditional jump instructions along with this instruction. It also has two versions; CMSB and
CMSW.
SCAS:
The SCAS instruction is used for searching a particular character or set of characters in a string. The
data item to be searched should be in AL (for SCASB) or AX (for SCASW) registers. The string to be
searched should be in memory and pointed by the ES: DI register. It is similar to CMPS instruction
except the operands of CMPS are both memory locations.
Repetition Prefixes
The REP prefix, when set before a string instruction, for example - REP MOVSB, causes repetition
of the instruction based on a counter placed at the CX register. REP executes the instruction, decreases
CX by 1, and checks whether CX is zero. It repeats the instruction processing until CX is zero.
- Use CLD (Clear Direction Flag, DF = 0) to make the operation left to right.
- Use STD (Set Direction Flag, DF = 1) to make the operation right to left.
Examples:
1. What will be the value of y, and the content of SI & DI, after executing the following program?
(Assume that offset of x = 0102h and offset of y = 0107h)
a b c
org 100h org 100h org 100h
.data .data .data
x db "hello" x db "hello" x db "hello"
y db ? y db ? y db ?
.code .code .code
mov ax, @data mov ax, @data mov ax, @data
mov ds, ax mov ds, ax mov ds, ax
cld std cld
mov si, offset x mov si, offset x mov si, offset x
mov di, offset y mov di, offset y mov di, offset y
movsb movsb movsw
ret ret ret
d e f
org 100h org 100h org 100h
.data .data .data
x db "hello" x db "hello" x db "hello"
y db ? y db ? y db ?
.code .code .code
mov ax, @data mov ax, @data mov ax, @data
mov ds, ax mov ds, ax mov ds, ax
std mov cx, 3 mov cx, 3
mov si, offset x cld std
mov di, offset y mov si, offset x mov si, offset x
movsw mov di, offset y mov di, offset y
ret rep movsb rep movsb
ret ret
g h ANSWERS
org 100h org 100h Y= SI= DI=
.data .data a h 0103h 0108h
x db "hello" x db "hello" b h 0101h 0106h
y db ? y db ? c he 0104h 0109h
.code .code d h 0100h 0105h
mov ax, @data mov ax, @data e hel 0105h 010ah
mov ds, ax mov ds, ax f h 00ffh 0104h
mov cx, 2 mov cx, 2 g hell 0106h 010bh
cld std h h 00FEh 0103h
mov si, offset x mov si, offset x
mov di, offset y mov di, offset y
rep movsw rep movsw
ret ret
The program transfer instructions operate on the instruction pointer and on the CS register; changing the con~
tent of these causes normal sequential execution to be altered.
Four groups of program transfers are available in the 8086/8088 : unconditional transfers, conditional
transfers, iteration control instructions and interrupt-related instructions. Only the interrupt-related
instructions affect any CPU flags.
These instructions are used to execute a series of instructions some number of times. The number is
specified in the CX register. The CX register is automatically decremented by one, each time after
execution of LOOP instruction. Until CX =0, execution will jump to a destination specified by a label
in the instruction.
The destination address for the jump must be in the range of -128 bytes to +127 bytes from the address
of the instruction after the iteration control instruction.
Interrupt Instructions
Interrupts can be seen as a number of functions. These functions make the programming much easier,
instead of writing a code to print a character you can simply call the interrupt and it will do everything
for you. There are also interrupt functions that work with disk drive and other hardware. We call such
functions software interrupts.
Interrupts are also triggered by different hardware, these are called hardware interrupts.
To make software interrupt there is an INT instruction, it has very simple syntax:
INT value
Where value can be a number between 0 to 255 (or 0 to 0FFh), generally we will use hexadecimal
numbers.
Following are some of the most important instructions under this group:
Procedures or subroutines are very important in assembly language, as the assembly language
programs tend to be large in size. Procedures are identified by a name. Following this name, the body
of the procedure is described, which perform a well-defined job. End of the procedure is indicated by
a return statement.
Proc_name PROC
; Here goes the code
; of the procedure ...
RET
Proc_name ENDP
name - is the procedure name, the same name should be in the top and the bottom, this is used to check
correct closing of procedures.
Probably, you already know that RET instruction is used to return to operating system. The same
instruction is used to return from procedure (actually operating system sees your program as a special
procedure).
PROC and ENDP are compiler directives, so they are not assembled into any real machine code.
Compiler just remembers the address of procedure.
The procedure is called from another function by using the CALL instruction. The CALL instruction
should have the name of the called procedure as argument as shown below:
CALL Proc_name
The called procedure returns the control to the calling procedure by using the RET instruction.
Example
org 100h
.data
x db "Hello$"
.code Hello
mov ax, @data
mov ds, ax
mov DX, offset x
CALL print
ret
print proc
mov ah, 09h
int 21h
ret
print endp
3.6.2. Macros
Macros are just like procedures, but not really. Macros look like procedures, but they exist only until
your code is compiled, after compilation all macros are replaced with real instructions. If you declared
a macro and never used it in your code, compiler will simply ignore it.
Unlike procedures, macros should be defined above the code that uses it
<instructions>
ENDM
name parameters
org 100h
.data
x db "Hello$"
y db "world$"
.code
mov ax, @data
mov ds, ax
macro_print x
macro_print y
ret