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

COM212 NOTE

COM212 NOTE

Uploaded by

solomon isaac
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

COM212 NOTE

COM212 NOTE

Uploaded by

solomon isaac
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

COM 212 SYSTEM PROGRAMMING

1.1 ASSEMBLY LANGUAGE

Each personal computer has a microprocessor that manages the computer's arithmetical, logical and
control activities.
Each family of processors has its own set of instructions for handling various operations like getting
input from keyboard, displaying information on screen and performing various other jobs. These set of
instructions are called 'machine language instruction'.
Processor understands only machine language instructions which are strings of 1s and 0s. However
machine language is too obscure and complex for using in software development. So the low level
assembly language is designed for a specific family of processors that represents various instructions
in symbolic code and a more understandable form.

1.2 ADVANTAGES OF ASSEMBLY LANGUAGE


An understanding of assembly language provides knowledge of:
 Interface of programs with OS, processor and BIOS;
 Representation of data in memory and other external devices;
 How processor accesses and executes instruction;
 How instructions accesses and process data;
 How a program access external devices.

Other advantages of using assembly language are:


 It requires less memory and execution time;
 It allows hardware-specific complex jobs in an easier way;
 It is suitable for time-critical jobs;
 It is most suitable for writing interrupt service routines and other memory resident programs.

1.3 BASIC FEATURES OF PERSONAL COMPUTER (PC) HARDWARE


The main internal hardware of a PC consists of the processor, memory and the registers. The registers
are processor components that hold data and address. To execute a program the system copies it from
the external device into the internal memory. The processor executes the program instructions.
The fundamental unit of computer storage is a bit; it could be ON (1) or OFF (0). A group of nine
related bits makes a byte. Eight bits are used for data and the last one is used for parity. According to
the rule of parity, number of bits that are ON (1) in each byte should always be odd.
So the parity bit is used to make the number of bits in a byte odd. If the parity is even, the system
assumes that there had been a parity error (though rare) which might have caused due to hardware fault
or electrical disturbance.
The processor supports the following data sizes:
 Word: a 2-byte data item
 Double word: a 4-byte (32 bit) data item
 Quad word: an 8-byte (64 bit) data item
 Paragraph: a 16-byte (128 bit) area
 Kilobyte: 1024 bytes
 Megabyte: 1,048,576 bytes

1.4 The Binary Number System


Page | 1
Every number system uses positional notation i.e., each position in which a digit is written has a
different positional value. Each position is power of the base, which is 2 for binary number system,
and these powers begin at 0 and increase by 1.

The table below shows the positional values for an 8-bit binary number, where all bits are set on.

Bit Value 1 1 1 1 1 1 1 1
Position Value as
a Power of base 2 128 64 32 16 8 4 2 1
Bit number 7 6 5 4 3 2 1 0
Table 1.1: 8-bit binary number table

The value of a binary number is based on the presence of 1 bits and their positional value. So the value
of the given binary number is: 1 + 2 + 4 + 8 +16 + 32 + 64 + 128 = 255, which is same as 28 - 1.

1.5 The Hexadecimal Number System


Hexadecimal number system uses base 16. The digits range from 0 to 15. By convention, the letters A
through F is used to represent the hexadecimal digits corresponding to decimal values 10 through 15.
Main use of hexadecimal numbers in computing is for abbreviating lengthy binary representations.
Basically hexadecimal number system represents a binary data by dividing each byte in half and
expressing the value of each half-byte. The following table provides the decimal, binary and
hexadecimal equivalents:
Decimal Binary Hexadecimal
Number Number umber
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Table 1.2: The Hexadecimal number table

To convert a binary number to its hexadecimal equivalent, break it into groups of 4 consecutive
groups each, starting from the right, and write those groups over the corresponding digits of the
hexadecimal number.

Page | 2
Example: Binary number 1000 1100 1101 0001 is equivalent to hexadecimal - 8CD1

To convert a hexadecimal number to binary just write each hexadecimal digit into its 4-digit binary
equivalent.

Example: Hexadecimal number FAD8 is equivalent to binary - 1111 1010 1101 1000

1.6 Binary Arithmetic


The table below illustrates four simple rules for binary addition:

Rule 1 Rule 2 Rule 3 Rule 4


1
0 1 1 1
0 +0 +1 +1
=+0 =1 =10 =11
Table 1.3: Rules for binary addition

Rules (iii) and (iv) shows a carry of a 1-bit into the next left position.
Example:
Decimal Binary
24 11000
+18 10010
=42 =101010

1.6.1 Two’s complement


A negative binary value is expressed in two's complement notation. According to this rule, to convert a
binary number to its negative value is to reverse its bit values and add 1.

Decimal Binary
35 100011
Reverse the bit 011100
Add 1 1
-35 011101

To subtract one value from another, convert the number being subtracted to two's complement format
and add the numbers.
Example: Subtract 24 from 35
Decimal Binary
35 100011
24 11000
Reverse the bit of 24 00111
Add 1 1
-24 01000
35-24=11 101011
The overflow of the last 1 bit is lost.
Page | 3
1.7 Addressing Data in Memory

The process through which the processor controls the execution of instructions is referred as the fetch-
decode-execute cycle or the execution cycle. It consists of three continuous steps:
1) Fetching the instruction from memory
2) Decoding or identifying the instruction
3) Executing the instruction

A standard process describes the steps needed for processing to take place. It is called the Fetch - Decode -
Execute cycle or sometimes simply called the Fetch-Execute Cycle.
First of all, both the data and the program that acts upon that data are loaded into main memory (RAM) by the
operating system. The CPU is now ready to do some work.

1. Fetching the instruction from Memory

The first step the CPU carries out is to fetch some data and instructions (program) from main memory
then store them in its own internal temporary memory areas. These memory areas are called 'registers'

Fetch
Instruction

Execute Decode
Instruction Instruction

Fetch –Decode – Execute Cycle Diagram

This is called the 'fetch' part of the cycle. For this to happen, the CPU makes use of a vital hardware path called
the 'address bus'. The CPU places the address of the next item to be fetched on to the address bus.
Data from this address then moves from main memory into the CPU by travelling along another hardware path
called the 'data bus'.

2. Decoding the Instruction


The next step is for the CPU to make sense of the instruction it has just fetched. This process is called 'decode'.
The CPU is designed to understand a specific set of commands. These are called the 'instruction set' of the CPU.
Each make of CPU has a different instruction set. The CPU decodes the instruction and prepares various areas
within the chip in readiness of the next step.

Page | 4
3. Executing the Instruction
This is the part of the cycle when data processing actually takes place. The instruction is carried out upon the data
(executed). The result of this processing is stored in yet another register. Once the execute stage is complete, the
CPU sets itself up to begin another cycle once more.

The processor may access one or more bytes of memory at a time. Let us consider a hexadecimal number
0725H. This number will require two bytes of memory. The high-order byte or most significant byte is
07 and the low-order byte is 25.
The processor stores data in reverse-byte sequence, i.e., a low-order byte is stored in a low memory
address and a high-order byte in high memory address. So, if the processor brings the value 0725H from
register to memory, it will transfer 25 first to the lower memory address and 07 to the next memory
address.

x: memory address
When the processor gets the numeric data from memory to register, it again reverses the bytes. There are
two kinds of memory addresses:
1. Absolute address – a direct reference of specific location.
2. Segment address (or offset) – starting address of a memory segment with the offset value.

1.8 Assembly Registers


Processor operations mostly involve processing data. This data can be stored in memory and accessed
from thereon. However, reading data from and storing data into memory slows down the processor, as it
involves complicated processes of sending the data request across the control bus and into the memory
storage unit and getting the data through the same channel.

To speed up the processor operations, the processor includes some internal memory storage locations,
called registers. The registers store data elements for processing without having to access the memory. A
limited number of registers are built into the processor chip.

1.8.1 Processor Registers


The main tools to write programs in x86 assembly are the processor registers. The registers are like
variables built in the processor. Using registers instead of memory to store values makes the process
faster and cleaner. The problem with the x86 series of processors is that there are few registers to use.
There are ten 32-bit and six 16-bit processor registers in IA-32 architecture. The registers are grouped
into three categories.

1) General registers: EAX EBX ECX EDX

2) Segment registers: CS DS ES FS GS SS

3) Control registers (Index and pointers / Indicator): ESI EDI EBP EIP ESP/EFLAGS
Page | 5
1.8.1.1 General registers
As the title says, general register are the one we use most of the time. Most of the instructions perform
on these registers. They all can be broken down into 16 and 8 bit registers.

1) 32 bits : EAX EBX ECX EDX


2) 16 bits : AX BX CX DX
3) 8 bits : AH AL BH BL CH CL DH DL

The "H" and "L" suffix on the 8 bit registers stand for high byte and low byte. With this out of the way,
their individual main use are as follows:

EAX,AX,AH,AL : They are called the Accumulator register. It is used for I/O port access, arithmetic,
interrupt calls, etc...

EBX,BX,BH,BL : They are called the Base register. It is used as a base pointer for memory access.
Gets some interrupt return values.

ECX,CX,CH,CL : They are called the Counter register. It is used as a loop counter and for shifts. Gets
some interrupt values

EDX,DX,DH,DL : They are called the Data register. It is used for I/O port access, arithmetic, some
interrupt calls.

1.8.1.2 Segment registers

Segment registers hold the segment address of various items. They are only available in 16 values. They
can only be set by a general register or special instructions. Some of them are critical for the good
execution of the program and you might want to consider playing with them when you will be ready for
multi-segment programming

1) CS: Holds the Code segment in which your program runs. Changing its value might make the
computer hang.

2) DS: Holds the Data segment that your program accesses. Changing its value might give erronous
data.

3) ES,FS,GS: These are extra segment registers available for far pointer addressing like video
memory and such.

4) SS: Holds the Stack segment your program uses. Sometimes has the same value as DS. Changing
its value can give unpredictable results, mostly data related.

1.8.1.3 Control Registers - Indexes and pointers

Indexes and pointer and the offset part of an address. They have various uses but each register has a

Page | 6
specific function. They sometime used with a segment register to point to far address (in a 1Mb range).
The register with an "E" prefix can only be used in protected mode.

1) ES:EDI EDI DI : Destination index register. It is used for string, memory array copying and
setting and for far pointer addressing with ES.

2) DS:ESI EDI SI: Source index register. It is used for string and memory array copying.

3) SS:EBP EBP BP: Stack Base pointer register. It holds the base address of the stack.

4) SS:ESP ESP SP: Stack pointer register. It holds the top address of the stack.

5) CS:EIP EIP IP: Index Pointer. It holds the offset of the next instruction. It can only be read.

1.8.1.4 Control Register - The EFLAGS register

The EFLAGS register hold the state of the processor. It is modified by many instructions and is used for
comparing some parameters, conditional loops and conditional jumps. Each bit holds the state of
specific parameter of the last instruction. Below is a listing:

The common flag bits are:

1) Overflow Flag (OF) − It indicates the overflow of a high-order bit (leftmost bit) of data after a signed
arithmetic operation.

2) Direction Flag (DF) − It determines left or right direction for moving or comparing string data. When
the DF value is 0, the string operation takes left-to-right direction and when the value is set to 1, the
string operation takes right-to-left direction.

3) Interrupt Flag (IF) − It determines whether the external interrupts like keyboard entry, etc., are to be
ignored or processed. It disables the external interrupt when the value is 0 and enables interrupts when
set to 1.

4) Trap Flag (TF) − It allows setting the operation of the processor in single-step mode. The DEBUG
program we used sets the trap flag, so we could step through the execution one instruction at a time.

5) Sign Flag (SF) − It shows the sign of the result of an arithmetic operation. This flag is set according
to the sign of a data item following the arithmetic operation. The sign is indicated by the high-order of
leftmost bit. A positive result clears the value of SF to 0 and negative result sets it to 1.

6) Zero Flag (ZF) − It indicates the result of an arithmetic or comparison operation. A nonzero result
clears the zero flag to 0, and a zero result sets it to 1.

7) Auxiliary Carry Flag (AF) − It contains the carry from bit 3 to bit 4 following an arithmetic
operation; used for specialized arithmetic. The AF is set when a 1-byte arithmetic operation causes a
carry from bit 3 into bit 4.

Page | 7
8) Parity Flag (PF) − It indicates the total number of 1-bits in the result obtained from an arithmetic
operation. An even number of 1-bits clears the parity flag to 0 and an odd number of 1-bits sets the
parity flag to 1.

9) Carry Flag (CF) − It contains the carry of 0 or 1 from a high-order bit (leftmost) after an arithmetic
operation. It also stores the contents of last bit of a shift or rotate operation.

The following table indicates the position of flag bits in the 16-bit Flags register:
Flag O D I T S Z A P C
:
Bit 1 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0
no: 5 4 3 2 1 0

Bit Label Desciption


---------------------------
0 CF Carry flag
2 PF Parity flag
4 AF Auxiliary carry flag
6 ZF Zero flag
7 SF Sign flag
8 TF Trap flag
9 IF Interrupt enable flag
10 DF Direction flag
11 OF Overflow flag
12-13 IOPL I/O Priviledge level
14 NT Nested task flag
16 RF Resume flag
17 VM Virtual 8086 mode flag
18 AC Alignment check flag (486+)
19 VIF Virutal interrupt flag
20 VIP Virtual interrupt pending flag
21 ID ID flag

Those that are not listed are reserved by Intel.

1.8.1.5 Undocumented registers

There are registers on the 80386 and higher processors that are not well documented by Intel. These are
divided in control registers, debug registers, test registers and protected mode segmentation registers.
As far as I know, the control registers, along with the segmentation registers, are used in protected
mode programming, all of these registers are available on 80386 and higher processors except the test
registers that have been removed on the Pentium. Control registers are CR0 to CR4, Debug registers are
DR0 to DR7, test registers are TR3 to TR7 and the protected mode segmentation registers are GDTR
(Global Descriptor Table Register), IDTR (Interrupt Descriptor Table Register), LDTR (Local DTR),
and TR.

Page | 8
Example 1: Assembly code to understand the use of registers in assembly programming. This
program displays 9 stars on the screen along with a simple message:

section .text
global_start ;must be declared for linker (gcc)

_start: ;tell linker entry point


mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel

mov edx,9 ;message length


mov ecx,s2 ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)


int 0x80 ;call kernel

section .data
msg db 'Displaying 9 stars',0xa ;a message
len equ $ - msg ;length of message
s2 times 9 db '*'

When the above code is compiled and executed, it produces the following result:
Displaying 9 stars
*********

WEEK 4 - 6:
Learning Outcome
Page | 9
 To be able to write a simple assembly language program using the general format.

4.1 Assembly Environment Setup

Assembly language is dependent upon the instruction set and the architecture of the processor. In this
course lab, we focus on Intel 32 processors like Pentium. To follow this lab, you will need the
following:

1) A computer PC
2) A copy of Linux operating system
3) A copy of GUI TASM assembler program

There are many good assembler programs such as:


1) Microsoft Assembler (MASM)
2) NASM
3) Borland Turbo Assembler (TASM)
4) The GNU assembler (GAS)
5) Flat Assembler (FASM)

We will use the TASM assembler, as it is:


1) Free. You can download it from various web sources.
2) Well-documented and you will get lots of information on net.
3) Could be used on both Linux and Windows.

4.2 Turbo Assembler (TASM) ENVIRONMENT USING GUI TASM

To launch the GUI TASM follow these steps:

Using Windows 7/8/10:

Step 1: Click the start menu


Step 2: Select All programs
Step 3: Select GUI TASM

4.3 Assembly Basic Syntax

An assembly program can be divided into three sections:


1) The data section,
2) The bss section, and
Page | 10
3) The text section.

4.3.1 The data Section


The data section is used for declaring initialized data or constants. This data does not change at
runtime. You can declare various constant values, file names, or buffer size, etc., in this section.
The syntax for declaring data section is:
section .data

4.3.2 The bss Section

The bss section is used for declaring variables. The syntax for declaring bss section is:
section .bss

4.3.3 The text section

The text section is used for keeping the actual code. This section must begin with the declaration
global _start, which tells the kernel where the program execution begins.
The syntax for declaring text section is:
section .text
global _start
_start:

4.3.4 Comments

Assembly language comment begins with a semicolon (;). It may contain any printable character
including blank. It can appear on a line by itself, like:

; This program displays a message on screen

OR, on the same line along with an instruction, like:


add eax ,ebx ; adds ebx to eax

4.4 Assembly Statement


Assembly language programs consist of three types of statements:
1) Executable instructions or instructions,
2) Assembler directives or pseudo-ops, and
3) Macros.

The executable instructions or simply instructions tell the processor what to do. Each instruction
consists of an operation code (opcode). Each executable instruction generates one machine language
instruction.
The assembler directives or pseudo-ops tell the assembler about the various aspects of the assembly
process. These are non-executable and do not generate machine language instructions.

Macros are basically a text substitution mechanism.

Page | 11
4.5 Assembly Statement Syntax

Assembly language statements are entered one statement per line. Each statement follows the following
format:

[label] mnemonic [operands] [;comment]

The fields in the square brackets are optional. A basic instruction has two parts, the first one is the name of
the instruction (or the mnemonic), which is to be executed, and the second are the operands or the
parameters of the command.

Following are some examples of typical assembly language statements:

INC COUNT ; Increment the memory variable COUNT


MOV TOTAL, 48 ; Transfer the value 48 in the
; memory variable TOTAL
ADD AH, BH ; Add the content of the
; BH register into the AH register
AND MASK1, 128 ; Perform AND operation on the
; variable MASK1 and 128
ADD MARKS, 10 ; Add 10 to the variable MARKS
MOV AL, 10 ; Transfer the value 10 to the AL register

Example 4.1: General format for Assembly Program

section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!', 0xa ;our dear string
len equ $ - msg ;length of our dear string

WEEK 7:
Learning Outcome
Page | 12
 To be able to write and compile a simple assembly language program and handle the errors.

7.1 Writing an Assembly Program

Experiment 1: Program to display digits 0, 1, 2,……..9 in assembly language.

Step 1: Write the Source Program:

DATA SEGMENT
DATA ENDS
CODE SEGMENT
ASSUME DS: DATA, CS: CODE
START:
MOV AX, DATA
MOV DS, AX
MOV BL, 00H
MOV CH, 00H
MOV CL, 0AH
L1: MOV DL, BL
ADD DL, '0'
MOV AH, 02H
INT 21H
INC BL
LOOP L1
MOV AH, 4CH
INT 21H
CODE ENDS
END START

Step 2: Build and compiling the source program on GUI TASM assembler – Option 1:

1) Click on Tools tab from Standard Bar


2) Select build command

Option 2:

Click on build icon on the interface

Page | 13
Step 3: Compiling the source program on GUI TASM assembler – Option 1:

1) Click on Tools tab from Standard Bar


2) Select Run command

Option 2:

Click on Run icon on the interface.

Page | 14
7.2 Compiling and Linking an Assembly Program in TASM

Each TASM contains Five Programs namely; Editor, Assembler, Linker, Loader/Locator, Debugger

7.2. 1 Editor
Editor is a program, which will help the programmer to input ALP using Keyboard. All the character
of ALP are stored in memory in the form of ASCII codes. This file of ALP generated is called as
source file. The extension of source file should be .ASM i.e. Assembly Language Program. Editor will
also help to save this source file into secondary memory like disc.

7.2.2 Assembler
An assembler is a program that takes basic computer instruction and converts them into a pattern of bits
that the computer's processor can use to perform its basic operations. Some people call these instructions
assembler language and others use the term assembly language.

Assembler will read ALP from source file and it performs two steps:
1) It will translate all the instructions from assembly language to hexadecimal number or binary
number (machine Language)
2) Assembler will allot effective address (EA) to each byte of Instruction code or Data.
Assembler will output two files:
1) List File (xyz.lst)
2) Object File (xyz.obj)
Page | 15
3) Linker

7.2.3 Linker

Linker is a Program, which will connect different small modules of programs to form one large program.
Linker will remove memory fragment by readjusting the effective address allotted to different modules by the
assembler.

For modularity of the program, it is better to break programs into several modules (subroutines). It is even
better to put common routine, like reading a hexadecimal number, writing a hexadecimal number etc.
which could be used by a lot of other programs also into a separate file. These files are assembled
(translated) separately. After each has been successfully assembled, they can be linked together to form a
large file, which constitutes the computer program. The program that links several programs is called the
linker.

7.2.4 Loader

A loader is a program that places programs into main memory and prepares them for execution. The
loader's target language is machine language, its source language is nearly machine language. Loading
is ultimately bound with the storage management function of operating systems and is usually
performed later than assembly or compilation. The period of executions of user's program is called
execution time. The period of translating a user's source program is called assembly or compile time.
Load time refers to the period of loading and preparing an object program for execution. Loader will
allot base address to each memory segment, hence each byte of instruction code or data will get
physical address.

7.2.5 Debugger

Debugger is a program, which will help to execute the machine language program loaded into memory.
The program can be executed by three methods:
1) Free Run Operation
2) Single Stepping Mode
3) Break Point Technique

Page | 16
7.3 COMPILING, LINKING, DEBUGGING AND EXECUTING ASSEMBLY PROGRAMS
USING TASM ASSEMBLER

1) “Beginning With TASM” … On getting to TASM, at the C: prompt, type: edit FILENAME.ASM e.g
EDIT ADDITION.ASM and press enter key. Then type the program/assemble codes and Save the file
(File->Save ) and exit (File->Exit or Alt+F+x) and get back to the cmd screen :

2) To Compile, at the C: prompt type: TASM FILENAME.ASM and press the enter key.

Page | 17
3) Then link the object file, by typing: TLINK FILENAME.OBJ and press the enter key
4) To run the program, if there are no compilation errors, at the C: prompt, type the FILENAME and
press the enter key.

Page | 18
WEEK 8-9
Learning Outcome
 To be able to write and compile a simple libraries and utilities assembly language program and
handle the errors.

8.1 Introduction
C and Assembly can be used together to provide extra flexibility. You can create static libraries in
Assembly that you call from C, and vice-versa, you can call C functions from within Assembly.
Check out the code samples below that demonstrate that process.
It's actually slightly incorrect to say we are 'calling C from Assembly' or vice-versa because once the
code is compiled, it is the same machine language. Whether you compile the object files from
Assembly or from C they turn out the same. Once they are object files they can be called from
anywhere, but they have to be defined as externals and linked. You could create static libraries as
object files from other languages too. It does not just have to be Assembly and C.

8.2 Creating a Static Library with Assembly

The first step to calling an assembly function from C is to package it as a static library. Create a
function and then compile the assembly source to an object file. We will link that later when we
compile the c program. Save the assembly source with a filename.

SECTION .DATA
hello: db 'Hello world!',10
helloLen: equ $-hello

SECTION .TEXT
GLOBAL say_hi

say_hi:
mov eax,4 ; write()
mov ebx,1 ; STDOUT
mov ecx,hello
mov edx,helloLen
int 80h ; Interrupt
ret ; Return control

Page | 19
WEEK 10- 12
Learning Outcome
 To be able to run program in different operating systems such as Windows and Unix.

An assembly language Program to display a line of text Using Windows Operating System.

(i) Source Code:

.MODEL small
.STACK 100h
.DATA
MESSAGE DB 'I AM KAREEM WAHAB BABATUNDE! $' ;message to displayed on output window
.CODE
COMPUTER PROC ; program/process name of assembled source codes
mov ax,@data
mov ds,ax ;sets data in Data Segment to move to Accumulator Register
mov ah,9 ;DOS print string function
mov dx,OFFSET Message ;point to “Message to be displayed!”
int 21h ;display Message to be displayed after interrupt”
mov ah, 4ch ;DOS terminate program function
int 21h ;terminate the program
COMPUTER ENDP ; ends / closes the program process
END COMPUTER ; ends the entire program

(ii) Save and Build the source code:

Page | 20
Build the source code:

Page | 21
(iii) Run the code:

Output:

Page | 22
Page | 23
WEEK 13 – 15
Learning Outcome
 To be able to write and run a simple interrupt program using assembly language.

13.1 Concept of Interrupt


Interrupt is a mechanism by which a program’s flow control can be altered. You can think of it as like
signaling Batman. You need something done, you send the signal, and then he comes to the rescue.
You do not care how he does his work. The most important thing to remember about an interrupt is
that it can pause the execution of some program at any point between two instructions when an
interrupt occurs. Therefore, you typically have no guarantee that one instruction always executes
immediately after another in the program because an interrupt could occur between the two
instructions. If an interrupt occurs in the middle of the execution of some instruction, then the CPU
finishes that instruction before transferring control to the appropriate interrupt service routine.

Examples of Interrupt Call:

1. BIOS Keyboard interrupt - int 16h


ah=0 reads key from keyboard without echo
returns al=ascii, ah=scan code
ah=1 finds out if a key was hit
if no keys were pressed:
zf set (ie, jz NoKey)
ax=0
if a key was pressed:
zf clear (not set -> jnz KeyReady)
ah=scan code
al=ascii

2. DOS functions - int 21h


ah=1 reads key from keyboard with echo
returns al=ascii
ah=2 prints a character (dl=ascii)
returns nothing
ah=5 prints character to printer (dl=ascii)
returns nothing
ah=9 prints a string ending in $ (ds:dx=pointer to string)
returns nothing
ah=a buffered keyboard input (ds:dx=pointer to buffer)
The buffer is in this form:
1 byte The max number of characters to read (including enter)
1 byte The number of characters read (it gets filled in)
n bytes The buffer (n must be at least 1)
Enter (ascii 13: carriage return) is also stored.
ah=4c Exit the program (al=error code)
Page | 24
3. BIOS Video interrupt - int 10h
ah=0 set video mode (al=3 -> text, al=12h,13h -> vga)
returns nothing
ah=2 set cursor position (bh=0, dh=row, dl=column)
dh and dl are zero-based
returns nothing
ah=c display graphics pixel (al=color, bh=0, cx=column, dx=row)
cx and dx are zero-based
returns nothing

4. Bootstrap loader - int 19h


This will reboot your computer to the drive specified in dl. Some computers warm boot, some cold
boot, and others do nothing register ah doesn't matter here.

13.2 Invoking Interrupt Call

Assembly code to invoke interrupt call:

Mov ah, 0x0e ; function number = 0Eh: Display Character


Mov al, “!” ; AL = codeof character to display
Int 0x10 ; call INT 10h, BIOS video service

WORKBOOK
Page | 25
SCORE
WEEK 1 – 3

Experiment 1:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program for 16-bit Addition & Subtraction in 8086
Microprocessor.
(ii) To write an Assembly Language Program for 16-bit Addition & Subtraction in 8086 Microprocessor.
(iii) To print out the output and attach it to your file.

Experiment 2:

Aim:
(1) To write an algorithm for an Assembly Language Program to display a line of text 10 times (e.g. “Welcome
to Computer Science Department”.
(ii) To write an Assembly Language Program to display a line of text 10 times (e.g. “Welcome to Computer
Science Department”.
(iii) To print out the output and attach it to your file.

Experiment 3:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to display a line of text 10 times (e.g. “Welcome
to Computer Science Department”.
(ii) To write an Assembly Language Program to display a line of text 10 times (e.g. “Welcome to Computer
Science Department”.

Page | 26
SCORE
WEEK 4 – 6

Experiment 4:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an assembly language program to count the number of 1’s in a byte stored in memory
location 2000H
(ii) To write an assembly language program to count the number of 1’s in a byte stored in memory location
2000H.
(iii) To print out the output and attach it to your file.

Experiment 5

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(i) To write an algorithm for an assembly language program to add 10 data bytes. Data is stored in
memory location starting from 4460H. The result is 8 bits only and in stored in 4480H.
(ii) Write an assembly language program to add 10 data bytes. Data is stored in memory location
starting from 4460H. The result is 8 bits only and in stored in 4480H.
(iii) To print out the output and attach it to your file.

Experiment 6:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(i) To write an algorithm for an assembly language program to count the number of 1’s and 0’s the
binary bit system 11001101 = CDH.
(ii) Write an assembly language program to count the number of 1’s and 0’s the binary bit system
11001101 = CDH.
(iii) To print out the output and attach it to your file.

Page | 27
WEEK 7 SCORE

Experiment:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to display Your Name, Age, Sex, Department.
(ii) To write an Assembly Language Program for to display Your Name, Age, Sex, Department.
(iii) To print out the output and attach it to your file.

Experiment 8:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to draw a box.
(ii) To write an Assembly Language Program to draw a box.
(iii) To print out the output and attach it to your file.

Page | 28
WEEK 8 – 9 SCORE

Experiment 8:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to set the video display mode.
(ii) To write an Assembly Language Program to set the video display mode.
(iii) To print out the output and attach it to your file.

Experiment 9:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to convert from Hex to ASCII.
(ii) To write an Assembly Language Program to convert from Hex to ASCII..
(iii) To print out the output and attach it to your file.

WEEK 10 – 12 SCORE

Page | 29
Experiment 10:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to display Hello Nigeria Application.
(ii) To write an Assembly Language Program to display Hello Nigeria Application.
(iii) To print out the output and attach it to your file.

Experiment 11:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to display Hello Nigeria Application.
(ii) To write an Assembly Language Program to display Hello Nigeria Application.
(iii) To print out the output and attach it to your file.

Experiment 12:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to display Hello Nigeria Application.
(ii) To write an Assembly Language Program to display Hello Nigeria Application.
(iii) To print out the output and attach it to your file.

WEEK 13 – 15 SCORE

Page | 30
Experiment 13:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to terminate a program and return to DOS
environment.
(ii) To write an Assembly Language Program to terminate a program and return to DOS environment.
(iii) To print out the output and attach it to your file.

Experiment 14:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to wait for a keystroke or pause macro.
(ii) To write an Assembly Language Program to wait for a keystroke or pause macro.
(iii) To print out the output and attach it to your file.

Experiment 15:

Apparatus Required:
8086 Microprocessor Trainer Kit with Power Supply and PC installed with a Turbo Assembler (TASM).

Aim:
(1) To write an algorithm for an Assembly Language Program to press any key to continue dialog.
(ii) To write an Assembly Language Program to press any key to continue dialog.
(iii) To print out the output and attach it to your file.

Page | 31

You might also like