Advanced Microprocessors 8086
Advanced Microprocessors 8086
Advanced Microprocessors 8086
CSE 401 ADVANCED MICROPROCESSORS
Chapter 2
Assembly Language Programming
Compiled by : Dr Manoj V.N.V.
1
Chapter 2 Assembly Language Programming
CHAPTER
2.1 Introduction
To run a program, a microcomputer must have the program stored in binary
form in successive memory locations. There are three language levels that can be
used to write a program for a microcomputer.
Machine Language:
We can write programs as simply a sequence of the binary codes for the
instructions we want the micro- computer to execute. The binary form of the
program is referred to as machine language because it is the form required by the
machine. 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.
Also, it is very easy for an error to occur when working with long series of 1's
and 0's. Using hexadecimal representation for the binary codes might help some,
but there are still thousands of instruction codes to cope with.
Assembly Language:
To make programming easier, many programmers write programs in
assembly language. They then translate the assembly language program to
machine language so that it can be loaded into memory and run. Assembly
language uses two-, three-, or four-letter mnemonics to represent each
instruction type. A mnemonic is just a device to help us to remember something.
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. For
example, 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.
High Level Languages:
Another way of writing program for a microcomputer is with high-level
languages such as BASIC, C, JAVA or C++. 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 a compiler program is used to translate higher-level
language statements to machine codes, which can be loaded into memory and
executed.
2
Chapter 2 Assembly Language Programming
Note:
1. D8 and D16 represent 8 and 16 bit displacements respectively.
2. The default segment for the addressing modes using BP and SP is SS. For all other addressing modes the
default segments are DS or ES.
When a data is to be referred as an operand, DS is the default data segment
register. CS is the default code segment register for storing program codes
(executable codes) . SS is the default segment register for the stack data accesses
and operations. ES is the default segment register for the destination data
storage. All the segments available (defined in a particular program) can be read
or written as data segments by newly defining the data segment as required.
There is no physical difference in the memory structure or no physical separation
between the segment areas. They may or may not overlap with each other.
According to the flow of instruction execution, the instructions may be
categorized as (i) Sequential control flow instructions and (ii) Control transfer
instructions.
Sequential control flow instructions are the instructions, which after
execution, transfer control to the next instruction appearing immediately after it
(in the sequence) in the pro- gram. For example, the arithmetic, logical, data
transfer and processor control instructions are sequential control flow
instructions. The control transfer instructions, on the other hand, transfer control
3
Chapter 2 Assembly Language Programming
4
Chapter 2 Assembly Language Programming
5
Chapter 2 Assembly Language Programming
LAHF Load (copy to) AH with the low byte of the flag register.
SAHF Store (copy) AH register to low byte of flag register.
PUSHF Copy flag register to top of stack.
POPF Copy word at top of stack to flag register.
(2) Arithmetic Instructions. Under this category there are following group of
instructions
Addition Instructions:
ADD Add specified byte-to-byte or specified word to word.
ADC Add byte + byte + carry flag or word + word + carry flag.
INC Increment specified byte or specified word by 1.
AAA ASCII adjust after addition.
DAA Decimal (BCD) adjust after addition.
Subtraction Instructions:
SUB Subtract byte from byte or word from word.
SBB Subtract byte and carry flag from byte or word and carry flag from word.
DEC Decrement specified byte or specified word by 1.
NEG Negate-Invert each bit of a specified byte or word and add 1 (form 2's
complement).
CMP Compare two specified bytes or two specified words.
AAS ASCII adjust after subtraction.
DAS Decimal (BCD) adjust after subtraction.
Multiplication Instructions:
MUL Multiply unsigned byte-by-byte or unsigned word by word.
IMUL Multiply signed byte by byte or signed word by word.
AAM ASCII adjust after multiplication.
Division Instructions:
DIV Divide unsigned word by byte or unsigned double word by word.
IDIV Divide signed word by byte or signed double word by word.
AAD ASCII adjust before division.
CBW Fill upper byte of word with copies of sign bit of lower byte.
CWD Fill upper word of double word with sign bit of lower word.
(3) Bit Manipulation Instructions. Under this category there are following group
of instructions
Logical Instructions:
NOT Invert each bit of a byte or word.
AND AND each bit in a byte or word with the corresponding bit in
another byte or word.
OR OR each bit in a byte or word with the corresponding bit in another
byte or word.
XOR Exclusive OR each bit in a byte or word with the corresponding bit
in another byte or word.
TEST AND operands to update flags, but don't change operands.
6
Chapter 2 Assembly Language Programming
Shift Instructions:
SHL/SAL Shift bits of word or byte left, put zero(s) in LSB(s).
SHR Shift bits of word or byte right, put zero(s) in MSB(s).
SAR Shift bits of word or byte right, copy old MSB into new MSB
Rotate Instructions:
ROL Rotate bits of byte or word left, MSB to LSB and to CF.
ROR Rotate bits of byte or word right, LSB to MSB and to CF.
RCL Rotate bits of byte or word left, MSB to CF and CF to LSB.
RCR Rotate bits of byte or word right, LSB to CF and CF to MSB.
(4) String Instructions: A string is a series of bytes or a series of words in
sequential memory locations. A string often consists of ASCII character codes. In
this list, a ‘/’ is used to separate different mnemonics for the same instruction.
Use the mnemonic, which most clearly describes the functions of the instruction
in a specific application. A ‘B’ in a mnemonic is used to specifically indicate that
a string of bytes is to be acted upon. A ‘W’ in the mnemonic is used to indicate
that a string of words is to be acted upon.
REP An instruction prefix. Repeat following instruction until CX = 0.
REPE/REPZ An instruction prefix. Repeat instruction until CX = 0 or zero flag
ZF ≠ 1.
REPNE/REPNZ An instruction prefix. Repeat until CX = 0 or ZF = 1.
MOVS/MOVSB/MOVSW Move byte or word from one string to another.
7
Chapter 2 Assembly Language Programming
8
Chapter 2 Assembly Language Programming
MOV‐‐‐Copy a Word or Byte‐‐‐MOV Destination, Source
The MOV instruction copies a word or byte of data from a specified
source to a specified destination. The destination can be a register or a memory
location. The source can be a register, a memory location, or an immediate
number. The source and destination in an instruction cannot both be memory
locations. The source and destination in a MOV instruction must both be of type
byte, or they must both be of type word. MOV instructions do not affect any
flags.
EXAMPLES:
MOV CX, 037AH Put the immediate number 037AH in CX
MOV BL, [437AH] Copy byte in DS at offset 437AH to BL
MOV AX, BX Copy contents of register BX to AX
MOV DL, [BX] Copy byte from memory at [BX] to DL.
MOV DS, BX Copy word from BX to DS register
MOV RESULTS[BP],AX Copy AX to two memory locations-AL to the first
location, AH to the second. EA of the first memory location is the sum of the
displacement represented by RESULTS and contents of BP. Physical address =
EA + SS.
MOV CS:RESULTS[BP],AX Same as the above instruction, but physical
address = EA + CS because of the segment override prefix CS.
PUSH‐PUSH Source
The PUSH instruction decrements the stack pointer by 2 and copies a
word from a specified source to the location in the stack segment where the stack
pointer then points. The source of the word can be a general- purpose register, a
segment register, or memory. The stack segment register and the stack pointer
must be initialized before this instruction can be used. PUSH can be used to save
data on the stack so that it will not be destroyed by a procedure. It can also be
used to put data on the stack so that a procedure can access it there as needed.
No flags are affected by this instruction.
9
Chapter 2 Assembly Language Programming
EXAMPLES:
PUSH BX Decrement SP by 2, copy BX to stack
PUSH DS Decrement SP by 2, copy DS to stack
PUSH AL Illegal, must push a word
PUSH TABLE [BX] Decrement SP by 2, copy word from memory in DS
at EA = TABLE + [BX] to stack
POP‐POP Destination
The POP instruction copies a word from the stack location pointed to by
the stack pointer to a destination specified in the Instruction. The destination can
be a general-purpose register, a segment register, or a memory location. The data
in the stack is not changed. After the word is copied to the specified destination,
the stack pointer is automatically incremented by 2 to point to the next word on
the stack. No flags are affected by the POP instruction.
NOTE: POP CS is illegal.
EXAMPLES:
POP DX Copy a word from top of stack to DX Increment SP by 2
POP DS Copy a word from top of stack to DS Increment SP by 2
POP TABLE [BX] Copy a word from top of stack to memory in DS
with EA = TABLE +[BX]
XCHG‐XCHG Destination, Source
The XCHG instruction exchanges the contents of a register with the
contents of another register or the contents of a register with the contents of a
memory locations). The XCHG cannot directly exchange the contents of two
memory locations. A memory location can be specified as the source or as the
destination by any of the 24 addressing modes. The source and destination must
both be words, or they must both be bytes. The segment registers cannot be used
in this instruction. No flags are affected by this instruction.
EXAMPLES:
XCHG AX,DX Exchange word in AX with word in DX
XCHG BL,CH Exchange byte in BL with byte in CH
XCHG AL,PRICES [BX] Exchange byte in AL with byte in memory at
EA = PRICES [BX] in DS
XLAT/XLATB‐Translate a Byte in AL
The XLATB instruction is used to translate a byte from one code to
another code. The instruction replaces a byte in the AL register with a byte
pointed to by BX in a lookup table in memory. Before the XLATB instruction can
be executed, the lookup table containing the values for the new code must be put
in memory, and the offset of the starting address of the lookup table must be
loaded in BX. The code byte to be translated is put in AL. To point to the desired
byte in the lookup table, the XLATB instruction adds the byte in AL to the offset
10
Chapter 2 Assembly Language Programming
of the start of the table in BX. It then copies the byte from the address pointed to
by (BX + AL) back into AL. XLATB changes no flags.
EXAMPLE:
8086 routine to convert ASCII code byte to EBCDIC equivalent.
ASCII code byte is in AL at start. EBCDIC code in AL at end.
MOV BX,OFFSET EBCDIC_TABLE
Point BX at start of EBCDIC table in DS
XLATB Replace ASCII in AL with EBCDIC from table
The XLATB instruction can be used to convert any code of 8 bits or less to any
other code of 8 bits or less.
IN‐Copy Data from a Port‐‐IN Accumulator, Port
The IN instruction will copy data from a port to the AL or AX register. If
an 8-bit port is read, the data will go to AL. If a 16-bit port is read, the data will
go to AX. The IN instruction has two possible formats, fixed port and variable
port.
For the fixed-port type, the 8-bit address of a port is specified directly in
the instruction.
EXAMPLES:
IN AL, 0C8H Input a byte from port 0C8H to AL
IN AX,34H Input a word from port 34H to AX
A_TO_D EQU 4AH
IN AX,A_TO_D Input a word from port 4AH to AX
For the variable-port-type IN instruction, the port address is loaded into
the DX register before the IN instruction. Since DX is a 16-bit register, the port
address can be any number between 0000H and FFFFH. Therefore, up to 65,536
ports are addressable in this mode.
MOV DX, 0FF78H Initialize DX to point to port
IN AL, DX Input a byte from 8-bit port 0FF78H to AL
IN AX, DX Input a word from 16-bit port 0FF78H to AX
The variable-port IN instruction has the advantage that the port address can be
computed or dynamically determined in the program.
OUT‐Output a Byte or Word to a Port‐‐OUT Port, Accumulator AL or AX
The OUT instruction copies a byte from AL or a word from AX to the
specified port. The OUT instruction has two possible forms, fixed port and
variable port.
For the fixed-port form, the 8-bit port address is specified directly in the
instruction. With this form, any one of 256 possible ports can be addressed.
EXAMPLES:
OUT 3BH,AL Copy the contents of AL to port 3BH
11
Chapter 2 Assembly Language Programming
OUT 2CH,AX Copy the contents of AX to port 2CH For the variable-port form
of the OUT instruction, the contents of AL or AX will be copied to the port at an
address contained in DX. Therefore, the DX register must always be loaded with
the desired port address before this form of the OUT instruction is used. The
advantage of the variable-port form of addressing is described in the discussion
of the IN instruction. The OUT instruction does not affect any flags.
MOV DX, 0FFF8H Load desired port address in DX
OUT DX, AL Copy contents of AL to port FFF8H
OUT DX, AX Copy contents of AX to port FFF8H
LEA‐Load Effective Address‐LEA Register, Source
This instruction determines the offset of the variable or memory location
named as the source and puts this offset in the indicated 16-bit register. LEA
changes no flags.
EXAMPLES:
LEA BX, PRICES Load BX with offset of PRICES in DS
LEA BP, SS: STACK_TOP Load BP with offset of STACK_TOP in SS
LEA CX, [BX][DI] Load CX with EA = (BX) + (DI)
Assume PRICES is an array of bytes in a segment called ARRAYS. The
instruction LEA BX, PRICES will load the displacement of the first element of
PRICES directly into BX. The instruction MOV AL,[BX] can then be used to bring
an element from the array into AL.
LDS‐Load Register and DS with Words from Memory‐LDS Register, Memory
Address of First Word
This instruction copies a word from two memory locations into the
register specified in the instruction. It then copies a word from the next two
memory locations into the DS register. LDS is useful for pointing SI and DS at the
start of a string before using one of the string instructions, LDS affects no flags.
EXAMPLES:
LDS BX, [4326] Copy contents of memory at displacement 4326H in DS to
BL, contents of 4327H to BH. Copy contents at displacement of 4328H and 4329H
in DS to DS register.
LDS SI, STRING_POINTER Copy contents of memory at displacements
STRING_POINTER and STRING_POINTER+1 in DS to SI register. Copy
contents of memory at displacements STRING_POINTER+2 and STRING
POINTER+3 In DS to DS register. DS:SI now points at start of desired string.
LES-Load Register and ES with Words from Memory-LES Register, Memory Address of
First Word
This instruction loads new values into the specified register and into the
ES register from four successive memory locations. The word from the first two
12
Chapter 2 Assembly Language Programming
memory locations is copied into the specified register, and the word from the
next two memory locations is copied into the ES register. LES can be used to
point DI and ES at the start of a string before a string instruction is executed. LES
affects no flags.
EXAMPLES:
LES BX, [789AH] Contents of memory at displacements 789AH and 789BH In
DS copied to BX. Contents of memory at displacements 789CH and 789DH in DS
copied to ES register.
LES DI, [BX] Copy contents of memory at offset [BX] and offset [BX+1] in
DS to DI register. Copy contents of memory at offsets [BX + 2] and [BX + 3] to ES
register.
SAHF‐Copy AH Register to Low Byte of Flag Register
The lower byte of the 8086 flag register corresponds exactly to the 8085
flag byte. SAHF replaces this 8085 equivalent flag byte with a byte from the AH
register. SAHF is used with the POP AX instruction to simulate the 8085 POP
PSW instruction. As described under the heading LAHF, an 8085 PUSH PSW
instruction will be translated to an LAHF--PUSH AX sequence to run on an 8086.
An 8085 POP PSW instruction will be translated to a POP AX--SAHF sequence to
run on an 8086. SAHF changes the flags in the lower byte of the flag register.
PUSHF‐Push Flag Register on the Stack
This instruction decrements the stack pointer by 2 and copies the word In
the flag register to the memory locations pointed to by the stack pointer. The
stack segment register is not affected. No flags are changed.
POPF‐Pop Word from Top of Stack to Flag Register
This instruction copies a word from the two memory locations at the top
of the stack to the flag register and increments the stack pointer by 2. The stack
segment register and the word on the stack are not affected. AU flags are
affected.
13
Chapter 2 Assembly Language Programming
Addition of unsigned numbers
CL = 01110011 = 115 decimal
+ BL = 01001111 = 79 decimal
ADD CL, BL Result in CL = 11000010= 194 decimal
Addition of signed numbers
CL = 01110011 = + 115 decimal
+ BL = 0 100 1111 + 79 decimal
ADD CL, BL Result in CL= 11000010 =62 decimal - incorrect because
result too large to fit in 7 bits.
CF = 0 No carry out of bit 7.
PF = 0 Result has odd parity.
AF = 1 Carry was produced out of bit 3.
ZF = 0 Result in destination was not 0.
SF = 1 Copies most significant bit of result; indicates negative result if we are
adding signed numbers.
14
Chapter 2 Assembly Language Programming
OF= 1 Set to indicate that the result of the addition was too large to fit in the
lower 7 bits of the destination used to represent the magnitude of a signed
number. In other words, the result was greater than + 127 decimal, so the result
overflowed into the sign bit position and incorrectly indicated that the result was
negative. If we are adding two signed 16-bit values, the OF will be set if the
magnitude of the result is too large to fit in the lower 15 bits of the destination.
NOTE: PF is meaningful only for an 8-bit result. AF is set only by a carry out of
bit 3. Therefore, the DAA instruction cannot be used after word additions to
convert the result to correct BCD.
INC-increment-INC Destination
The INC instruction adds1to a specified register or to a memory location
specified in any one of the 24 ways. AF, OF, PF, SF, and ZF are affected
(updated) by this instruction. Note that the carry flag (CF) is not affected. This
means that if an 8-bit destination containing FFH or a 16-bit destination
containing FFFFH is incremented, the result will be all 0's with no carry.
EXAMPLES:
INC BL Add 1 to contents of BL register
INC CX Add1to contents of CX register
INC BYTE PTR [ BX] Increment byte in data segment at offset contained in
BX. The BYTE PTR directive is necessary to tell the assembler to put in the right
code to indicate that a byte in memory, rather than a word, is to be incremented.
The instruction essentially says, "Increment the byte pointed to by the contents of
BX."
INC WORD PTR [BX] Increment the word at offset of [BX] and [BX + 1] in
the data segment. In other words, increment the word in memory pointed to by
BX.
INC MAX_TEMPERATURE Increment byte or word named MAX-
TEMPERATURE in data segment. Increment byte if MAX_TEMPERATURE
declared with DB. Increment word if MAX-TEMPERATURE declared with DW.
15
Chapter 2 Assembly Language Programming
AAA‐ASCII Adjust for Addition
Numerical data coming into a computer from a terminal is usually in
ASCII code. In this code, the numbers 0 to 9 are represented by the ASCII codes
30H to 39H. The 8086 allows we to add the ASCII codes for two decimal digits
without masking off the "3" in the upper nibble of each. After the addition, the
AAA Instruction is used to make sure the result is the correct unpacked BCD; A
simple numerical example will show how this works.
EXAMPLE:
Assume AL = 0 0 1 1 0 1 0 1, ASCII 5
BL = 0 0 1 1 1 0 0 1, ASCII 9
ADDAL,BL Result: AL= 0 1 1 0 1 1 1 0 = 6EH,which is incorrect BCD
AAA Now AL = 00000100, unpacked BCD 4.
CF = 1 indicates answer is 14 decimal
NOTE: OR AL with 30H to get 34H, the ASCII code for 4, if we want to send the
result back to a CRT terminal. The 1 in the carry flag can be rotated into the low
nibble of a register, ORed with 30H to give the ASCII code for 1, and then sent to
the terminal.
The AAA instruction works only on the AL register. The AAA instruction
updates AF and CF, but OF, PF, SF, and ZF are left undefined.
DAA‐Decimal Adjust AL after BCD Addition
This instruction is used to make sure the result of adding two packed BCD
numbers is adjusted to be a legal BCD number. The result of the addition must
be in AL for DAA to work correctly. If the lower nibble in AL after an addition is
greater than 9 or AF was set by the addition, then the DAA instruction will add 6
to the lower nibble in AL. If the result in the upper nibble of AL is now greater
than 9 or if the carry flag was set by the addition or correction, then the DAA
instruction will add 60H to AL.
EXAMPLES:
AL = 0101 1001 = 59 BCD ; BL = 0011 0101 = 35 BCD
ADD AL, BL AL = 1000 1110 = 8EH
DAA Add 01 10 because 1110 > 9 AL = 1001 0100 = 94 BCD
AL = 1000 1000 = 88 BCD BL = 0100 1001 = 49 BCD
ADD AL, BL AL = 1101 0001, AF=1
DAA Add 0110 because AF =1, AL = 11101 0111 = D7H
1101 > 9 so add 0110 0000
AL = 0011 0111= 37 BCD, CF =1
The DAA instruction updates AF, CF, PF, and ZF. OF is undefined after a DAA
instruction.
16
Chapter 2 Assembly Language Programming
17
Chapter 2 Assembly Language Programming
DEC‐Decrement Destination Register or Memory‐DEC Destination
This Instruction subtracts 1 from the destination word or byte. The
destination can be a register or a memory location specified by any one of the 24
addressing modes. AF, OF, PF, SF, and ZF are updated, but CF is not affected.
This means that if an 8-bit destination containing 00H or a 16-bit destination
containing 0000H is decremented, the result will be FFH or FFFFH with no carry
(borrow).
DEC CL Subtract 1 from contents of CL register
DEC BP Subtract 1 from contents of BP register
DEC BYTE PTR [BX] Subtract 1 from byte at offset [BX]in DS. The BYTE PTR
directive is necessary to tell the assembler to put in the correct code for
decrementing a byte in memory, rather than decrementing a word. The
instruction essentially says, "Decrement the byte in memory pointed to by the
offset in BX. "
DEC WORD PTR [BP] Subtract 1 from a word at offset [BP] in SS. The WORD
PTR directive tells the assembler to put in the code for decrementing a word
pointed to by the contents of BP. An offset in BP will be added to the SS register
contents to produce the physical address.
DEC TOMATO_CAN_COUNT, Subtract 1 from byte or word named
TOMATO_CAN_COUNT in DS. If TOMATO_CAN_COUNT was declared with
a DB, then the assembler will code this instruction to decrement a byte. If
TOMATO-CAN-COUNT was declared with a DW, then the assembler will code
this instruction to decrement a word.
NEG‐Form 2's Complement‐NEG Destination
This Instruction replaces the number in a destination with the 2's
complement of that number. The destination can be a register or a memory
location specified by any one of the 24-addressing modes. This instruction forms
18
Chapter 2 Assembly Language Programming
the 2's complement by subtracting the original word or byte in the indicated
destination from zero. As shown in some of the following examples, the NEG
Instruction is useful for changing the sign of a signed word or byte. An attempt
to NEG a byte location containing - 128 or a word location containing - 32,768
will produce no change in the destination contents because the maximum
positive signed number in 8 bits is + 127 and the maximum positive signed
number In 16 bits is + 32,767. OF will be set to Indicate that the operation could
not be done. The NEG instruction updates AF, CF, SF, PF, ZF, and OF.
EXAMPLES:
NEG AL Replace number in AL with its 2's complement
NEG BX Replace word in BX with its 2's complement
NEG BYTE PTR [BX] Replace byte at offset [BX] in DS with its 2's
complement
NEG WORD PTR [BP] Replace word at offset [BP] in SS with its 2's
complement
CMP‐Compare Byte or Word‐CMP Destination, Source
This instruction compares a byte from the specified source with a byte
from the specified destination, or a word from the specified source with a word
from the specified destination. The source can be an immediate number, a
register, or a memory location specified by one of the 24 addressing modes. The
destination can be a register or a memory location. However, the source and the
destination cannot both be memory locations in the same instruction. The
comparison is actually done by subtracting the source byte or word from the
destination byte or word. The source and the destination are not changed, but
the flags are set to indicate the results of the comparison. AF, OF, SF, ZF, PF, and
CF are updated by the CMP instruction. For the instruction 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
EXAMPLES:
CMP AL, 01H Compare immediate number01H with byte In AL
CMP BH, CL Compare byte in CL with byte in BH
CMP CX, TEMP_MIN Compare word in DS at displacement TEMP_MIN
with word in CX
CMP TEMP-MAX, CX Compare CX with word in DS at displacement TEMP-
MAX
CMP PRICES [BX], 49H Compare immediate 49H with byte at offset [BX] in
array PRICES
19
Chapter 2 Assembly Language Programming
NOTE: The Compare instructions are often used with the Conditional Jump
instructions. Having the Compare instructions formatted the way they are makes this use
very easy to understand.
AAS‐‐ASCII Adjust for Subtraction
Numerical data coming into a computer from a terminal is usually in
ASCII code. In this code the numbers 0 to 9 are represented by the ASCII codes
30H to 39H. The 8086 allows us to subtract the ASCII codes for two decimal
digits without masking the "3" in the upper nibble of each. The AAS instruction
is then used to make sure the result is the correct unpacked BCD. Some simple
numerical examples will show how this works.
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 and CF = 0
AAS Result: AL = 00000100 = BCD 04 and CF = 0
no borrow required
ASCII 5-ASCII 9 (5-9)
Assume AL = 00110101 = 35H ASCII 5
and BL = 0011 1001 = 39H = ASCII 9
SUB AL, BL Result: AL = 11111100 = - 4 in 2s complement and CF =1
AAS Result: AL = 00000100 = BCD 04 and CF = 1, borrow needed
The AAS instruction leaves the correct unpacked BCD result in the low
nibble of AL and resets the upper nibble of AIL to all 0's. If we want to send the
result back to a CRT terminal, we can OR AL with 30H to produce the correct
ASCII code for the result. If multiple-digit numbers are being subtracted, the CF
can be taken into account by using the SBB instruction when subtracting the next
digits.
The AAS instruction works only on the AL register. It updates AF and CF, but
OF, PF, SF, and ZF are left undefined.
DAS‐Decimal Adjust after BCD Subtraction
This instruction is used after subtracting two packed BCD numbers to
make sure the result is correct packed BCD. The result of the subtraction must be
in AL for DAS to work correctly. If the lower nibble in AL after a subtraction is
greater than 9 or the AF was set by the subtraction, then the DAS instruction will
subtract 6 from the lower nibble of AL. If the result in the upper nibble is now
greater than 9 or if the carry flag was set, the DAS instruction will subtract 60
from AL.
EXAMPLES:
AL 1000 0110 86 BCD ; BH 0101 0111 57 BCD
20
Chapter 2 Assembly Language Programming
21
Chapter 2 Assembly Language Programming
Multiplying a signed byte by a signed word
MOV CX, MULTIPLIER Load signed word in CX
MOV AL, MULTIPLICAND Load signed byte in AL
CBW Extend sign of AL into AH
IMUL CX Result in DX and AX
69 x 14
AL = 01000101 = 69 decimal, BL = 00001110 = 14 decimal
IMUL BL AX = 03C6H = + 966 decimal
MSB = 0, positive result magnitude in true form.
SF = 0, CF,OF = 1
22
Chapter 2 Assembly Language Programming
AAM‐BCD Adjust after Multiply
Before we can multiply two ASCII digits, we must first mask the upper 4
bits of each. This leaves unpacked BCD (one BCD digit per byte) in each byte.
After the two unpacked BCD digits are multiplied, the AAM instruction is used
to adjust the product to two unpacked BCD digits In AX.
AAM works only after the multiplication of two un- packed BCD bytes,
and it works only on an operand in AL. AAM updates PF, SF, and ZF, but AF,
CF, and OF are left undefined.
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 = 0405H,
which is unpacked BCD for 45.
If ASCII codes for the result are desired, use next instruction
OR AX, 3030H Put 3 in upper nibble of each byte.
AX = 0011 0100 0011 0101 = 3435H, which is ASCII code for 45
23
Chapter 2 Assembly Language Programming
location specified by any one of the 24 addressing modes. If the divisor does not
divide an integral number of times into the dividend the quotient is truncated,
not rounded. The example below will illustrate this. All flags are undefined after
a DIV instruction.
If we want to divide a byte by a byte, we must first put the dividend byte
In AL and fill AH with all 0's. The SUB AH, AH instruction is a quick way to do
this. Likewise, if we want to divide a word by a word, put the dividend word in
AX and fill DX with all 0's. The SUB DX, DX instruction does this quickly.
EXAMPLES:
DIV BL Divide word in AX by byte in BL. Quotient in AL, remainder in AH
DIV CX Divide doubleword in DX and AX by word in CX.
Quotient in AX, remainder in DX.
DIV SCALE [BX] AX/(byte at effective address SCALE[BX]), if SCALE[BX] is of
type byte or (DX and AX)/(word at effective address SCALE
[BX]) if SCALE[BX] is of type word
DIV BH AX = 37D7H = 14,295 decimal BH = 97H = 151 decimal
AX/BH, AL = quotient = 5EH = 94 decimal
AH = remainder = 65H = 101 decimal
Since the remainder is greater than half of the divisor, the actual quotient is
closer to 5FH than to the 5EH produced. However, as indicated before, the
quotient is always truncated to the next lower integer rather than rounded to the
closest integer. If we want to round the quotient, we can compare the remainder
with (divisor/2) and add 1 to the quotient if the remainder is greater than
(divtsor/2).
IDIV‐Divide by Signed Byte or Word‐IDIV Source
This instruction is used to divide a signed word by a signed byte, or to
divide a signed doubleword (32 bits) by a signed word.
When dividing a signed word by a signed byte, the word must be in the
AX register. The divisor can be in an 8-bit register or a memory location. After
the division, AL will contain the signed result (quotient), and AH will contain the
signed remainder. The sign of the remainder will be the same as the sign of the
dividend. If an attempt is made to divide by 0, the quotient is greater than 127
(7FH), or the quotient is less than -127 (81 H), the 8086 will automatically do a
type 0 Interrupt. For the 80186, 80286, etc. this range is - 128 to + 127.
When dividing a signed doubleword by a signed word, the most
significant word of the dividend (numerator) must be in the DX register, and the
least significant word of the dividend must be in the AX register. The divisor can
be in any other 16-bit register or memory location. After the division, AX will
contain a signed 16-bit quotient, and DX will contain a signed 16-bit remainder.
The sign of the remainder will be the same as the sign of the dividend. Again, if
an attempt is made to divide by 0, the quotient is greater than +32,767 (7FFFH),
24
Chapter 2 Assembly Language Programming
or the quotient is less than - 32,767 (800 1 H), the 8086 will automatically do a
type 0 interrupt. For the 80186, 80286, etc., this range is - 32,768 to + 32,767.
If the divisor does not divide evenly into the dividend, the quotient will
be truncated, not rounded. An example, below illustrates this. All flags are
undefined after an IDIV.
If we want to divide a signed byte by a signed byte, we must first put the
dividend byte in AL and fill AH with copies of the sign bit from AL. In other
words, if AL is positive (sign bit = 0), then AH should be filled with 0's. If AL is
negative (sign bit = 1), then AH should be filled with 1's. The 8086 Convert Byte
to Word instruction, CBW, does this by copying the sign bit of AL to all the bits
of AH. AH is then said to contain the “sign extension of AL." Likewise, if we
want to divide a signed word by a signed word, we must put the dividend word
in AX and extend the sign of AX to all the bits of DX. The 8086 Convert Word to
Doubleword instruction, CWD, will copy the sign bit of AX to all the bits of DX.
EXAMPLES:
IDIV BL Signed word in AX/signed byte in BL
IDIV BP Signed doubleword in. DX and AX/signed word in BP
IDIV BYTE PTR [BX] AX/byte at offset [BX] in DS
MOV AL, DIVIDEND Position byte dividend
CBW Extend sign of AL into AH
IDIV DMSOR Divide by byte divisor
EXAMPLES
A signed word divided by a signed byte
AX = 00000011 10101011 03ABH = 39 decimal
BL 11010011 = D3H = - 2DH = - 45 decimal
IDIV BL Quotient: AL = ECH = -14H = -20 decimal
Remainder: AH = 27H + 39 decimal
NOTE: The quotient is negative because positive was divided -by negative. The
remainder has same sign as dividend (positive).
AL 1101 1010 - 26 H = - 38 decimal
CH 0000 0011 + 3H = + 3 decimal
CBW Extend sign of AL through AH,
AX= 11111111 11011010
IDIV CH Divide AX by CH
AL 11110100 = -0CH = -12 decimal
AH 11111110 = 2H = -2 decimal
Although the quotient is actually closer to 13 (12.666667) than to 12, the 8086
truncate it to 12 rather than rounding it to 13. If we want to round the quotient,
we can compare the magnitude of the remainder with (divisor/2) and add 1 to
25
Chapter 2 Assembly Language Programming
the quotient if the remainder is greater than (divisor/2). Note that the sign of the
remainder is the same as the sign of the dividend (negative). All flags are
undefined after IDIV.
26
Chapter 2 Assembly Language Programming
NOT‐Invert Each Bit of Operand‐NOT Destination
The NOT instruction inverts each bit (forms the I's complement) of the
byte or word at the specified destination. The destination can be a register or a
memory location specified by any one of the 24 addressing modes. No flags are
affected by the NOT Instruction.
EXAMPLES:
NOT BX Complement contents of BX register
NOT BYTE PTR [BX] Complement memory byte at offset IBXI in data segment
AND‐AND Corresponding Bits of Two Operands‐‐AND Destination, Source
This instruction ANDs each bit in a source byte or word with the same
number bit in a destination byte or word. The result is put in the specified
destination. The contents of the specified source will not be changed. The result
for each bit position will follow the truth table for a two-input AND gate. In
other words, a bit in the specified destination will be a 1 only if that bit is a 1 in
both the source and the destination operands. Therefore, a bit can be masked
(reset) by ANDing it with 0.
The source operand can be an immediate number, the contents of a
register, or the contents of a memory location specified by one of the 24
addressing modes. The destination can be a register or a memory location. The
source and the destination cannot both be memory locations in the same
instruction. CF and OF are both 0 after AND. PF, SF, and ZF are updated by
AND. AF is undefined. Note that PF has meaning only for an 8-bit operand.
EXAMPLES :
AND CX, [SI] AND word in DS at offset [SI] with word in CX register
Result in CX register
AND BH, CL AND byte in CL with byte in BH Result in BH
AND BX, 00FFH AND word in BX with immediate 00FFH.
Masks upper byte, leaves lower byte unchanged
BX = 10110011 01011110
AND BX, 00FFH Mask out upper 8 bits of BX
Result: BX = 00000000 01011110 CF, OF, PF, SF, ZF = 0
27
Chapter 2 Assembly Language Programming
destination. The contents of the specified source will not be changed. The result
for each bit will follow the truth table for a two-input OR gate. In other words, a
bit in the destination will become a 1 if that bit is a 1 in the source operand or
that bit is a1in the original destination operand. Therefore. a bit in the destination
operand can be set to a 1 by simply ORing that bit with a 1 in the same bit of the
source operand.
The source operand can be an immediate number, the contents of a
register, or the contents of a memory location specified by one of the 24
addressing modes. The destination can be a register or a memory location. The
source and the destination cannot both be memory locations in the same
instruction. CF and OF are both 0 after OR. PF, SF, and ZF are updated by the OR
instruction. AF is undefined after OR. Note that PF has meaning only for the
lower 8 bits of a result.
EXAMPLES (SYNTAX):
OR AH, CL CL ORed with AH, result in AH. CL not changed
OR BP, SI SI ORed with BP, result in BP. SI not changed
OR SI, BP BP ORed with SI, result in SI. BP not changed
OR BL, 80H BL ORed with immediate 80H. Set MSB of BL to a 1
OR CX, TABLE [BX][SI] CX ORed with word from effective address
TABLE[BX][SI] in data segment.
OR CX, 0FF00H CX = 00111101 10100101 ,OR CX with immediate
FF00H, Result in CX = 11111111 10100101
CF=0,OF=0,PF= 1,SF= 1,ZF=0.
28
Chapter 2 Assembly Language Programming
TEST‐AND Operands to Update Flags‐TEST Destination, Source
This instruction ANDs the contents of a source byte or word with the
contents of the specified destination word. Flags are updated, but neither
operand is changed. The TEST instruction is often used to set flags before a
Conditional Jump instruction.
The source operand can be an immediate number, the contents of a
register, or the contents of a memory location specified by one of the 24
addressing modes. The destination operand can be in a register or in a memory
location. The source and the destination cannot both be memory locations in an
instruction. CF and OF are both 0's after TEST. PF, SF, and ZF will be updated to
show the results of the ANDing. PF has meaning only for the lower 8 bits of the
destination. AF will be undefined.
EXAMPLES:
TEST AL, BH AND BH with AL, no result stored. Update PF, SF, ZF
TEST CX, 0001 H AND CX with immediate number 0001H, no result
stored. Update PF, SF, ZF
TEST BP, [BX][DI] AND word at offset [BX][DI] in DS with word in BP.
no result stored. Update PF, SF, and ZF
AGAIN: IN AL, 2AH Read port with strobe connected to LSB
TEST AL, 01H AND immediate 01 H with AL to test if LBB of
AL is 1 or 0 ZF = 1 if LSB of result is 0 No
result stored
JZ AGAIN Read port again if LSB = 0
AL = 01010001
TEST AL, 80H AND immediate 80H with AL to test if MSB of AL is 1 or 0
ZF = 1if MSB of AL = 0. AL = 01010001 (unchanged)
PF = 0, SF = 0, ZF = 1, because ANDing produced 00.
29
Chapter 2 Assembly Language Programming
SHR‐Shift Operand Bits Right, Put Zero in MSB(s)‐SHR Destination, Count
This instruction shifts each bit in the specified destination some number of
bit positions to the right. As a bit is shifted right out of the MSB position, a 0 is
put in its place. The bit shifted out of the LSB position goes to CF. In the case of a
multiple-bit shift, CF will contain the bit most recently shifted in from the LSB.
Bits shifted into CF previously will be lost. See the following diagram.
0 MSB LSB CF
The destination operand can be a byte or a word in a register or in a
memory location specified by any one of the 24 addressing modes.
30
Chapter 2 Assembly Language Programming
SAR‐Shift Operand Bits Right, New MSB Old MSB‐SAR Destination, Count
This instruction shifts each bit in the specified destination some number of
bit positions to the right. As a bit is shifted out of the MSB position, a copy of the
old MSB is put in the MSB position. In other words, the sign bit is copied into the
MSB. The LSB will be shifted into CF. In the case of multiple bit shifts; CF will
contain the bit most recently shifted in from the LSB. Bits shifted into CF
previously will be lost. See the following diagram.
MSB MSB LSB CF
The destination operand can be a byte or a word. It can be in a register or
in a memory location specified by any one of the 24 addressing modes.
If the desired number of shifts is one, this can be specified by putting a 1
in the count position of the instruction. For shifts of more than one bit position,
the desired number of shifts is loaded into the CL register, and CL is put in the
count position of the instruction.
The flags are affected as follows: CF contains the bit most recently shifted in from
the LSB. For a count of one, OF will be a 1 if the two MSBs are not the same. After
a multibit SAR, OF will be 0. SF and ZF will be updated to show the condition of
the destination. PF will have meaning only for an 8-bit destination. AF will be
undefined after SAR.
EXAMPLES:
SAR DI, 1 Shift word in DI one bit position right, new MSB = old MSB
MOV CL, 02H Load desired number of shifts in CL
31
Chapter 2 Assembly Language Programming
SAR WORD PTR [BP], CL Shift word at offset [BP] in stack segment right
two bit positions. Two MSBs are now copies of
original MSB
2.3.4.3. Rotate Instructions
ROL‐Rotate All Bits of Operand Left, MSB to LSB‐ROL Destination, Count
This instruction rotates all the bits in a specified word or byte to the left
some number of bit positions. The operation can be thought of as circular,
because the data bit rotated out of the MSB is circled back into the LSB. The data
bit rotated out of the MSB is also copied to CF during ROL. In the case of
multiple bit rotates, CF will contain a copy of the bit most recently moved out of
the MSB. See the following diagram.
CF MSB LSB
32
Chapter 2 Assembly Language Programming
ROR‐Rotate All Bits of Operand Right, LSB to MSB‐ROR Destination, Count
This Instruction rotates all the bits of the specified word or byte some
number of bit positions to the right. The operation is described as a rotate rather
than a shift because the bit moved out of the LSB is rotated around into the MSB.
To help visualize the operation, think of the operand as a loop with the LSB
connected around to the MSB. The data bit moved out of the LSB is also copied to
CF during ROR. See the following diagram. In the case of multiple-bit rotates, CF
will contain a copy of the bit most recently moved out of the LSB.
MSB LSB CF
RCL‐Rotate Operand Around to the Left through CF‐RCL Destination, Count
This instruction rotates all the bits in a specified word or byte some
number of bit Positions to the left. The operation is circular because the MSB of
the operand is rotated into the carry flag and the bit in the carry flag is rotated
around into the LSB of the operand. See the following diagram.
CF
MSB LSB
The "C" In the middle of the mnemonic should help us remember that CF
is in the rotated loop and help distinguish this instruction from the ROL
instruction. For multi-bit rotates, CF will contain the bit most recently rotated out
of the MSB.
The destination operand can be in a register or in a memory location,
specified by any one of the 24 addressing modes. If we want to rotate the
operand one bit position, we can specify this by putting a 1 in the count position
of the instruction. To rotate more than one bit position, load the desired number
into the CL register and put "CL" in the count position of the Instruction.
RCL affects only CF and OF. After RCL, CF will contain the bit most
recently rotated out of the MSB. OF will be a 1 after a single-bit RCL if the MSB
was changed by the rotate. OF is undefined after a multi-bit rotate.
The RCL instruction is a handy way to move CF into the LSB of a register
or memory location to save it after addition or subtraction.
33
Chapter 2 Assembly Language Programming
EXAMPLES:
RCL DX, 1 Word in DX 1 bit left, MSB to CF, CF to LSB
MOV CL,4 Load number of bit positions to rotate into CL
RCL SUM [BX], CL Rotate byte or word at effective address SUM[BX], 4
bits left Original bit 4 now in CF, original CF now in
bit 3
RCL BH, 1 CF = 0, BH = 1011 0011 ,Result: BH= 0110 0110
CF = 1, OF =1 because MSB changed CF = 1,
AX= 0001 1111 1010 1001
MOV CL, 2 Load CL for rotating 2 bit positions
RCL AX, CL Result: CF = 0, OF undefined AX = 0111 1110 1010 0110
CF
MSB LSB
The "C" in the middle of the mnemonic should help us remember that CF
is in the rotated loop and should help distinguish this instruction from the ROR
instruction. For multi-bit rotates, CF will contain the bit most recently rotated out
of the LSB.
The destination operand can be in a register or in a memory location
specified by any one of the 24 addressing modes. If we want to rotate the
operand one bit position, we can specify this by putting a1in the count position
of the instruction. To rotate more than one bit position, load the desired number
into the CL register and put "CL" In the count position of the instruction.
RCR affects only CF and OF. After RCR, CF will contain the bit most
recently rotated out of the MSB. OF will be a1after a single-bit RCR if the MSB
was changed by the rotate. OF will be undefined after multibit rotates.
EXAMPLES:
RCR BX, 1 Word in BX right1bit CF to MSB, LSB to CF
MOV CL, 04H Load CL for rotating 4 bit positions
RCR BYTE PTR [BX] Rotate byte at offset [BX] in DS 4 bit positions right
CF = original bit 3. Bit 4 original CF
34
Chapter 2 Assembly Language Programming
35
Chapter 2 Assembly Language Programming
LODS/LODSB/LODSW Load String Byte into AL or Load String Word into AX
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. If the direction flag is
cleared (0), SI will automatically be incremented to point to the next element of
the string. For a string of bytes, SI will be incremented by 1. For a string of
words, SI will be incremented by 2 if the direction flag (DF) is set (1), SI will be
automatically decremented to point to the next string element. For a byte string,
SI will be decremented by 1, and for a word string, SI will be decremented by 2.
LODS affects no flags.
EXAMPLE:
CLD Clear direction flag so SI is auto incremented.
MOV SI, OFFSET SOURCE-STRING Point SI at start of string
LODS SOURCE-STRING Copy byte or word from string to AL or AX
NOTE: The assembler uses the name of the string to determine whether the
string is of type byte or type word. Instead of using the string name to do this,
we can use the mnemonic LODSB to tell the assembler that the string is of type
byte or the mnemonic LODSW to tell the assembler that the string is of type
word.
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. STOS does not affect any flags.
EXAMPLES:
Point DI at start of destination string
MOV DI, OFFGET TARGET_STRING
STOS TARGET_STRING Assembler uses string name to determine whether
string is of type byte or type word. If byte string, then string byte replaced with
contents of AL. If word string, then string word replaced with contents of AX
Point DI at start of destination string.
MOV DI,OFFSET TARGET_STRING
36
Chapter 2 Assembly Language Programming
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. SCAS affects AF, CF, OF, PF, SF, and ZF, but it
does not change either the operand in AL (AX) or the operand in the string. This
instruction is often used with a repeat prefix to find the first occurrence of a
specified byte or word In a string.
EXAMPLE:
Scan a text string of 80 characters for a carriage return, ODH. Put offset of string
into DI.
MOV DI,OFFSET TEXT_STRING
MOV AL,ODH Byte to be scanned for into AL
MOV CX,80 CX used as element counter
CLD Clear DF so DI autoincrements
REPNE SCAS TEXT_STRING Compare byte in string with byte in AL
NOTE: Scanning is repeated as long as the bytes are not equal and the end of the
string has not been reached. If a carriage return ODH is found, ZF = 1, and DI
will point at the next byte after the carriage return in the string. If a carriage
return is not found, then CX = 0 and ZF = 0. The assembler uses the name of the
string to determine whether the string is of type byte or type word. Instead of
using the name, we can tell the assembler the type of string directly by using the
mnemonic SCASB for a byte string and SCASW for a word string.
37
Chapter 2 Assembly Language Programming
EXAMPLE:
MOV SI, OFFSET FIRST-STRING Point SI at source string
MOV DI, OFFSET SECOND-STRING Point DI at destination string
CLD DF cleared, so SI and DI will auto
increment after compare
MOV CX,100 Put number of string elements in CX
REPE CMPSB Repeat the comparison of string bytes
until end of string or until compared
bytes are not equal
NOTE: CX functions as a counter, which, the REPE prefix will cause to be
decremented after each compare. The B attached to CMPS tells the assembler that
the strings are of type byte. If we want to tell the assembler that the strings are of
type word, write the instruction as CMPSW. The REPE CMPSW instruction will
cause the pointers in SI and DI to be incremented by 2 after each compare if the
direction flag is cleared or decremented by 2 if the direction flag is set.
EXAMPLE:
38
Chapter 2 Assembly Language Programming
REPE CMPSB, Compare string bytes until end of string or until string bytes not
equal. See the discussion of the CMPS instruction for a more detailed example of
the use of REPE.
REPNE and REPNZ are also two mnemonics for the same prefix. They stand for
Repeat if Not Equal and Repeat if Not Zero, respectively. REPNE or REPNZ is
often used with the Scan String instruction. RFPNE or REPNZ will cause the
string instruction to be repeated until the compared bytes or words are equal (ZF
= 1) or until CX = 0 (end of string).
REPNE SCASW, Scan a string of words until a word in the string matches the
word in AX or until all of the string has been scanned. See the discussion of
SCAS for a more detailed example of the use of this prefix
The string instruction used with the prefix determines which flags are
affected. See the individual instructions for this information.
NOTE: Interrupts should be disabled when multiple prefixes are used, such as
LOCK, segment override, and REP with string instructions on the 8086/8088.
This is because, during an interrupt response, the 8086 can remember only the
prefix just before the string instruction. The 80186, 80286, etc., will remember all
the prefixes and start up correctly after an interrupt during a string instruction.
CALL--Call a Procedure
The CALL instruction is used to transfer execution to a subprogram or
procedure. There are two basic types of calls, near and far. A near call is a call to
a procedure, which is in the same code segment as the CALL instruction. When
the 8086 executes a near CALL instruction, it decrements the stack pointer by 2
and copies the offset of the next instruction after the CALL onto the stack. This
offset saved on the stack is referred to as the return address, because this is the
address that execution will return 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. A RFT instruction at the end of the procedure will
return execution to the instruction after the call by copying the offset saved on
the stack back to IP.
A far call is a call to a procedure, which is in a different segment from the
one that contains the CALL instruction. When the 8086 executes a far call, it
decrements the stack pointer by 2 and copies the contents of the CS register to the
stack. It then decrements the stack pointer by 2 again and copies the offset of the
instruction after the CALL instruction to the stack. Finally, it loads CS with the
segment base of the segment, which contains the procedure, and loads IP with
the offset of the first instruction of the procedure in that segment. A RFT
instruction at the end of the procedure will return execution to the next
39
Chapter 2 Assembly Language Programming
Instruction after the CALL by restoring the saved values of CS and IP from the
stack.
EXAMPLES:
CALL MULTO A direct within-segment (near or intra- segment) call. MULTO
is the name of the procedure. The assembler determines the displacement of
MULTO from the instruction after the CALL and codes this displacement in as
part of the instruction.
CALL BX An indirect within-segment near or intrasegment call. BX
contains the offset of the first instruction of the procedure. Replaces contents of
IP with contents of register BX.
CALL WORD PTR [BX] An indirect within-segment near or intrasegment
call. Offset of first instruction of procedure is in two memory addresses in DS.
Replaces contents of IP with contents of word memory location in DS pointed to
by BX.
CALL SMART_DIVIDE A direct call to another segment-far or intersegment
call. SMART_DIVIDE is the name of the procedure. The procedure must be
declared far with SMART_DIVIDE PROC FAR at its start. The assembler will
determine the code segment base for the segment, which contains the procedure
and the offset of the start of the procedure. It will put these values in as part of
the instruction code.
CALL DWORD PTR [BX] An indirect call to another segment-far or
intersegment call. New values for CS and IP are fetched from four memory
locations in DS. The new value for CS is fetched from [BX] and [BX + 1]; the new
IP is fetched from [BX + 2] and [BX + 3].
RET‐Return Execution from Procedure to Calling Program
The RET instruction will return execution from a procedure to the next
instruction after the CALL instruction which was used to CALL the procedure. If
the procedure is a near procedure (in the same code segment as the CALL
instruction), then the return will be done by replacing the instruction pointer
with a word from the top of the stack. The word from the top of the stack is the
offset of the next instruction after the CALL. This offset was pushed onto the
stack as part of the operation of the CALL instruction. The stack pointer will be
incremented by 2 after the return address is popped off the stack.
If the procedure is a far procedure (in a different code segment from the
CALL instruction which calls it), then the instruction pointer will be replaced by
the word at the top of the stack. This word is the offset part of the return address
put there by the CALL instruction. The stack pointer will then be incremented
by 2. The code segment register is then replaced with a word from the new top of
the stack. This word is the segment base part of the return address that was
pushed on to the stack by a far call operation. After the code segment word is
popped off the stack, the stack pointer is again incremented by 2.
40
Chapter 2 Assembly Language Programming
A RET instruction can be followed by a number, for example, RET 6. In this case
the stack pointer will be incremented by an additional six addresses after the IP
or the IP and CS are popped off the stack. This form is used to increment the
stack pointer over parameters passed to the procedure on the stack. The RET
instruction affects no flags.
41
Chapter 2 Assembly Language Programming
The destination label for the jump must be in the range of - 128 bytes to + 127
bytes from the address of the Instruction after the JA. JA/JNBE affects no flags.
CMP AX, 4371H Compare by subtracting 4371H from AX
JA RUN-PRFSS Jump to label RUN-PRFSS if AX above 4371H
CMP AX, 4371H Compare (AX - 4371H)
JNBE RUN_PRESS JUMP to label RUN_PRESS if AX not below or equal
to 4371H
ADD AL, BL Add two bytes. If the result within acceptable range
JNC OK Continue
JAE/INB/INC‐‐Jump if Above or Equal/jump if Not Below/jump if No Carry
These three mnemonics represent the same instruction. The terms above
and below are used when referring to the magnitude of unsigned numbers. The
number 0111 is above the number 0010. If, after a compare or some other
instruction, which affects flags, the carry flag is 0, this instruction will cause
execution to jump to a label given in the instruction. If CF is 1, the instruction
will have no effect on program execution. The destination label for the jump
must be in the range of - 128 bytes to +I 127 bytes from the address of the
instruction after the JAE. JAE/JNB/JNC affects no flags.
EXAMPLES:
CMP AX, 4371H Compare (AX - 4371H)
JAE RUN_PRESS Jump to label RUN_PRESS if AX above or equal to 4371H
CMP AX, 4371H Compare (AX - 4371H)
JNB RUN_PRESS Jump to label RUN-PRESS if AX not below 4371H
ADD AL, BL Add two bytes. If result within JNC OK
acceptable range, continue.
JB/JC/JNAE‐jump if Below/jump if Carry/jump if Not Above or Equal
These three mnemonics represent the same instruction. The terms above
and below are used when referring to the magnitude of unsigned numbers. The
number 0111 is above the number 0010. If, after a compare or some other
instruction, which affects flags, the carry flag is a 1, this instruction will cause
execution to jump to a label given in the instruction. If CF is 0, the instruction
will have no effect on program execution. The destination label for the jump
must be in the range of - 128 bytes to + 127 bytes from the address of the
instruction after the JB. JB/JC/JNAE affects no flags.
EXAMPLES:
CMP AX, 4371H Compare (AX - 4371H)
JB RUN_PRFSS Jump to label RLIN-PRFSS if AX below 4371H
ADD BX, CX Add two words and jump
JC ERROR_FIX to label ERROR-FIX if CF = 1
42
Chapter 2 Assembly Language Programming
JBE/JNA‐Jump if Below or Equal/Jump if Not Above
These two mnemonics represent the same Instruction. The terms above
and below are used when referring to the magnitude of unsigned numbers. The
number 0111 is above the number 0010. If, after a compare or some other
instruction, which affects flags, the zero, flag or the carry flag is 1, this instruction
will cause execution to jump to a label given in the instruction. If CF and ZF are
both 0, the instruction will have no effect on program execution. The destination
label for the jump must be in the range of - 128 bytes to + 127 bytes from the
address of the instruction after the JBE. JBE/JNA affects no flags.
EXAMPLES:
CMP AX, 4371H Compare (AX - 4371H)
JBE RUN-PRESS Jump to label RUN-PRESS if AX below or equal to 4371H
CMPAX, 4371H Compare (AX - 4371H)
JNA RUN-PRESS Jump to label RUN-PRESS if AX not above 4371H
JE/JZ‐Jump if Equal/jump if Zero
These two mnemonics represent the same Instruction. If the zero flag is
set, then this instruction will cause execution to jump to a label given In the
instruction. If the zero flag is not 1, then execution will simply go on to the next
instruction after JE or JZ. The destination label for the JE/JZ instruction must be
in the range of - 128 to + 127 bytes from the address of the instruction after the
JE/JZ instruction. JE/JZ affects no flags.
EXAMPLES:
NXT:CMP BX,DX Compare (BX-DX)
JE DONE Jump to DONE if BX = DX
SUB BX,AX Else subtract AX
INC CX Increment counter
JMP NXT Check again
DONE:MOV AX,CX Copy count to AX
IN AL,8FH Read data from port 8FH
SUB AL,30H Subtract minimum value
JZ START_MACHINE Jump to label if result of subtraction was 0
JG/JNLE‐Jump if Greater/Jump if Not Less Than or Equal
These two mnemonics represent the same Instruction. The terms greater
and less are used to refer to the relationship of two signed numbers. Greater
means more positive. The number 0000 0111 is greater than the number 1110
1010, because in signed notation the second number is negative. This instruction
is usually used after a Compare instruction. The Instruction will cause a jump to
43
Chapter 2 Assembly Language Programming
a label given in the Instruction if the zero flag is 0 and the carry flag is the same
as the overflow flag. The destination label must be in the range of - 128 bytes to +
127 bytes from the address of the instruction after the JG/JNLE instruction. If the
jump is not taken, execution simply goes on to the next instruction after the JG or
JNLE instruction. JG/JNLE affects no flags.
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
44
Chapter 2 Assembly Language Programming
on to the next instruction after JNE or JNZ. The destination label for the
JNE/JNZ instruction must be in the range of - 128 to + 127 bytes from the
address of the instruction after the JNE/JNZ instruction. JNE/JNZ affects no
flags.
JNP/JPO‐Jump if No Parity/Jump if Parity Odd
If the number of 1's left in the lower 8 bits of a data word after an instruction
which affects the parity flag is odd, then the parity flag will be 0. The JNP/JPO
instruction will cause execution to jump to a specified destination address if the
parity flag is 0. The destination address must be in the range of - 128 bytes to +
127 bytes from the address of the instruction after the JNP/ JPO instruction.
JO‐jump if Overflow
The JO instruction will cause the 8086 to jump to a destination given in the
instruction if the overflow flag is set. The overflow flag will be set if the
magnitude of the result produced by some signed arithmetic operation is too
large to fit in the destination register or memory location. The destination for the
JO instruction must be in the range of - 128 bytes to + 127 bytes from the address
of the instruction after the JO instruction. If the overflow flag is not set, execution
will simply continue with the next instruction after JO. JO affects no flags.
JP/JPE‐Jump if Parity/jump if Parity Even
If the number of 1's left In the lower 8 bits of a data word after an
instruction which affects the parity flag is even, then the parity flag will be set. If
the parity flag is set, the JP/JPE instruction will cause execution to jump to a
specified destination address. If the parity flag is 0, execution will simply
continue on to the instruction after the JP/JPE instruction. The destination
address must be in the range of - 128 bytes to + 127 bytes from the address of the
instruction after the JP/JPE instruction. The JP/JPE instruction affects no flags.
45
Chapter 2 Assembly Language Programming
LOOPE/LOOPZ‐‐‐Loop While CX ≠ 0 and ZF = 1
LOOPE and LOOPZ are two mnemonics for the same instruction. This
instruction is used to repeat a group of instructions some number of times or
until the zero flags becomes 0. The number of times the instruction sequence is to
be repeated is loaded into CX. Each time the LOOP instruction executes, CX is
automatically decremented by 1. If CX = 0 and ZF = 1, execution will jump to a
destination specified by a label in the instruction. If CX = 0 after the
autodecrement or if ZF = 0, execution will simply go on to the next instruction
after LOOPE/LOOPZ. In other words, the two ways to exit the loop are CX = 0
or ZF = 0. 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 LOOPE/LOOPZ
instruction. LOOPE/LOOPZ affects no flags.
EXAMPLE:
MOV BX, OFFSET ARRAY Point BX to just
DEC BX before start of array
MOV CX, 100 Put number of array elements in CX
NEXT: INC BX Point to next element in array
46
Chapter 2 Assembly Language Programming
LOOPNE/LOOPNZ‐‐‐Loop While CX ≠ 0 and ZF = 0
LOOPNE and LOOPNZ are two mnemonics for the same instruction. This
instruction is used to repeat a group of instructions some number of times or
until the zero flag becomes a 1. The number of times the instruction sequence is
to be repeated is loaded into the count register CX. Each time the
LOOPNE/LOOPNZ instruction executes, CX is automatically decremented by 1.
If CX ≠ 0 and ZF = 0, execution will jump to a destination specified by a label in
the Instruction. If CX = 0 after the autodecrement or if ZF = 1, execution will
simply go on to the next instruction after LOOPNE/LOOPNZ.
JCXZ‐Jump if the CX Register Is Zero
This instruction will cause a jump to a label given in the Instruction if the
CX register contains all 0's. If CX does not contain all 0's, execution will simply
proceed to the next instruction. Note that this instruction does not look at the
zero flag when it decides whether to jump or not. The destination label for this
instruction must be in the range of - 128 to + 127 bytes from the address of the
instruction after the JCXZ instruction. JCXZ affects no flags.
EXAMPLE:
JCXZ SKIP_LOOP If CX = 0, skip the process
NXT: SUB [BX], 07H Subtract 7 from data value
INC BX Point to next value
LOOP NXT Loop until CX = 0
SKIP_LOOP: Next instruction
47
Chapter 2 Assembly Language Programming
2. Decrement the stack pointer by 2 and push the contents of CS onto the
stack.
3. Decrement the stack pointer by 2 and push the offset of the next
instruction after the TNT number Instruction on the stack.
4. Get a new value for IP from an absolute memory address of 4 times the
type specified in the instruction. For an INT 8 Instruction, for example, the
new IP will be read from address 00020H.
5. Get a new value for CS from an absolute memory address of 4 times the
type specified in the instruction plus 2. For an INT 8 instruction, for
example, the new value of CS will be read from address 00022H.
6. Reset both IF and TF. Other flags are not affected.
EXAMPLES:
INT 35 New IP from 0008CH, new CS from 0008EH
INT 3 This is a special form, which has the single-byte code of CCH.
Many systems use this as a breakpoint Instruction. New IP from 0000CH, new CS
from 0000EH.
INTO‐interrupt on Overflow
If the overflow flag (OF) is set, this instruction will cause the 8086 to do an
indirect far call to a procedure we write to handle the overflow condition. Before
doing the call, the 8086 will:
1. Decrement the stack pointer by 2 and push the flags onto the stack.
2. Decrement the stack pointer, by 2 and push CS onto the stack.
3. Decrement the stack pointer by 2 and push the offset of the next instruction
after the INTO instruction onto the stack
4. Reset TF and IF. Other flags are not affected. To do the call, the 8086 will read
a new value for IP from address 00010H and a new value of CS from address
00012H.
EXAMPLE:
INTO Call interrupt procedure if OF = 1
IRET-interrupt Return
When the 8086 responds to an interrupt signal or to an interrupt
instruction, it pushes the flags, the current value of CS, and the current value of
IP onto the stack. It then loads CS and IP with the starting address of the
procedure, which we write for the response to that Interrupt. The IRET
Instruction is used at the end of the interrupt service procedure to return
execution to the interrupted program. To do this return, the 8086 copies the
saved value of IP from the stack to IP, the stored value of CS from the stack to
CS, and the stored value of the flags back to the flag register. Flags will have the
values they had before the interrupt, so any flag settings from the procedure will
be lost unless they are specifically saved in some way.
48
Chapter 2 Assembly Language Programming
NOTE: The RET instruction should not normally be used to return from interrupt
procedures because it does not copy the flags from the stack back to the flag
register.
2.3.7 Process Control Instructions
2.3.7.1. Flag Set/clear Instructions
CLD‐Clear Direction Flag
This instruction resets the direction flag to 0. No other flags are affected. If
the direction flag is reset, SI and DI will automatically be incremented when one
of the string instructions, such as MOVS, CMPS, or SCAS, executes.
EXAMPLE:
CLD Clear direction flag so that string pointers auto increment after each string
operation
STI-Set Interrupt Flag (IF)
Setting the interrupt flag to a 1 enables the INTR interrupt input of the
8086. The instruction will not take effect until after the next instruction after STI.
When the INTR input is enabled, an interrupt signal on this input will then cause
the 8086 to interrupt program execution, push the return address and flags on
the stack, and execute an interrupt service procedure. An IRET instruction at the
end of the interrupt service procedure will restore the flags, which were pushed
onto the stack, and return execution to the interrupted program. STI does, not
affect any other flags.
49
Chapter 2 Assembly Language Programming
CLI‐Clear Interrupt Flag
This instruction resets the interrupt flag to 0. No other flags are affected. If
the interrupt flag is reset, the 8086 will not respond to an interrupt signal on its
INTR input. The CLI instruction, however, has no effect on the non-maskable
interrupt input, NMI.
WAIT‐Wait for Test Signal or interrupt Signal
When this instruction executes, the 8086 enters an idle condition in which
it is doing no processing. The 8086 will stay in this idle state until the 8086 TEST
input pin is made low or until an interrupt signal is received on the INTR or the
NMI interrupt input pins. If a valid interrupt occurs while the 8086 is in this idle
state, the 8086 will return to the idle state after the interrupt service procedure
executes. It returns to the idle state because the address of the WAIT instruction
is the address pushed on the stack when the 8086 responds to the interrupt
request. WAIT affects no flags. The WAIT instruction is used to synchronize the
8086 with external hardware such as the 8087-math coprocessor.
ESC‐Escape
This instruction is used to pass instructions to a coprocessor, such as the
8087-math coprocessor, which shares the address and data bus with an 8086.
Instructions for the coprocessor are represented by a 6-bit code embedded in the
escape instruction. As the 8086 fetches instruction bytes, the coprocessor also
catches these bytes from the data bus and puts them in its queue. However, the
coprocessor treats all the normal 8086 instructions as NOPs. When the 8086
fetches an ESC instruction, the coprocessor decodes the instruction and carries
out the action specified by the 6-bit code specified in the instruction. In most
cases the 8086 treats the ESC instruction as a NOP. In some cases the 8086 will
access a data item in memory for the coprocessor.
LOCK‐Assert Bus Lock Signal
Many microcomputer systems contain several microprocessors. Each
microprocessor has its own local buses and memory. The individual
microprocessors are connected together by a system bus so that each can access
system resources such as disk drives or memory. Each microprocessor takes
control of the system bus only when it needs to access some system resource. The
50
Chapter 2 Assembly Language Programming
LOCK prefix allows a microprocessor to make sure that another processor does
not take control of the system bus while it is in the middle of a critical instruction
which uses the system bus. The LOCK prefix is put in front of the critical
instruction. When an instruction with a LOCK prefix executes, the 8086 will
assert its bus lock signal output. This signal is connected to an external bus
controller device, which then prevents any other processor from taking over the
system bus. LOCK affects no flags.
EXAMPLE:
LOCK XCHG SEMAPHORE, AL The XCHG instruction requires two bus
accesses. The LOCK prefix prevents another processor from taking control of the
system bus between the two accesses.
NOP‐Perform No Operation
This instruction simply uses up three clock cycles and Increments the instruction
pointer to point to the next instruction. NOP affects no flags. The NOP
instruction can be used to increase the delay of a delay loop. When hand coding
a NOP can also be used to hold a place in a program for an instruction that will
be added later.
ASSUME
The ASSUME directive is used to tell the assembler the name of the logical
segment it should use for a specified segment. The statement ASSUME CS:
CODE, for example, tells the assembler that the instructions for a program are in
a logical segment named CODE. The statement ASSUME DS: DATA tells the
assembler that for any program instruction, which refers to the data segment, it
should use the logical segment called DATA. If, for example, the assembler reads
the statement MOV AX,[BX] after it reads this ASSUME, it will know that the
memory location referred to by [BX] is in the logical segment DATA. We must
tell the assembler what to assume for any segment we use in a program. If we
use a stack in our program, we must tell the assembler the name of the logical
segment we have set up as a stack with a statement such as ASSUME SS:
STACK_HERE. For a program with string Instructions which use DI, the
51
Chapter 2 Assembly Language Programming
assembler must be told what to assume for the extra segment with a statement
such as ASSUME ES: STRING_DESTINATION.
DB--Define Byte
The DB directive is used to declare a byte-type variable, or to set aside one
or more storage locations of type byte in memory. The statement
CURRENT_TEMPERATURE DB 42H, for example, tells the assembler to reserve
1 byte of memory for a variable named CURRENT_TEMPERATURE and to put
the value 42H in that memory location when the program is loaded into RAM to
be run.
Here are a few more examples of DB statements.
PRICES DB 49H, 98H, 29H Declare array of 3 bytes named-
PRICES and initialize 3 bytes as shown.
NAME_HERE DB 'YOHANNAS' ; Declare array of 6 bytes and initialize
with ASCII codes for letters in YOHANNAS.
TEMPERATURE-STORAGE DB 100 DUP(?) ; Set aside 100 bytes of storage in
memory and give it the name TEMPERATURE_STORAGE, but leave the 100
bytes uninitialized. Program instructions will load values into these locations.
PRESSURE_STORAGE DB 20H DUP(0) ; Set aside 20H bytes of storage in
memory, give it the name PRESSURE_STORAGE, and put 0 in a 20H locations.
DD‐Define Doubleword
The DD directive is used to declare a variable of type doubleword or to,
reserve memory locations, which can be accessed as type doubleword. The
statement ARRAY_POINTER DD 25629261H, for example, will define a
doubleword named ARRAY_POINTER and initialize the doubleword with the
specified value when the pro- gram is loaded into memory to be run. The low
word, 9261H, will be put in memory at a lower address than the high word. A
declaration of this type is often used with the LES or LDS instruction. The
Instruction LES DI, ARRAY_POINTER, for example, will copy the low word of
this doubleword, 9261H, into the DI register and the high word of the
doubleword, 2562H, into the extra segment register.
DQ‐define Quadword
This directive is used to tell the assembler to declare a variable 4 words in
length or to reserve 4 words of storage in memory.
The statement BIG_NUMBER DQ 243598740192A92BH, for example, will
declare a variable named BIG_NUMBER and initialize the 4 words set aside with
the specified number when the program is loaded into memory to be run. The
statement STORAGE DQ 100 DUP (0) reserves 100 quadwords of storage and
initializes them all to 0 when the program is loaded into memory to be run.
52
Chapter 2 Assembly Language Programming
DT‐Define Ten Bytes
DT is used to tell the assembler to define a variable, which is 10 bytes in
length, or to reserve 10 bytes of storage in memory.
The statement PACKED_BCD DT 11223344556677889900 will declare an
array named PACKED_BCD, which is 10 bytes in length. It will initialize the 10
bytes with the values 11223344556677889900 when the program is loaded into
memory to be run. This directive is often used when declaring data arrays for the
8087 math coprocessor. The statement RESULTS DT 20H DUP (0) will declare an
array of 20H blocks of 10 bytes each and initialize all 320 bytes to 00 when the
program is loaded into memory to be run.
DW‐Define Word
The DW directive is used to tell the assembler to define a variable of type
word or to reserve storage locations of type word in memory. The statement
MULTIPLIER DW 437AH, for example, declares a variable of type word named
MULTIPLIER. The statement also tells the assembler that the variable
MULTIPLIER should be initialized with the value 437AH when the program is
loaded into memory to be run.
Few more examples of DW statements.
THREE_LITFLE_WORDS DW 1234H,3456H,5678H ;Declare array of 3 words
and initialize with specified values.
STORAGE DW 100 DUP(0) ; Reserve an array of 100 words of memory and
initialize all100 words with 0000. Array is named STORAGE.
STORAGEDW 100 DUP(?) ; Reserve 100 words of storage in memory and
give it the name STORAGE, but leave the words uninitialized.
END‐End Program
The END directive is put 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 statements after an END directive, so we should make sure to use only one
END directive at the very end of our program module. A carriage return is
required after the END directive.
ENDP‐End Procedure
This directive is used along with the name of the procedure to indicate the
end of a procedure to the assembler. This directive, together with the procedure
directive, PROC, is used to "bracket" a procedure. Here's an example.
SQUARE-ROOT PROC Start of procedure
//Procedure instruction statements
SQUARE-ROOT ENDP End of procedure
53
Chapter 2 Assembly Language Programming
ENDS‐End Segment
This directive is used with the name of a segment to indicate the end of
that logical segment. ENDS is used with the SEGMENT directive to "bracket" a
logical segment containing instructions or data. Here's an example.
CODE SEGMENT Start of logical segment containing code
//Instruction statements
CODE ENDS End of segment named CODE
EQU‐Equate
EQU is used to give a name to some value or symbol. Each, time the
assembler finds the given name in the program, it will replace the name with the
value or symbol we equated with that name. Suppose, for example, we write the
statement CORRECTION_FACTOR EQU 03H at the start of our program, and
later in the program we write the instruction statement ADD AL,
CORRECTION_ACTOR. When it codes this instruction statement, the assembler
will code it as if we had written the instruction ADD AL, 03H. The advantage of
using EQU in this manner is that if CORRFCTION_FACTOR is used 27 times in
a program, and we want to change the value, all we have to do is change the
EQU statement and reassemble the program. The assembler will automatically
put in the new value each time it finds the name CORRECTION_FACTOR. If we
had used 03H Instead of the EQU approach, then we would have had to try to
find all 27 instructions and change them ourself. Here are some more examples.
54
Chapter 2 Assembly Language Programming
55
Chapter 2 Assembly Language Programming
Note: A Large Set of Programs using debugger and the Macro-Assemblers will be
discussed in the class. Some sample programs are listed below.
DATA SEGMENT
ONE DW 1234H
TWO DW 5678H
RESULT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX
MOV AX,ONE
ADD AX,TWO
MOV RESULT,AX
INT 3
CODE ENDS
END START
56
Chapter 2 Assembly Language Programming
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
57
Chapter 2 Assembly Language Programming
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
end start
58