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

Assembly Languge

The document discusses assembly language programming. It defines assembly language and how it relates to machine language and high-level languages. Assembly language uses mnemonics to represent machine code instructions in a readable way. The document provides examples of assembly language instructions and addressing modes on 8-bit and 16-bit processors. It also describes how to write, assemble, link and run assembly language programs using MASM assembler.

Uploaded by

ytbarekuk
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Assembly Languge

The document discusses assembly language programming. It defines assembly language and how it relates to machine language and high-level languages. Assembly language uses mnemonics to represent machine code instructions in a readable way. The document provides examples of assembly language instructions and addressing modes on 8-bit and 16-bit processors. It also describes how to write, assemble, link and run assembly language programs using MASM assembler.

Uploaded by

ytbarekuk
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 72

Debre Berhan University

Department of Electrical and


Computer Engineering
Micro computer and Interfacing

Tesfai Gebrekirstos
CHAPTER 2
Assembly Language
Programming, Addressing modes
and Instruction Sets

10/22/19 2
1. Assembly Language Programming
• Introduction to assembly language
• Machine Language:
• The binary form of the program is referred to as machine language
because it is the form required by the machine. OR
• Programs consist of 0’s and 1’s are called machine language.
• Assembly languages:
• Assembly Language is a low-level language. Deals directly with
the internal structure of CPU.
• Assembly Language : provides mnemonics for machine code
instructions.
• Mnemonics refer to codes and abbreviations to make it easier for
the users to remember.
10/22/19 3
Contd…
• High Level Languages: Another way of writing
program for a microcomputer is high- level languages
such as FORTRAN, PASCAL, C#, JAVA, C++ or
Python.
• These languages use program statements, which are
even more English-like than those of assembly language.
• Each high- level statement may represent many machine
code instructions.
• An interpreter program or compiler program is used to
translate higher-level language statements to machine
codes, which can be loaded into memory and executed.

10/22/19 4
• Assembler translates Assembly language
program into machine code.
• In high-level languages, Pascal, Basic, C,C+
+,Java; the programmer does not have to be
concerned with internal details of the CPU.
• Compilers translate the program into machine
code.
However, it is difficult, if not impossible, for a
programmer to memorize the thousands of binary
instruction codes for a CPU such as the 8086.
10/22/19 5
What is Assembly Language?
• Each personal computer has a microprocessor that
manages the computer's arithmetical, logical and control
activities.
• Each family of processors has its own set of instructions
for handling various operations like getting input from
keyboard, displaying information on screen and
performing various other jobs.
• These set of instructions are called 'machine language
instructions'.
• Processor understands only machine language
instructions which are strings of 1’s and 0’s.
• However machine language is too obscure and complex
for using in software development. 6
• So the low level assembly language is designed for a
specific family of processors that represents various
instructions in symbolic code and a more understandable
form.
• Advantages of Assembly Language:
 An understanding of assembly language provides
knowledge of:
• Interface of programs with OS, processor and BIOS;
• Representation of data in memory and other external
devices;
• How processor accesses and executes instruction;
• How instructions accesses and process data;

10/22/19 7
• How a program access external devices.
Other advantages of using assembly language
are:
• It requires less memory and execution time;
• It allows hardware-specific complex jobs in an
easier way;
• It is suitable for time-critical jobs;
• It is most suitable for writing interrupt service
routines and other memory resident programs.

10/22/19 8
• Example: (8-bit)
MOV CL, 55H ; move 55H into register CL
MOV DL, CL ; move/copy the contents of
CL into DL (now DL=CL=55H)
MOV BH, DL ; move/copy the contents of
DL into BH (now DL=BH=55H)
MOV AH, BH ; move/copy the contents of
BH into AH (now AH=BH=55H)

10/22/19 9
• Example: (16-bit)
• MOV CX, 468FH ; move 468FH into CX (now
CH =46, CL=8F)
• MOV AX, CX ; move/copy the contents of
CX into AX (now AX=CX=468FH)
• MOV BX, AX ; now BX=AX=468FH
• MOV DX, BX ; now DX=BX=468FH
• MOV DI, AX ; now DI=AX=468FH
• MOV SI, DI ; now SI=DI=468FH
• MOV DS, SI ; now DS=SI=468FH
• MOV BP, DS ; now BP=DS=468FH

10/22/19 10
Assembly Language:
 To make programming easier, many programmers write programs in
assembly language.
 Assembly language uses two-, three-, or four-letter mnemonics to
represent each instruction type.
 The letters in an assembly language mnemonic are usually initials or a
shortened form of the English word(s) for the operation performed by
the instruction.
 E.g. the mnemonic for subtract is SUB, the mnemonic for Exclusive
OR is XOR, and the mnemonic for the instruction to copy data from
one location to another is MOV.
 8088/8086 Machine Language:
 Native language of the 8088/8086 (PC) is “machine language (code)”.
 One to one correspondence to assembly language statements.
 Instructions are encoded with 0’s and 1’s.
 Machine instructions can take up from 1 to 6 bytes.. 11
Assignment of assembly language
• In Java, assignment takes the form:
x = 42 ;
y = 24;
z = x + y;
• In assembly language we carry out the same
operaion but we use an instruction to denote the
assignment operator (“=” in Java).
mov ax, 42
mov bx, 24
add ax, bx
12
• The mov instruction carries out assignment in
8086 assembly language.
• It allows us to place a number in a register or in a
memory location (a variable) i.e. it assigns a
value to a register or variable.
• Example: Store the ASCII code for the letter A
in register bx.
• A has ASCII code 65d (01000001B, 41H)
• The following mov instruction carries out the
task:
• mov bx, 65d
13
• We could also write it as:
mov bx, 41h
or mov bx, 01000001b
or mov bx, ‘A’
• All of the above are equivalent. They each carry out
exactly the same task, namely the binary number
representing the ASCII code of A is copied into the bx
register.
• we could also have written it as:
• mov bl, 65d
• mov bl, ‘A’
• Since register bl represents the low-order byte of
register bx. 14
• Note: The 8086 Assembler converts a character
constant i.e. a character in single quotes (e.g.
‘A’) to its ASCII code automatically.
• This is a very useful feature and it means that
you can specify many characters without having
to look up their ASCII code.
• You simply enclose the character in single
quotes.

10/22/19 15
Structure of an Assembly Language Statement
 General structure of an assembly language
statement:
LABEL: INSTRUCTION ;COMMENT
 Label—address identifier for the statement.
 Instruction—the operation to be performed.
 Comment—documents the purpose of the statement.
 Example:
START: MOV AX, BX ; COPY BX into AX
 Not all instructions need a comment.

10/22/19 16
Compilers, Assemblers, Linkers & Loaders

Compilation Process

17
Assembler and the Source Program
 Assembly language program:
 Assembly language program (.asm) file—known as “source code”.
 Converted to machine code by a process called “assembling”.
 Assembling performed by a software program — an “8088/8086
assembler”.
• “Machine (object ) code” that can be run on a PC is output in the
executable (.exe) file.
• “Source listing” output in (.lst) file—printed and used during
execution and debugging of program.
 DEBUG—part of “disk operating system (DOS)” of the PC
 Permits programs to be assembled and disassembled.
 Line-by-line assembler.
 Also permits program to be run and tested.
 MASM—Microsoft 80x86 macroassembler.
 Allows a complete program to be assembled in one step. 18
 After typing the program save the file with appropriate file name
with an extension .ASM
Example: Add.ASM
Assembling an ALP
 To assemble an ALP we need executable file called MASM.EXE.
Only if this file is in current working directory we can assemble
the program. The command is: MASM<filename.ASM>
 If the program is free from all syntactical errors, this command
will give the Object file. In case of errors it list out the number of
errors, warnings and kind of error.
 Note: No object file is created until all errors are rectified.
 Linking:
 After successful assembling of the program we have to link it to
get Executable file. The command is LINK<File name.OBJ>

10/22/19 19
Assembly Characteristics: Data Types
“Integer” data of 1, 2, or 4 bytes
–Data values
–Addresses
Floating point data of 4, 8, or 10 bytes
No aggregate types such as arrays or structures.
–Just contiguously allocated bytes in memory.
Assembly Characteristics: Operations
• Perform arithmetic function on register or
memory data.
10/22/19 20
Transfer data between memory and register
–Load data from memory into register.
–Store register data into memory.
Transfer control
–Unconditional jumps to/from procedures.
–Conditional branches.

10/22/19 21
How to open and run MASM
• In the DOS:
• C:\Users\Tesfay>cd..
• C:\Users>cd..
• C:\>cd “Program Files”
• C:\ Program Files>cd masm
• C:\ Program Files\ masm>edit
• Writing an ALP
• Assembly level programs generally abbreviated as ALP are
written in text editor Edit.
• Type Edit in front of the command prompt to open an untitled text
file.
• Edit<file name>

10/22/19 22
 Instructions are organized into groups of functionally related instructions:
• Data Transfer instructions
• Input/output instructions
• Arithmetic instructions
• Logic instructions
• String Instructions
• Control transfer instructions
• In assembly language each instruction is represented by a “mnemonic” that
describe its operation and is called its “operation code (opcode)”
• MOV = move → data transfer
• ADD = add → arithmetic
• JMP = unconditional jump → control transfer
• Operands: Identify whether the elements of data to be processed are in registers
or Memory.
 Source operand– location of one operand to be processed.
 Destination operand—location of the other operand to be processed and the
location of the result.

10/22/19 23
• Addressing Modes of 8086

10/22/19 24
Addressing modes describe the types of operands
and the way they are accessed for executing an
instruction.
There are in total eight addressing modes for 8086
instructions to specify operand. These are:
1.Register Addressing :- In this mode of
addressing an 8-bit or 16 bit general purpose
register contains an operand.
Example: MOV BX,CX ; move the content of CX
reg. to BX reg.
ADD AL,CH
10/22/19
ADD CX,DX 25
2. Immediate Addressing:-In this addressing
mode, the operand is contained in the instruction
itself.
Example:- MOV AL,58H ; move 58H to
AL register.
MOV BX, 0354H ; move 0354H to BX
register.
ADD AX, 0395H ; Add 0395H to the
content of AX reg.
• An offset is called effective address. The offset
is determined by adding any combination of the
ff three offset address elements:
10/22/19 26
• Displacement, base and index. The combination depends
on the addressing mode of the instruction to be executed.
• Displacement: It is an 8-bit or 16-bit immediate value
given in the instruction.
• Base: It is the content of the base register, BX or BP.
• Index: It is the content of the index register, SI or DI.
3. Direct Addressing:- In this mode of addressing an
effective address (or offset) is given in the instruction
itself.
Example: MOV AL, [0300H] ; this instruction will move
the content of the offset address 0300H to AL.
MOV [0401H], AX ; this instruction will move
the content of AX to the offset address 0401H.
27
• 4. Register Indirect Addressing:-
The operand’s offset is in the base register, BX or
base pointer, BP or in an index register (SI or DI)
specified in the instruction.
• Example: ADD CX,[BX] ; this will add the
contents of the memory locations addressed by
register BX to the register CX.
MOV DX,[SI]; the content of the memory
location addressed by SI will move to DX.

10/22/19 28
5. Based Addressing:- The operand’s offset is the
sum of the contents of the base register , BX or
BP and an 8-bit or 16-bit displacement.
Offset (effective address)= [BX or BP +8-bit or 16-
bit displacement].
Example: ADD AL, [BX + 04]; case of 8-bit
displacement. Suppose, BX contains 0301H. So
0301 + 04 =0305H. The content of [BX + 4] will
be added to the content of AL, and the result will
be placed in AL.
ADD AL, [BX + 1243H] ; case of 16-bit
displacement.
29
6. Indexed Addressing:- the operand’s offset is
computed by adding an 8-bit or 16-bit
displacement to the content of an index register
SI or DI.
Offset= [SI or DI + 8-bit or 16-bit displacement].
Example: ADD AX, [SI + 08]
MOV CX, [SI + 1523H].
7. Based Indexed Addressing:- The operand’s
offset is computed by adding the contents of a
base register to the contents of an index register.
Off set = [BX or BP] + [SI or DI]. BX is used as a
base register for stack segment. 30
Example:- MOV AX, [BX + SI]
ADD CX, [BX + SI]
8. Based Indexed with Displacement:- The
operand’s offset is computed by adding a base
register’s contents, an index register’s contents
and an 8-bit or 16-bit displacement.
Offset = [BX or BP] + [SI or DI] + Displacement.
BX is used with data segment, where as BP is
used with stack segment.
Example:- MOV AX, [BX + SI + 05 ]
ADD CX, [BX + SI + 1212H]
31
2.INSTRUCTION SET OF 8086 MP

• Classification of Instructions

10/22/19 32
1. Data Transfer Instructions
MOV (copy) instruction; MOV D,S → (S) =>
(D).
XCHG- exchanges the contents of a Reg with the
contents of any other Reg or Mem location.
Note:-This instruction cannot exchange segment
registers or memory–to- memory data.
It can use any addressing modes discussed above
except immediate addressing.

10/22/19 33
E.g.
XCHG [5000H], AX ; exchange contents of AX
with contents in 5000H.
XCHG AX, BX ; exchange AX with BX. This can
be implicit addressing.
XCHG [SUM], BX ; exchange BX with the SUM.
XCHG AL, CL

10/22/19 34
• XLAT :. Translates (converts) the contents of the
AL register into a number stored in a memory
table. It is used to find out the codes in case of
code conversion problems, using lookup table
techniques.
E.g. It is used in -ASCII to 7-segment display
conversion.
ASCII-to-EBCDIC conversion.
AL contains offset of the element to be accessed
from the beginning of the lookup table.
(AL) ←( (BX) + (AL) ) ; translate
10/22/19 35
E.g. Assume (DS)=0300H, (BX)=0100H,
(AL)=41H (ASCII code for character A),
EBCDIC code for A = C1H
MOV AX, SEGTABLE ; address of the segment
containing look-up table.
MOV DS, AX ; is transferred to DS
MOV AL, CODE ; code of the pressed key (say
A=41) as offset is transferred in AL.
MOV BX, OFFSET TABLE ; offset of the code
lookup table in BX.
XLAT(MOV AL,[AL][BX]) ; find the equivalent
code and store in AL. 36
P.A=DSx10 + (BX) + (AL) = 03000+0100H + 41H
= 03141H
then (AL) ← (03141H)
Note:-DS and BX must be initialized first before
executing XLAT.
LEA:. Load effective address. Loads the offset of
an operand in the specified register.
E.g. LEA BX, NUMB ;load BX with the offset
address of NUMB.
LEA SI, [INPUT] ;loads the offset address
specified by [INPUT] (content of INPUT).
37
LEA, LDS, LES Instructions:

10/22/19 38
LDS-load the specified register as well as the DS
register.
LES -load the specified register as well as the ES
register.
E.g.
LDS DI, LIST
LDS BX, 5000H
LES BX, CAT
LES BX, 4000H

10/22/19 39
LAHF -Load AH from lower byte of flag register.
SAHF -store AH to lower byte of flag register.
PUSHF -push flag to stack.
-first the upper byte, then the lower byte.
POPF -pop flags from stack.

10/22/19 40
• IN /OUT Ins:. -IN -input from the port.
-OUT-Output to the port.
• AL and AX are the allowed destinations for 8-bit and 16-bit I/O
operations.
• DX is the implicit address of I/O ports.
• E.g.
• IN AL, 30H ; read from an 8-bit port whose address is 0030H
in to AL
• IN AX, 0400H
• IN AX ; read from a 16-bit port whose implicit address is in
DX in to AX
• IN AX, DX
• OUT 30H;
• OUT 0400H
• OUT AX ;
41
• OUT AX, DX
• PUSH/POP:. PUSH- push to stack. Pushes the contents
of the specified Reg/Mem location on to stack.
• Higher byte is pushed first, then lower byte.
• PUSH Reg16
• PUSH Mem16
• PUSH Seg ; Seg can be any of CS, DS, ES, SS
• PUSH imm8 ; not available in the earlier 8086/8088
MPs.
• PUSH imm16 ; not available in the earlier 8086/8088
MPs.
• PUSHA ; save all 16-bit registers except Seg registers
as AX, CX, DX, BX, SP, BP, SI, and DI. ; not
available in the earlier 8086/8088 MPs.
42
• PUSHF ; save flags
• POP:. performs the inverse operation of a PUSH instruction.
• POP Seg ; Seg can be any of DS, ES, SS, but not CS
• Eg
• POP CS ; invalid, CS cannot popped.
• POP [5000H]

2. Arithmetic Instructions:
• Addition Instructions:
o ADD Add specified byte-to-byte or specified word to word.
• Format:
• operand1 = operand1 + operand2
• MOV AL, 5 ; AL = 5
• ADD AL, -3 ; AL = 2
• RET
o ADC-Add with Carry-ADC
• Format:
• operand1 = operand1 + operand2 + CF
• Example: STC ; set CF = 1
• MOV AL, 5 ; AL = 5
• ADC AL, 1 ; AL = 7 43

INC-increment- INC Destination
• Format:
• operand = operand + 1
• Example:
MOV AL, 4
INC AL ; AL = 5
o AAA-ASCII Adjust After Addition
o This allows us to add the ASCII codes for two decimal digits.
o After the addition, the AAA Instruction is used to make sure the result is the correct BCD.
o Example:
Assume AL = 0 0 1 1 0 1 0 1, ASCII 5
BL = 0 0 1 1 1 0 0 1, ASCII 9
ADD AL,BL Result: AL= 0 1 1 0 1 1 1 0 = 6EH, which is
incorrect BCD
AAA 14 decimal
DAA-Decimal Adjust after BCD Addition
• Decimal adjust After Addition.
• Corrects the result of addition of two BCD values.
AL = 0101 1001 = 59 BCD
BL = 0011 0101 = 35 BCD
ADD AL, BL AL = 1000 1110 = 8EH
44
DAA Add 01 10 because 1110 > 9 AL = 1001 0100 = 94 BCD
DEC-Decrement Destination Register or Memory
• This instruction subtracts 1 from the destination.
• Format :
• operand = operand – 1
Example : DEC CL
DEC BP
Example2:
MOV AL, 255 ; AL = 255
DEC AL ; AL = 254
NEG- (Form 2's Complement)
Format:
Invert all bits of the operand.
Add 1 to inverted operand. 45
Subtraction Instructions:
o SUB-Subtract
• Format:
• operand1 = operand1 - operand2
• Example:
MOV AL, 5
SUB AL, 1 ; AL = 4
RET

o SBB-Subtract with Carry


o Format:
o operand1 = operand1 - operand2 – CF
• Example:
STC
MOV AL, 5
SBB AL, 3 ; AL = 5 - 3 - 1 = 1
RET 46
o CMP-Compare Byte or Word
The comparison is done by subtracting the source
byte or word from the destination byte or word.
o Format: operand1 - operand2
Example:
MOV AL, 5
MOV BL, 5
CMP AL, BL ; AL = 5, ZF = 1

10/22/19 47
CMP CX, BX CF, ZF, and SF will be left as follows:
CF ZF SF
CX = BX 0 1 0 Result of subtraction is 0
CX > BX 0 0 0 No borrow required, so CF = 0
CX < BX 1 0 1 Subtraction required borrow, so CF = 1
• AAS--ASCII Adjust After Subtraction
The 8086 allows us to subtract the ASCII codes for two decimal
digits.
The AAS instruction is then used to make sure the result is the
correct BCD number.
Example: ASCII 9-ASCII 5 (9-5)
AL = 00111001 = 39H = ASCII 9
BL = 001 10101 = 35H = ASCII 5
SUB AL, BL Result: AL = 00000100 = BCD 04
AAS Result: AL = 00000100 = BCD 04. 48
DAS-Decimal Adjust after BCD Subtraction
Example: AL 1000 0110 86 BCD
BH 0101 0111 57 BCD
SUB AL,BH AL 0010 1111 2FH, CF = 0
DAS Lower nibble of result is 1111, so DAS automatically

subtracts 0000 0110 to give AL = 00101001 = 29 BCD

Multiplication Instruction
o MUL---Multiply Unsigned Bytes or Words
when operand is a byte:
• AX = AL * operand.
• when operand is a word:
• (DX AX) = AX * operand. 49
Example: MUL BH AL times BH, result in AX
MUL CX AX times CX, result high
word in DX, low word in AX

o IMUL-Multiply Signed Numbers


This instruction multiplies a signed byte from some source times a
signed byte in AL or a signed word from some source times a
signed word in AX.
Example: IMUL BH Signed byte in AL times signed byte in
BH, result in AX
IMUL AX AX times AX, result in DX and AX

10/22/19 50
AAM-ASCII Adjust after Multiplication
• Before we can multiply two ASCII digits, we must first mask the
upper 4 bits of each. Corrects the result of multiplication of two
BCD values.
 After the two unpacked BCD digits are multiplied, the AAM
instruction is used to adjust the product to two unpacked BCD
digits In AX.
Example:
AL 00000101 unpacked BCD 5
BH 00001001 unpacked BCD 9
MUL BH AL x BH; result in AX
AX = 00000000 00101101 = 002DH
AAM AX = 00000100 00000101 = 045H,
which is unpacked BCD for 45.

10/22/19 51
Division Instruction
• DIV-Unsigned Divide
• This instruction is used to divide an unsigned word by a byte or
to divide an unsigned double word (32 bits) by a word.
• Example:
MOV AX, 203 ; AX = 00CBh
MOV BL, 4
DIV BL ; AL = 50 (32h), AH = 3
RET
IDIV-Divide by Signed Byte or Word
• This instruction is used to divide a signed word by a signed byte,
or to divide a signed double word (32 bits) by a signed word.
• Example: MOV AX, -405
MOV BL, 4
IDIV BL ; AL = -101 , AH = -1
10/22/19
RET 52
AAD- BCD Adjust before Division.
• Prepares two BCD values for division.
• Example:
MOV AX, 0309 ;dividend in AX in BCD.
AAD ;BCD in AX converted to hex
and placed in AL
MOV BL, 04 ;divisor in BL in binary.
DIV BL ; divide AX by BL.

10/22/19 53
3. Bit Manipulation Instructions:
• Logical Instructions
• NOT- Invert each bit of the operand.
• Format:
• If bit is 1 turn it to 0.
• If bit is 0 turn it to 1.
• Example:
MOV AL, 00011011b
NOT AL ; AL = 11100100b
RET
• AND-Logical AND between all bits of two operands.
• These rules apply:
• 1 AND 1 = 1
• 1 AND 0 = 0
• 0 AND 1 = 0
• 0 AND 0 = 0
54
• Example:
MOV AL, 'a' ; AL = 01100001b
AND AL, 11011111b ; AL = 01000001b ('A')
RET
OR- OR Logical between all bits of two operands.
• These rules apply:
• 1 OR 1 = 1
• 1 OR 0 = 1
• 0 OR 1 = 1
• 0 OR 0 = 0
• Example:
MOV AL, 'A' ; AL = 01000001b
OR AL, 00100000b ; AL = 01100001b ('a')
RET
10/22/19 55
XOR -Logical XOR (Exclusive OR) between all bits of
two operands.
• These rules apply:
• 1 XOR 1 = 0
• 1 XOR 0 = 1
• 0 XOR 1 = 1
• 0 XOR 0 = 0
• Example:
MOV AL, 00000111b
XOR AL, 00000010b ; AL = 00000101b
RET

10/22/19 56
o Shift Instructions:
o SHL-Shift Operand Bits Left, Put Zero in LSB(s)
o CF ← MSB ← LSB ← 0
Example: MOV AL, 11100000b
SHL AL, 1 ; AL = 11000000b, CF=1.
RET

o SHR-Shift Operand Bits Right, Put Zero in MSB(s)


0 → MSB → LSB → CF
CF contains the bit most recently shifted in from the LSB.
Example1:
MOV AL, 00000111b
SHR AL, 1 ; AL = 00000011b, CF=1.
RET
57
o ROL-Rotate Bits of Operand Left, MSB to LSB
Example1: ROL AX, 1 Word in AX 1bit position left,
MSB to LSB and CF
• Example2 :
MOV AL, 1Ch ; AL = 00011100b
ROL AL, 1 ; AL = 00111000b, CF=0.
RET

10/22/19 58
ROR-Rotate Bits of Operand Right, LSB to MSB
• Example:
MOV AL, 1Ch ; AL = 00011100b
ROR AL, 1 ; AL = 00001110b, CF=0.
RET

o RCL-Rotate Operand to the Left through CF


• shift all bits left, the bit that goes off is set to CF and previous
value of CF is inserted to the right-most position.
• Example:
STC ; set carry (CF=1).
MOV AL, 1Ch ; AL = 00011100b
RCL AL, 1 ; AL = 00111001b, CF=0.
RET
10/22/19 59
RCR-Rotate Operand to the Right through CF
• Shift all bits right, the bit that goes off is set to CF and previous
value of CF is inserted to the leftmost position.
• Example:
STC ; set carry (CF=1).
MOV AL, 1Ch ; AL = 00011100b
RCR AL, 1 ; AL = 10001110b, CF=0.
RET
4. String Instructions
o MOVS/MOVSB/MOVSW -Move String Byte or String Word
This instruction copies a byte or a word from a location in the data
segment to a location in the extra segment.
 The offset of the source byte or word in the data segment must be
in the SI register.
 The offset of the destination in the extra segment must be 60
contained in the DI register.
Example:
MOV SI, OFFSET SOURCE_STRING Load offset of start of source string in
DS into SI
MOV DI, OFFSET DESTINATION-STRING Load offset of start of destination
string in ES into DI

o LODS/LODSB/LODSW Load String Byte into AL or Load


String Word into AX
o This instruction copies a byte from a string location pointed to by SI to AL, or a
word from a string location pointed to by SI to AX.
EXAMPLE:
LODSB, Load AL with the content of memory locations specified by SI
register.

MOV SI, 0301H ;memory address in SI.


LODSB ; load [SI] in to AL.
INT3 ;interrupt.
61
STOS/STOSB/STOSW-Store Byte or Word in String
 The STOS instruction copies a byte from AL or a word from AX to a memory location in
the extra segment pointed to by DI.
 In effect, it replaces a string element with a byte from AL or a word from AX.
 After the copy, DI is automatically incremented or decremented to point to the next string
element in memory.
 If the direction flag (DF) is cleared, then DI will automatically be incremented by 1 for a
byte string or incremented by 2 for a word string.
 If the direction flag is set, DI will be automatically decremented by 1 for a byte string or
decremented by 2 for a word string.
 Store AL or AX in to the memory locations addressed by DI.
 Example:
MOV AX, 7642H ;Data in AX
MOV DI, 0302H ; memory address in DI.
STOSW ; store [AX] in [DI].
INT3 ; interrupt.
Result
0302 42H
0303 76H
62
SCAS/SCASB/SCASW-Scan a String Byte or a String Word
• SCAS compares a byte in AL or a word in AX with a byte or word pointed to by
DI in ES.
• Therefore, the string to be scanned must be in the extra segment, and DI must
contain the offset of the byte or the word to be compared.
• If the direction flag is cleared (0), then DI will be incremented after SCAS. If
the direction flag is set (1), then DI will be decremented after SCAS. For byte
strings, DI will be incremented or decremented by 1, and for word strings, DI
will be incremented or decremented by 2.
• Example:
MOV DI, OFFSET BLOCK ;address data
CLD ;auto-increment
MOV CX, 100 ;load counter
REPNE SCASB ;search

10/22/19 63
REP/REPE/REPZ/REPNE/REPNZ- (Prefix) Repeat String
Instruction until Specified Conditions Exist
REP is a prefix, which is written before one of the string
instructions. It will cause the CX register to be decremented and
the string instruction to be repeated until CX = 0.
REPE and REPZ are two mnemonics for the same prefix.
They stand for Repeat if Equal and Repeat if Zero, respectively.
REPE or REPZ is often used with the Compare String instruction
or with the Scan String instruction.
REPE or REPZ will cause the string instruction to be repeated as
long as the compared bytes or words are equal (ZF = 1) and CX is
not yet counted down to zero.
In other words, there are two conditions that will stop the
repetition: CX = 0 or string bytes or words not equal.

10/22/19 64
5. Program Execution Transfer Instructions
 Conditional Transfer Instructions:
JA/JNBE Jump if above/jump if not below or equal.
JAE/JNB Jump if above or equal/jump if not below.
JB/JNAE Jump if below/jump if not above or equal.
JBE/JNA Jump if below or equal/jump if not above.
JC Jump if carry flag CF = 1.
JE/JZ Jump if equal/jump if zero flag ZF = 1.
JG/JNLE Jump if greater/jump if not less than or equal.
JGE/JNL Jump if greater than or equal/ Jump if not less than.
JL/JNGE Jump if less than/jump if not greater than or-equal.
JLE/JNG Jump if less than or equal/jump if not greater than.
JNC Jump if no carry (CF = 0).
JNE/JNZ Jump if not equal/jump if not zero (ZF = 0).
JNO Jump if no overflow (overflow flag OF = 0).
JNP/JPO Jump if not parity/jump if parity odd (PF = 0).
JNS Jump if not sign (sign flag SF= 0).
JO Jump if overflow flag OF = 1.
JP/JPE Jump if parity/jump if parity even (PF = 1).
JS Jump if sign (SF=1)

10/22/19 65
• Iteration Control Instructions:
LOOP Loop through a sequence of instructions until CX =
0.
LOOPE/LOOPZ Loop through a sequence of instructions while ZF
=1 and CX ≠ 0.
LOOPNE/LOOPNZ Loop through a sequence of instructions while ZF = 0 and
CX ≠ 0.
JCXZ Jump to specified address if CX = 0.

• Interrupt Instructions:
INT Interrupt program execution, call service procedure (ISR-
Interrupt Service Routine).
INTO Interrupt program execution if OF = 1.
IRET Return from interrupt service procedure to main program.

10/22/19 66
6. Processor Control Instructions
Flag Set/Clear Instructions:
STC Set carry flag CF to 1.
CLC Clear carry flag CF to 0.
CMC Complement the state of the carry flag CF.
STD Set direction flag DF to 1.
CLD Clear direction flag DF to 0.
STI Set interrupt enable flag to 1 (enable INTR input).
CLI Clear interrupt enable flag to 0 (disable INTR input).

10/22/19 67
Contd…
o External Hardware Synchronization Instructions:
HLT Halt (do nothing) until interrupt or reset.
WAIT Wait (do nothing) until signal on the TEST pin is low.
ESC Escape to external coprocessor such as 8087 or 8089
LOCK An instruction prefix. Prevents another processor from
taking the bus (in MAX mode)
 No Operation Instruction:

NOP No action except fetch and decode.

10/22/19 68
Examples
1. To Copy a String from one set of locations to another
data segment
testmsg db 'Mekelle Institute of Technology'
len dw 30
data ends
dst segment
dest db 30 dup(0)
dst ends
code segment
assume cs:code,ds:data,es:dst
start:mov ax,data
mov ds,ax
mov ax,dst
mov es,ax
lea si,testmsg
lea di,dest
mov cx,len
cld
rep movsb
mov ah,4ch
int 21h
code ends
69
end start
2. Program to find the largest of 5 bytes
data segment
arr db 09,02,99,02,05
count dw 05h
lar db ?
data ends
code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov si,offset[arr]
mov cx,count
dec cx
mov al,[si]
up: inc si
cmp al,[si]
jnc down1
mov al,[si]
down1: loop up
mov lar,al
int 3
code ends
end start
70
3. To Sort an array in ascending order (Using Bubble
Sorting Method)
data segment
arr db 5, 4, 3, 1, 2
count dw 05
data ends
code segment
assume cs:code, ds:data
start:mov ax,data
mov ds,ax
mov cx,count
dec cx
up2: mov dx,cx
mov bx,0000h
up1: mov al,arr[bx]
cmp al,arr[bx+1]
jbe down
xchg al,arr[bx+1]
mov arr[bx],al
down: inc bx
loop up1
mov cx,dx
loop up2
int 3h
code ends
end start

71
END OF CHAPTER 2

10/22/19 72

You might also like