Week 3
8086/8088 Instruction Set, Machine
Codes, and Addressing Modes
Software
• The sequence of commands used to tell a microcomputer what to do
is called a program,
• Each command in a program is called an instruction
• 8088 understands and performs operations for 117 basic
instructions
• The native language of the IBM PC is the machine language of the
8088
• A program written in machine language is referred to as machine
code
• In 8088 assembly language, each of the operations is described by
alphanumeric symbols instead of just 0s or 1s.
ADD AX, BX
Source operand
Opcode
Destination operand
2
Instructions
LABEL: INSTRUCTION ; COMMENT
Address identifier Does not generate any machine code
Ex. START: MOV AX,BX ; copy BX into AX
• There is a one-to-one relationship between assembly and machine
language instructions
• What is found is that a compiled machine code implementation of a
program written in a high-level language results in inefficient code
– More machine language instructions than an assembled version of an
equivalent handwritten assembly language program
• Two key benefits of assembly language programming
– It takes up less memory
– It executes much faster
3
Applications
• One of the most beneficial uses of assembly language programming
is real-time applications.
• Real time means the task required by the application must be
completed before any other input to the program that will alter its
operation can occur
• For example the device service routine which controls the operation
of the floppy disk drive is a good example that is usually written in
assembly language
• Assembly language not only good for controlling hardware devices
but also performing pure software operations
– searching through a large table of data for a special string of characters
– Code translation from ASCII to EBCDIC
– Table sort routines
– Mathematical routines
• Assembly language: perform real-time operations
• High-level languages: used to write those parts that are not time
critical
4
Converting Assembly Language Instructions to
Machine Code
• An instruction can be coded with 1 to 6 bytes
• Byte 1 contains three kinds of information
– Opcode field (6 bits) specifies the operation such as add, subtract, or
move
– Register Direction Bit (D bit)
• Tells the register operand in REG field in byte 2 is source or destination
operand
– 1: destination
– 0: source
– Data Size Bit (W bit)
• Specifies whether the operation will be performed on 8-bit or 16-bit data
– 0: 8 bits
– 1: 16 bits
• Byte 2 has two fields
– Mode field (MOD)
– Register field (REG)
– Register/memory field (R/M field)
5
Continued
• REG field is used to identify the register fro the first operand
REG W=0 W=1
000 AL AX
001 CL CX
010 DL DX
011 BL BX
100 AH SP
101 CH BP
110 DH SI
111 BH DI
6
Continued
• 2-bit MOD field and 3-bit R/M field together specify the second
operand
7
Examples
• MOV BL,AL
• Opcode for MOV = 100010
• We’ll encode AL so
– D = 0 (AL source operand)
• W bit = 0 (8-bits)
• MOD = 11 (register mode)
• REG = 000 (code for AL)
• R/M = 011
opcode D W MOD REG R/M
MOV AL,BL = 10001000 11000011 = 88 C3h
ADD AX,[SI] = 00000011 00000100 = 03 04 h
ADD [BX],[DI] + 1234h, AX = 00000001 10000001 __ __ h
= 01 81 34 12 h
8
Addressing Modes
• When the 8088 executes an instruction, it performs the specified
function on data
• These data, called operands,
– May be a part of the instruction
– May reside in one of the internal registers of the microprocessor
– May be stored at an address in memory
• Register Operand Addressing
– MOV AX, BX
• Immediate Operand Addressing Mode
– MOV AL,15h
• Memory Addressing Modes
– One must calculate the PA (physical address)
CS
SS BX SI 8 bit displacement
PA = DS : BP + DI + 16 bit displacement
ES 9
Immediate Addressing
• In immediate addressing, source operand is a constant
• Used to load information to any one of the registers
except for the segment registers and the flag register
– MOV AX,2550h
– MOV CX, 625 ; decimal 625
– MOV BL,40h
– Etc.
10
Direct Addressing Mode
MOV CX, [1234h]
11
Register Indirect Addressing Mode
MOV AX, [SI]
12
Based-Indexed Addressing Mode
MOV AH, [BX] [SI] + 1234h
13
Based-Indexed Addressing Mode
MOV BX, 0600h
(Based relative addressing mode)
MOV SI, 0010h ;4 x 4 = 16
(Indexed relative addressing mode)
MOV AL, [BX + SI + 3]
Based-indexed relative addressing mode)
14
Based relative Addressing Mode
• MOV CX,[BX]+10
– Physical Address : DS (shifted left) + BX + 10
• MOV AL,[BP]+5
– Physical Address: SS (shifted left) + BP + 5
15
Indexed relative Addressing Mode
• MOV DX,[SI]+5
– Physical Address : DS (shifted left) + SI + 5
• MOV CL,[DI]+20
– Physical Address: DS (shifted left) + DI + 20
16
Based Indexed Relative Addressing Mode
• MOV CX,[BX][DI]+8h
– Physical Address : DS (shifted left) + BX + DI + 8h
• MOV AH,[BP][SI]+29
– Physical Address: SS (shifted left) + BP + SI + 29
• Other notations possible
– MOV AH,[BP+SI+29]
or
– MOV AH, [SI+BP+29]
However
• MOV AX,[SI][DI]+displacement is illegal
17
Segment Override
Instruction Segment Default Segment
Used
MOV AX,CS:[BP] CS:BP SS:BP
MOV DX,SS:[SI] SS:SI DS:SI
MOV AX,DS:[BP] DS:BP SS:BP
MOV CX,ES:[BX]+12 ES:BX+12 DS:[BX]+12
MOV SS:[BX][DI]+12,AX SS:BX+DI+12 DS:BX+DI+12
18
The PTR Operator
• MOV AL,[20h] ; 8 bit data copied into AL
• MOV AX,[20h] ; 16 bit data copied into AX
• INC [20h] ; 8 bit or 16 bits incremented”?
• Byte or word or doubleword?
• To clarify we use the PTR operator
– INC BYTE PTR [20h]
– INC WORD PTR [20h]
– INC DWORD PTR [20h]
19
Example
Copy the contents of a block of memory (16 bytes) starting at
location 20100h to another block of memory starting at 20120h
MOV AX,2000h
MOV DS,AX
MOV SI, 100h
MOV DI, 120h
MOV CX, 10h
NXTPT: MOV, AH, [SI]
MOV [DI], AH
INC SI
INC DI
DEC CX
JNZ NXTPT
20
MS-DOS Functions and BIOS Calls
Application Command Processor
Programs COMMAND.COM
MS-DOS Kernel
MSDOS.SYS
BIOS
ROM plus IO.SYS
System Hardware
• BIOS is hardware specific
• BIOS is supplied by the computer manufacturer
• Resident portion which resides in ROM and nonresident portion IO.SYS
which provides a convenient way of adding new features to the BIOS
21
80x86 Interrupts
• An interrupt is an event that causes the processor to suspend its present
task and transfer control to a new program called the interrupt service
routine (ISR)
• There are three sources of interrupts
– Processor interrupts
– Software interrupts
– Hardware interrupts
• Each interrupt must supply a type number which is used by the processor
as a pointer to an interrupt vector table (IVT) to determine the address of
that interrupt’s service routine
• Each entry in the IVT points to the location of the corresponding ISR.
• Before transferring control to the ISR, the processor performs one very
important task
– It saves the current program address and flags on the stack
– Control then transfers to the ISR
– When the ISR finishes, it uses the instruction IRET to recover the flags and old
program address from the stack
• Many of the vectors in the IVT are reserved for the processor itself and
others have been reserved by MS-DOS for the BIOS and kernel.
– 10-1A are used by the BIOS
– 20 – 3F are used by the MS-DOS kernel 22
Interrupt Vector Table
23
Interrupts
• There are some extremely useful subroutines within BIOS or DOS
that are available to the user through the INT (Interrupt) instruction.
• The INT instruction is like a FAR call; when it is invoked
– It saves CS:IP and flags on the stack and goes to the subroutine
associated with that interrupt.
– Format:
• INT xx ; the interrupt number xx can be 00-FFH
– This gives a total of 256 interrupts
– Two of them are widely used: INT 10H and INT 21H
– Before the services of INT 10H and 21H is requested, certain registers
must have specific values in them, depending on the function being
requested.
24
INT 10H
• INT 10H subroutines are burned into the ROM BIOS of the 80x86-
based PC and are used to communicate with the computer’s screen
video.
•
00,00 00,79
12,39
24,00 24,79
Cursor Locations
• INT 10H Function 06
– AL = number of lines to scroll (with AL=00, window will be cleared)
– BH = attribute of blank rows
– CH, CL = upper row, left column
– DH, DL = lower row, right column
25
INT 10H
• INT 10H function 02; setting the cursor to a specific location
– Function AH = 02 will change the position of the cursor to any location.
– The desired cursor location is in DH = row, DL = column
• EX. Write a program that clears the screen and sets the cursor at the center
of the screen ; clearing the screen
MOV AX, 0600H ;scroll the entire page
MOV BH, 07 ; normal attribute (white on black)
MOV CX, 0000 ; upper left
MOV DX,184FH ; lower right
INT 10H
;setting the cursor at the center
MOV AH,02 ; set cursor option
MOV BH, 00 ; page 0
MOV DL, 39 ;
MOV DH, 12 ;
INT 10H
• INT 10H function 03; get current cursor position
MOV AH, 03
MOV BH, 00
INT 10H
– Registers DH and DL will have the current row and column positions and CX
provides info about the shape of the cursor.
26
INT 21H
• INT 21H is provided by DOS to be invoked to perform extremely
useful functions.
• INT 21H Option 09: Outputting a string of data to the monitor
– INT 21H can be used to send a set of ASCII data to the monitor.
– AH = 09; DX = offset address of the ASCII data to be displayed.
– INT 21H option 09 will display the ASCII data string pointed to by DX
until it encounters the dollar sign “$”.
DATA_ASC DB ‘The earth is one country’,’$’
MOV AH,09
MOV DX, OFFSET DATA_ASC
INT 21H
• INT 21H Option 02: Outputting a single character to the monitor
– DL is loaded with the character first
MOV AH 02
MOVE DL,’J’
INT 21H
27
INT 21H
• INT 21H Option 01: Inputting a single character with echo
– This function waits until a character is input from the keyboard, then
echoes it to the monitor. After the interrupt, the input character will be in
AL.
• INT 21H Option 0AH: Inputting a string of data from the keyboard
with echo
– AH = 0AH
– DX = offset address at which the address is stored
– First byte specifies the size of the buffer, second byte is the number of
characters
ORG 0010H
DATA1 DB 6,?,6 DUP(FF)
MOV AH, 00H
MOV DX, OFFSET DATA1
INT 21H
Ex. What happens if one enters USA and then <RETURN>
0010 0011 0012 0013 0014 0015 0016 0017
06 03 55 53 41 0D FF FF 28