Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

Chapter III

Interrupts
• An interrupt is a hardware/software -initiated procedure
that interrupts the microprocessor whatever program is
currently executing.
•Figure shows a time line that indicates a typist typing
data on a keyboard, a printer removing data from the
memory, and the main program execution.
•The main program that is interrupted for each keystroke
and each character that is to print on the printer.
•Note that the keyboard interrupt service procedure,
called by the keyboard interrupt, will take little time to
execute.
•Similarly, printer interrupt service procedure , called by
the printer interrupt will take little time to execute.
 
Interrupts can be
(i) Software interrupts i.e., software instructions
(ii) Hardware interrupts i.e., those are triggered by different
hardware.
 
(i) Software interrupts: 
syntax:
INT value
where value can be a number between 0 to 255 (or 0 to 0FFh),
Each interrupt may have sub-functions. 
Each interrupt may have up to 256 sub-functions (so we
get 256 * 256 = 65536 functions).
In general AH register is used to specify a sub-function before
calling interrupt., but sometimes other registers may be in use.
A list of supported interrupts with descriptions:
INT 10h / AH = 00h - set video mode.
input:
AL = desired video mode.
The video modes supported are:
00h - Text mode 4025, 16 colors, 8 pages.
03h - Text mode 8025, 16 colors, 8 pages.
INT 10h / AH = 02h - set cursor position.
input:
DH = row.
DL = column.
BH = page number (0..7).
INT 12h - get memory size.
return:
AX = kilobytes of contiguous memory starting at absolute address 00000h, this call
returns the contents of the word at 0040h:0013h.
INT 20h - exit to operating system.
INT 21h / AH=09h - output of a string at DS:DX.
INT 21h / AH=0Ah - input of a string to DS:DX,
first byte is buffer size, second byte is number of chars actually read.
INT 21h / AH=4Ch - exit to operating system.
INT 21h / AH=01h - read character from standard input, with echo, result is stored in AL.
INT 21h / AH=02h - write character to standard output, DL = character to write, after
execution AL = DL.
Hardware interrupts
The microprocessor has two hardware interrupt inputs
1. Non-Maskable Interrupt (NMI) and
2. Interrupt Request (INTR).
1. Non-Maskable Interrupt (NMI)
NMI input is a type 2 interrupt because NMI is internally decoded.
It is an edge-triggered (0-to-1 transition) interrupt request.
It is used for parity errors and other major system faults, such as power
failures.
Power failures are easily detected by monitoring the AC power line and
causing an NMI interrupt whenever AC power drops out. In
response to this type of interrupt, the microprocessor stores all of the
internal register in a battery-backed-up memory or an EEPROM.
 
 
(vi)The 64-bit registers are designated as RAX, RBX, etc.
In addition to the renaming of the registers for 64-bit
width, there are also additional 64-bit registers that
are called R8 through R15. The 64-bit extensions are
more than 8 times in the Pentium 4 and the Core2.
(vii) Program-Invisible Registers
The global and local descriptor tables are found in the
memory system. In order to access and specify the
address of these tables, the 80286–Core2 contain
program-invisible registers.
(viii) Paging Registers
The paging unit is controlled by the contents of the
microprocessor’s control registers. These registers are
available to the 80386 through the Core2microprocessors.
(ix) The additional registers allowed software to be written more
efficiently.
1. Machine language :Machine language is constructed with 0’s
and 1’s using binary codes that are stored in the computer
memory system as groups of instructions called a program.
This programming procedure is called as machine level
programming.
 Disadvantages of machine level programming are as given:
1. The process is complicated and time consuming.
2. The chances of human errors are more while entering
the program byte-by-byte into the system.
3. Debugging a program at the machine level is more
difficult.
4. These programs are difficult to understand unless one
has a thorough technical knowledge of the processor
architecture and instruction set.
Advantage: The memory control is directly in the hands of
the programmer, so that, he/she may be able to manage
the memory of the system more efficiently
1. Assembly Language/Basic Instruction /Instruction Operands/
Assembling the code
Assembly Language: A program in the form of mnemonics of instructions
written is called Assembly Language.
'Assembler' is used to convert Assembly Language program along with the data
into its equivalent machine code or object code. These object code modules
may further be converted in executable code using the linker and loader
programs. This type of programming is called assembly level programming
(ALP).
 
The advantages of assembly language over machine language are as given:
1. The ALP is not as complicated as in m.l because the function of coding is
performed by an assembler.
2. The chances of error being committed are less.
3. As the mnemonics are purpose-suggestive the debugging is easier.
4. The constants and address locations can be labeled with suggestive labels.
5. The memory control is in the hands of users as in machine language.
6. The results may be stored in a more user-friendly form.
7. The flexibility of programming is more in ALP.
Basic Instruction Format:
The 8086 instruction sizes vary from one to six bytes. The Basic
Instruction Format is shown in Fig. below.
Here, the first 6 bits of the first byte are the binary opcode. The
opcode defines the operation to be carried out by the instruction.
The remaining 2 bits are:
(i) Register Direct bit (D) occupies one bit. It defines the direction of
the data flow i.e., whether the register operand in byte 2 is the
source or destination operand.
D=1 specifies that the register operand is the destination operand
(data flow to the register REG field from the R/M field)
D=0 indicates that the register is a source operand
(data flow to the R/M field from the REG field.)
(ii) Data size bit (W) defines whether the operation to be performed
is an 8 bit or 16 bit data
W=0 indicates 8 bit operation
W=1 indicates 16 bit operation
Basic Instruction Format
The second byte of the instruction usually identifies whether one
of the operands is in memory or whether both are registers.
This byte contains 3 fields. These are
the mode (MOD) field,
the register (REG) field and
the Register/Memory (R/M) field.

MOD (2 bits) Function


00 Memory mode with no displacement
01 Memory mode with 8-bit displacement
10 Memory mode with 16-bit displacement
11 Register mode (no displacement)
Register field occupies 3 bits. It defines the register for the
first operand which is specified as source or destination
by the D bit.
REG (3 bits)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
 
The R/M field occupies 3 bits. The R/M field along with the
MOD field defines the second operand as shown below.
MOD 11
R/M(3 bits)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
Effective Address Calculation

R/M Memory Mode (EA Calculation)


00 01 10
000 (BX)+(SI) (BX)+(SI)+d8 (BX)+(SI)+d16
001 (BX) +(DI) (BX)+(DI)+d8 (BX)+(DI)+d16
010 (BP)+(SI) (BP)+(SI)+d8 (BP)+(SI)+d16
011 (BP)+(DI) (BP)+(DI)+d8 (BP)+(DI)+d16
100 (SI) (SI) + d8 (SI) + d16
101 (DI) (DI) + d8 (DI) + d16
110 (BP) (BP) + d8 (BP) + d16
111 (BX) (BX) + d8 (BX) + d16
Table: Summary of all Addressing
Modes
MOD / Memory Mode (EA Calculation) Register Mode
R/M
00 01 10 W=0 W=1
000 (BX)+(SI) (BX)+(SI)+d8 (BX)+(SI)+d16 AL AX
001 (BX) + (DI) (BX)+(DI)+d8 (BX)+(DI)+d16 CL CX
010 (BP)+(SI) (BP)+(SI)+d8 (BP)+(SI)+d16 DL DX
011 (BP)+(DI) (BP)+(DI)+d8 (BP)+(DI)+d16 BL BX
100 (SI) (SI) + d8 (SI) + d16 AH SP
101 (DI) (DI) + d8 (DI) + d16 CH BP
110 BP (BP) + d8 (BP) + d16 DH SI
111 (BX) (BX) + d8 (BX) + d16 BH DI
In the above, encoding of the R/M field
depends on how the mode field is set. If
MOD=11 (register to register mode), this
R/M identifies the second register operand.
MOD selects memory mode, then R/M
indicates how the effective address of the
memory operand is to be calculated.
Bytes 3 through 6 of an instruction are
optional fields that normally contain the
displacement value of a memory operand
and / or the actual value of an immediate
constant operand.
Example 1: MOV CH, BL
This instruction transfers 8 bit content of BL into CH.
The 6 bit Opcode for this instruction is 1000102
D bit indicates whether the register specified by the REG field of
byte 2 is a source or destination operand.
D=0 indicates BL is a source operand.
W=0 indicates byte operation
In byte 2, since the second operand is a register, MOD field is
112.
Register (REG) field = 011 (BL)
The R/M field = 101 (CH)
Hence the machine code for MOV CH, BL is
10001000 11 011 101
Byte 1 Byte2
= 88DD
Example 2: SUB BX, (DI)
This instruction subtracts the 16 bit content of
memory location addressed by DI and DS from
BX.
The 6 bit Opcode for SUB is 0010102.
D=1 indicates that REG field of byte 2 is the
destination operand.
W=1 indicates 16 bit operation.
MOD = 00
REG = 011
R/M = 101
The machine code is 0010 1011 0001 1101
=2B1D16
Example 3: Code for MOV 1234 (BP), DX
Here we have to specify DX using REG field, the D bit must be
0, indicating the DX is the source register.
The REG field must be 010 to indicate DX register.
The W bit must be 1to indicate it is a word operation. 1234 [BP]
is specified using MOD value of 10 and
R/M value of 110 and a displacement of 1234H. The 4 byte code
for this instruction would be
Opcode D W MOD REG R/M LOB displacement. HOB displacement.
100010 0 1 10 010 110 34H 12H

=89
96 34
12H.
Example 4: Code for MOV DS : 2345 [BP], DX
Here we have to specify DX using REG field. The D bit must be 0, indicating
that DX is the source register.
The REG field must be 010 to indicate DX register.
The W bit must be 1 to indicate it is a word operation.
2345 [BP] is specified with MOD=10 and R/M =110 and displacement = 2345 H.
Whenever BP is used to generate the Effective Address (EA), the default
segment would be SS.
In this example, we want the segment register to be DS, we have to provide the
segment override prefix byte (SOP byte) to start with.
The SOP byte is 001 SR 110 where SR value is provided as per table shown
below.
 
SR Segment register
00 ES
01 CS
10 SS
11 DS
To specify DS register, the SOP byte would be 001 11 110 = 3E H. Thus the 5
byte code for this instruction would be

Byte 1 Byte 2 Byte 3 Byte 4 Byte 5


SOP Opcode D W MOD REG R/M LOB disp. HOB disp.
3EH 1000 10 0 1 10 010 110 45H 23H
=3E 89
96 45 23 H

Suppose we want to code MOV SS : 2345 (BP), DX.


This generates only a 4-byte code, without SOP
byte, as SS is already the default segment register
in this case.
Example 5:
Determine the instruction template and generate code for the instruction ADD
OFABE [BX][DI], DX.
Given that code for ADD instruction is 000000.
 
ADD 0FABE [BX] [DI], DX
Here we have to specify DX using REG field. The bit D is 0, indicating that DX
is the source register.
The REG field must be 010 to indicate DX register.
The W must be 1 to indicate it is a word operation.
FABE (BX + DI) is specified using MOD value of 10 and R/M value of 001
(from the summary table).
The 4 byte code for this instruction would be
Opcode D W MOD REG R/M LOB disp. HOB disp.
000000 0 1 10 010 001 BEH FAH

=01
91 BE
FAH
Example 6:
Give the instruction template and generate the code for the
instruction MOV AX, [BX]
(Code for MOV instruction is 100010)
 
AX destination register with D=1 and code for AX is 000 [BX]
is specified using 00 Mode and R/M value 111
It is a word operation. So
Opcode D W MOD REG R/M
100010 1 1 00 000 111

=8B 07H
 
Directives
Directives are statements that are not assembled into any real
machine code.
(1) INCLUDE : It is a compiler directive which tells the
compiler that to use functions defined in other file.
Syntax for a file declaration:
include 'file name' 
Eg. include 'emu8086.inc' 
We should use the  directive followed by a file name in
program.
(2) ORG: It is a compiler directive which tells the compiler that
the executable file will be loaded at the offset address specified
in the directive.
(3) .CODE: This statement or directive indicates the start of
the code segment.
(4) .STARTUP: This statement indicates the starting
instruction in the program.
(5) .EXIT: This statement causes the program to exit to DOS.
Variables 
Variable is a memory location.
Syntax for a variable declaration:
name DB value
name DW value
DB - stands for Define Byte.
DW - stands for Define Word.
name - can be any letter or digit combination, though it
should start with a letter.
value - can be any numeric value in any supported
numbering system (hexadecimal, binary, or decimal), or "?"
symbol for variables that are not initialized.
#MAKE_COM#
ORG 100h
 
MOV AL, var1
MOV BX, var2
 
RET ; stops the program.
 
VAR1 DB 7
var2 DW 1234h
The variables are replaced with actual
memory locations.
Constants 
To define constants EQU directive is used. After
definition of a constant its value cannot be
changed.
Syntax for a constant declaration:
name EQU < any expression >
For example:
k EQU 5

MOV AX, k

The above example is functionally identical to


code:
MOV AX, 5
(8) END: This directive is to stop the compiler. The END
statement indicates the end of the program file.
(9) DUP: Generates duplicates
(10) PROC: Starts a procedure
(11) ENDP: Ends a procedure
(12) MACRO: Designates the start of a MACRO sequence
(13) ENDM: Ends a MACRO sequence
(14) PTR: Designates a pointer
(15)BYTE: Indicates byte-sized as in BYTE PTR
(16)WORD: Indicates word-sized, as in WORD PTR
(17)NEAR: Defines a near pointer, as in NEAR PTR
(18)FAR: Defines a far pointer, as in FAR PTR
(19)OFFSET: Specifies an offset address
(20).DATA: Indicates the start of the data segment (models
only)
(21)ASSUME: Informs the assembler to name each segment (full
segments only)
 
Creating a program
It is much easier to program in a higher-level
language than in assembly language programming.
Also, using assembly makes a program very hard to
port to other platforms.
So, why to create a standalone assembly language
program?
1. Sometimes code written in assembly can be faster
and smaller than compiler generated code.
2. AP allows access to direct hardware features of the
system that might be difficult to use from a HLL.
3. Learning to program in assembly helps in deeper
understanding of how computers work.
4. Learning to ALP helps one understand better how
compilers and high-level languages like C work.
First program
The following shows a simple assembly program.

This example uses INT 10h/ sub-function 0Eh to type a "Hello!" message. This function
displays a character on the screen, advancing the cursor and scrolling the screen as necessary.
#MAKE_COM# ; instruct compiler to make COM file.
ORG 100h
MOV AH, 0Eh ; select INT 10h / 0Eh sub-function, receives an ASCII code of the
; character that will be printed in AL register.
MOV AL, 'H' ; ASCII code: 72
INT 10h ; print it!
MOV AL, 'e' ; ASCII code: 101
INT 10h ; print it!
MOV AL, 'l' ; ASCII code: 108
INT 10h ; print it!
MOV AL, 'l' ; ASCII code: 108
INT 10h ; print it!
MOV AL, 'o' ; ASCII code: 111
INT 10h ; print it!
MOV AL, '!' ; ASCII code: 33
INT 10h ; print it!
RET ; returns to operating system.
Compiler dependencies
Dependencies are mostly of two types:
a)Header files
b)Libraries
To use functions defined in other file we should use
the INCLUDE directive followed by a file name in
program. Compiler automatically searches for the file
in the same folder where the source file is located, and
if it cannot find the file there - it searches
in .inc folder. 
To use any of the functions in emu8086.inc use the
following line in the beginning of the source file:
include 'emu8086.inc' 

You might also like