Coal Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 84

COMPUTER LABORATORY MANUAL

Computer Organization and Assembly Language


(CSC - 201 L)
Version 2.0

Student Name:
Class and Section:
Roll Number:
CGPA:
Email ID:

DEPARTMENT OF SOFTWARE ENGINEERING (DSE)


FOUNDATION UNIVERSITY RAWALPINDI CAMPUS (FURC)
www.fui.edu.pk
http://www.fui.edu.pk/FURC/

1 Microprocessor and Assembly Language Lab Manual


PREFACE
This lab manual has been prepared to facilitate the students of software engineering in studying and analysing
various functions of a computer network. The students will have plan the IP address scheme, configure and
test the several network devices. Different tools are used to monitor network traffic and analyse packets. The
lab sessions are designed to improve the abilities of the students by giving hands on experience. After
completing the laboratory exercises, the students will be familiar with the practical issues of the different
concepts explained in the course, as well as with the real equipment used nowadays in computer networks.
PREPARED BY
Lab manual is prepared by Mr. Bilal Mushtaq and Ms. Nida Tasneem under the supervision of Head of
Department Prof. Dr. Muhammad Shaheen.

GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the semester.
b. All readings, answers to questions and illustrations must be solved on the place provided. If more space is
required, then additional sheets may be attached. You may add screen print to the report by using the ‘Print
Screen’ command on your keyboard to get a snapshot of the displayed output.
c. It is the responsibility of the student to have the manual graded before deadlines as given by the instructor.
d. Loss of manual will result in re submission of the complete manual.
e. Students are required to go through the experiment before coming to the lab session.
f. Students must bring the manual in each lab.
g. Keep the manual neat clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarised and no re-submission
will be entertained.
i. Marks will be deducted for late submission.
j. You need to submit the report even if you have demonstrated the exercises to the lab instructor or shown
them the lab report during the lab session.

VERSION HISTORY
Month Update By Details
July 2015 Muhammad Bilal Mushtaq Version 1.0. Initial draft prepared and experiments outlined.
Sept 2017 Nida Tasneem Version 1.1. Improvements added in the manual
Sept 2018 Nida Tasneem Version 1.2. Formatting corrected.
August 2019 Nida Tasneem 14 sessions, theoretical portion and activities are also added.
Umar Mahmud

2 Microprocessor and Assembly Language Lab Manual


MARKS

LAB Date Lab Title Max. Marks Instructor


# Conducted Marks Obtained Sign
1 10

2 10

3 10

4 10

5 10

6 10

7 10

8 10

9 10

10 10

11 10

12 10

13 10

14 10

Grand Total

3 Microprocessor and Assembly Language Lab Manual


LIST OF EXPERIMENTS
EXPERIMENT 1 – INTRODUCTION TO MICROPROCESSOR LAB AND ASSEMBLY LANGUAGE . 5
EXPERIMENT 2 – INTRODUCTION TO EMU 8086 .................................................................................. 11
EXPERIMENT 3 – REGISTER, INPUT/ OUTPUT CHARACTERS & DISPLAY STRINGS ................... 15
EXPERIMENT 4 – INPUT/ OUTPUT SINGLE AND MULTI-DIGIT NUMBER ....................................... 24
EXPERIMENT 5 - EXPLORING FLAG REGISTER AND DEFINING VARIABLES ............................... 31
EXPERIMENT 6 – ARITHMETIC INSTRUCTIONS ................................................................................... 36
EXPERIMENT 7 – BOOLEAN OPERATORS .............................................................................................. 43
EXPERIMENT 8 – DECISION INSTRUCTIONS ......................................................................................... 48
EXPERIMENT 9 – IMPLEMENTATION OF LOOP STRUCTURES ......................................................... 56
EXPERIMENT 10 – ARRAY PROCESSING AND INDIRECT ADDRESSING ........................................ 62
EXPERIMENT 11 – OPERATIONS ON STACK ......................................................................................... 68
EXPERIMENT 12 – BIOS AND DOS INTERRUPTS PROGRAMMING ................................................... 74
EXPERIMENT 13 – STATIC AND DYNAMIC MULTIPLICATION ......................................................... 78
EXPERIMENT 14 – SQUARES, CUBE AND FACTORIALS ..................................................................... 81

4 Microprocessor and Assembly Language Lab Manual


EXPERIMENT 1 – INTRODUCTION TO MICROPROCESSOR LAB AND
ASSEMBLY LANGUAGE

OBJECTIVE
Introduction to microprocessor lab, system and kits and software. Student will get use to the software and lab
equipment, trainers and get use to the lab environment.
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : NIL
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
INTRODUCTION TO ASSEMBLY LANGUAGE
Assembly language is a low-level programming language. You
need to get some knowledge about computer structure in order
to understand anything. The system bus (shown in yellow)
connects the various components of a computer. The CPU is
the heart of the computer, most of computations occurs inside
the CPU. RAM is a place to where the programs are loaded in
order to be executed.
8086 MICRO-PROCESSOR
We will be studying the assembly language for the microprocessor 8086.
 8086 micro-processor is a 16-bit microprocessor (a 16-bit microprocessor can operate on the 16-bits of
data at a time) having 16- bit registers.
 16-bit data bus and 20-bit address bus.
 It can access 1 MB of total memory.
INSIDE A CPU
To write the assembly language programs for 8086 micro-processor, we are required to know the internal
organization at least programming model
GENERAL PURPOSE REGISTERS
8086 CPU has 8 general purpose registers, each register has its own name:
 AX - the accumulator register (divided into AH / AL).
 BX - the base address register (divided into BH / BL).
 CX - the count register (divided into CH / CL).
 DX - the data register (divided into DH / DL).
 SI - source index register.

5 Experiment 1 - Introduction to Microprocessor Lab and Assembly Language


 DI - destination index register.
 BP - base pointer.
 SP - stack pointer.
Despite the name of a register, it's the programmer who determines the usage for each general-purpose register.
The main purpose of a register is to keep a number (variable). The size of the above registers is 16 bit, it's
something like: 0011000000111001b (in binary form), or 12345 in decimal (human) form.
4 general purpose registers (AX, BX, CX, DX) are made of two sep arate 8 bit registers, for example if AX=
0011000000111001b, then AH=00110000b and AL=00111001b. Therefore, when you modify any of the 8-
bit registers 16-bit register is also updated, and vice-versa. The same is for other 3 registers, "H" is for high
and "L" is for low part.
Because registers are located inside the CPU, they are much faster than memory. Accessing a memory location
requires the
use of a
system bus,
so it takes
much longer.
Accessing
data in a
register
usually takes
no time.
Therefore,
you should
try to keep variables in the registers. Register sets are very small, and most registers have special purposes
which limit their use as variables, but they are still an excellent place to store temporary data of calculations.
SEGMENT REGISTERS
 CS - points at the segment containing the current program.
 DS - generally points at segment where variables are defined.
 ES - extra segment register, it's up to a coder to define its usage.
 SS - points at the segment containing the stack.
Although it is possible to store any data in the segment registers, this is never a good idea. The segment
registers have a very special purpose - pointing at accessible blocks of memory. Segment registers work
together with general purpose register to access any memory value. For example, if we would like to access

6 Experiment 1 - Introduction to Microprocessor Lab and Assembly Language


memory at the physical address 12345h (hexadecimal), we should set the DS = 1230h and SI = 0045h. This is
good, since this way we can access much more memory than with a single register that is limited to
16-bit values.
CPU makes a calculation of physical address by multiplying the segment register by 10h and adding general
purpose register to it (1230h * 10h + 45h = 12345h):

The address formed with 2 registers is called an effective address. By default, BX, SI and DI registers work
with DS segment register; BP and SP work with SS segment register.
Other general-purpose registers cannot form an effective address! Also, although BX can form an effective
address, BH and BL cannot!
SPECIAL PURPOSE REGISTERS
 IP - the instruction pointer.
 Flags Register - determines the current state of the processor.
IP register always works together with CS segment register and it points to currently executing instruction.
Flags Register is modified automatically by CPU after mathematical operations, this allows to determine the
type of the result, and to determine conditions to transfer control to other parts of the program. Generally, you
cannot access these registers directly.
REGISTERS CLASSIFICATIONS
The registers inside the microprocessor are classified according to the function they perform. In general, they
are classified as: -
 Data registers
 Address registers
 Segment register
 Offset registers
 Status register
REGISTER SIZE
We have three different sizes of registers:
 8-bit register: AH, AL, BH, BL, CH, CL, DH, DL
 16-bit registers: AX, BX, CX, DX, SP, BP, SI, DI, SS, DS, CS, ES, FS, GS, IP, FLAGS
 32-bit registers: EAX, EXB, ECX, EDX, ESI, EDI, ESP, EBP, EIP, and EFLAGS.
SEGMENT AND OFFSET REGISTER
 Address registers store addresses of instructions and data in memory.

7 Experiment 1 - Introduction to Microprocessor Lab and Assembly Language


 These values are used by the microprocessor to access memory locations.
 Every memory location is identified by an address of 20-bit.
 We are having the registers of 16-bits on that over 20-bit address (Physical Address) is store into two
parts in two 16-bit registers called segment number and offset
 A memory segment is a block of 2^16 (64 K) consecutive memory bytes.
 A segment number, called segment address (starting with 0) identifies each segment.
 A segment number is 16-bits so the highest segment number is FFFFh.
 Within segment, giving an offset number called the offset identifies a memory location.
 This is the number of bytes from the beginning of the segment.
 With a 64KB segment, the offset can be given as a 16-bit number.
 The first byte in a segment has offset 0.
 The last offset in a segment is FFFFh.
PROGRAM SEGMENT
 A machine language program consists of instructions (code) and data.
 A data structure called Stack is used by the microprocessor to implement procedure calls.
 The program’s code, data and stack are loaded into different memory segments, called code segment,
data segment and the stack segment.
SEGMENT REGISTERS
 To keep track of the various program segments, the 8086 microprocessor is equipped with four
segment registers to hold the segment numbers.
 The CS, DS and SS registers contain the code, data and stack segment numbers respectively.
 If a program needs to access a second data segment, it can use the ES (Extra Segment) register.
POINTER AND SEGMENT REGISTER
 The registers SP, BP, SI and DI normally points to (contain the offset address of) memory location.
 These registers can be used in arithmetic and other operations.
SP (STACK POINTER)
 The SP register contains the offset of the top of stack.
 The SP and SS registers combine to form the complete address of the top of the stack.
BP (BASE POINTER)
 The BP register contains an assumed offset from the SS register, as does the stack pointer.
 The BP register is often used by a subroutine to locate variables that were passed on the stack by a
calling program.
 The BP register can be used for other segment unlike SP register.

8 Experiment 1 - Introduction to Microprocessor Lab and Assembly Language


SI (SOURCE INDEX)
 The SI register is used to point to memory locations in the data segment addressed by DS.
 This register takes its name from the string movement instructions, in which the source string is pointed
to by the SI register.
DI (DESTINATION INDEX)
 The DI register performs the same operation as the SI register.
 The DI register acts as the destination for string movement instructions.
IP (INSTRUCTION POINTER)
 The IP register contains the offset of the next instruction to be executed within the current code
segment.
 The IP and CS registers combine to form the complete address of the next instruction.
FLAGS (FLAG REGISTER)
 This is a special register with individual bit positions assigned to show he status of he
 CPU or the results of arithmetic operations.
 Each relevant bit is given a name and others are undefined.

EXERCISE 1.1 [5]


What is the register set on a 32-bitand 64-bit Intel machine?

9 Experiment 1 - Introduction to Microprocessor Lab and Assembly Language


EXERCISE 1.2 [5]
Draw schematic for 32-bit and 64-bit Intel machine.

RESOURCES:
https://www.youtube.com/watch?v=wLXIWKUWpSs
https://www.tutorialspoint.com/assembly_programming/assembly_introduction.htm

10 Experiment 1 - Introduction to Microprocessor Lab and Assembly Language


EXPERIMENT 2 – INTRODUCTION TO EMU 8086

OBJECTIVE
In this lab, an introduction of Emu8086 will be given. Also, the Hello World Program will be explained to
make students understand how programs are written in Emu8086 using Assembly Language.
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
INTRODUCTION
Emu8086 is a program that compiles the source code (assembly language) and executes it. You can watch
registers, flags and memory while your program executes. Arithmetic & Logical Unit (ALU) shows the
internal work of the central processor unit (CPU). Emulator runs programs on a Virtual PC; this completely
blocks your program from accessing real hardware, such as hard-drives and memory, 8086 machine code is
fully compatible with all next generations of Intel's microprocessors.
WHERE TO START?
 Start Emu8086 by selecting its icon from the start menu, or by running Emu8086.exe.
 Select "Samples" from "File" menu.
 Click [Compile and Emulate] button (or press F5 hot key).
 Click [Single Step] button (or press F8 hot key) and watch how the code is being executed.
 Try opening other samples, all samples are heavily commented, so it's a great learning tool.
DIRECTIVES
ORG 100h is a compiler directive (it tells compiler how to handle the source code). This directive is very
important when you work with variables. It says to compiler that the executable file will be loaded at the offset
of 100h (256 bytes), so compiler should calculate the correct address for all variables when it replaces the
variable names with their offsets. Directives are never converted to any real machine code.
Why executable file is loaded at offset of 100h? Operating system keeps some data about the program in the
first 256 bytes of the CS (code segment), such as command line parameters etc. Offset is used to get the offset
address of the variable in register specified
MOV INSTRUCTION
 Copies the second operand (source) to the first operand (destination).
 The source operand can be an immediate value, general-purpose register or memory location.
 The destination register can be a general-purpose register, or memory location.
 Both operands must be the same size, which can be a byte or a word.

11 Experiment 2 – Introduction to EMU 8086


SYNTAX:
MOV Destination, Source

MOV AX, 10 ; puts the value of 10 in the register ax


MOV CX, AX ; puts the value contained in the register ax into cx
Register is a series of memory cells inside the CPU itself. Because registers are inside the CPU there is very
little overhead in working with them. There are four general purpose registers, AX, BX, CX, and DX. These
are the registers you will be using often. Each of these general registers is 16- bit. They also have 8-bit
counterparts. AX is 16 bits whereas AH and AL is 8bit.
Note: - AH being the high bit, and AL being the low bit. Together AH and AL make AX.
Procedure is a part of code that can be called from your program in order to make some specific task.
Procedures make program more structural and easier to understand. Generally procedure returns to the same
point from where it was called.
Syntax:
namePROC
; here goes the code
; of the procedure ...
RET
nameENDP
ACTIVITY 2.1 : HELLO WORLD PROGRAM
; Title Hello World Program
org 100h
.data ; Declare variables
hello_message db 'Hello','World',0dh,0ah,'$'
.code ; Write code
main proc
mov ax,@data ; Copy the address of data
mov ds,ax ; Segment into DS register
mov dx,offset hello_message
mov ah,9 ; MS-Dos Function to display string
INT 21H
mov ax,4C00h ; Halt the program and return control to OS
INT 21H
main endp
end main ; Mark the end of the source file

12 Experiment 2 – Introduction to EMU 8086


EXERCISE 2.1 [3]
Write the output (print and paste), of Activity 2.1 here: -

EXERCISE 2.2 [3]


Print your name and date of birth using the program in Activity 2.1. Paste outcome here.

13 Experiment 2 – Introduction to EMU 8086


EXERCISE 2.3 [4]
Load all example of assembly language one by one in Emu8086 and execute them. Practice all these examples
and analyses output. Paste outcomes here.

RESOURCES:
Download: https://download.cnet.com/Emu8086-Microprocessor-Emulator/3000-2069_4-10392690.html
https://www.youtube.com/watch?v=vlSjuq5BoG0
https://www.tutorialspoint.com/assembly_programming/assembly_introduction.htm

14 Experiment 2 – Introduction to EMU 8086


EXPERIMENT 3 – REGISTER, INPUT/ OUTPUT CHARACTERS & DISPLAY
STRINGS

OBJECTIVE
In this lab students will learn registers and practice how to Input, Output and Display Characters as well as
strings.
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
REGISTERS
Registers are high-speed storage locations directly inside the CPU, designed to be accessed at much higher
speed than conventional memory.
TYPES OF REGISTERS
 General purpose Registers
 Segment Registers
 Status Flags Register
 Instruction Pointer
GENERAL PURPOSE REGISTER: Can be divided into Data and Index
 Data Registers: Are used for arithmetic and data movement
 AX (Accumulator register)
 BX (Base Register)
 CX (Counter Register)
 DX (Data Register)
 Index Register: Contains the offsets of data and instructions.
 BP (Base pointer Register)
 SP (Stack pointer Register)
 SI (Source Index Register)
 DI (Destination Index Register)
AX (ACCUMULATOR)
 Used to store the temporary result after arithmetic or logic operation.
 Can be accessed as 32 bit (EAX), 16 bit (AX) or 8bit (AH or AL) register.
 General Purpose Use in MOV, ADD, SUB instrs.
 Special Purpose Use in MUL, DIV, etc.
 General Purpose Use of AX register
15 Experiment 3 – Register, Input/Output Characters and Display Strings
ACTIVITY 3.1: ADDITION OF BINARY DIGITS
.code
main proc
MOV AL,01001011b
MOV BL,00000011b
ADD AL,BL
MOV DL, AL

MOV AH,2H
INT 21H

MOV ax,4C00h
INT 21H
main endp
end main
Execute and write the outcome of Activity 3.1 here: -

CX (COUNT REGISTER)
 Contains the count for certain instructions e,g shift count, rotate the number of bytes and a counter
with loop instruction.
 Can be accessed as 32 bit (ECX), 16 bit (CX) or 8 bit (CH or CL) register 32 bit
 General Purpose use in ADD, MUL, DIV, MOV
 Special Purpose use in LOOP etc.
ACTIVITY 3.2
.code
main proc
MOV CL,12
MOV BL,65
AGN:INC BL
MOV DL, BL
MOV AH,2H
INT 21H
LOOP AGN

MOV ax,4C00h
INT 21H
main endp
end main
16 Experiment 3 – Register, Input/Output Characters and Display Strings
Execute and write the outcome of Activity 3.2 here: -

INTRODUCTION TO INPUT / OUTPUT


 In 8086 assembly language, we use a software interrupt mechanism for I/O.
 An interrupt signals the processor to suspend its current activity (i.e. running your program) and to
pass control to an interrupt service program (i.e. part of the operating system).
 A software interrupt is one generated by a program (as opposed to one generated by hardware).
 The 8086 INT instruction generates a software interrupt.
 For I/O and some other operations, the number used is 21h.
 A Specific number is placed in the register AH to specify which I/O operation (e.g. read a character,
display a character) you wish to carry out.
 When the I/O operation is finished, the interrupt service program terminates and program will be
resumed at the instruction following int.
ACTIVITY 3.3: WRITE A CODE FRAGMENT TO DISPLAY THE CHARACTER ‘A’ ON THE
SCREEN
;To Display the character 'a'
org 100h
.data

.code
main proc
mov dl, 'a'
mov ah,2h
INT 21H
mov ax,4C00h
INT 21H
main endp
end main
17 Experiment 3 – Register, Input/Output Characters and Display Strings
Execute and write the outcome of Activity 3.3 here: -

CHARACTER INPUT FROM USER


To get the input from keyboard a subprogram at 1h will be called. First 1h will be placed in ah and then an
interrupt 21h generated to call the subprogram. Finally the character will be placed in al register.
Example
mov ah,1h ; Keyboard input subprogram
INT 21H ; Call the subprogram to get character input and
stored in AL
Carriage Return ASCII (0DH) is the control character to bring the cursor to the start of a line.
;display Return
mov dl, 0dh
mov ah, 2h
int 21h ; display Carriage Return

18 Experiment 3 – Register, Input/Output Characters and Display Strings


Line-feed ASCII (oAh) is the control character that brings the cursor down to the next line on the screen.
;display Line-feed
mov dl, 0ah
mov ah, 2h
int 21h ; display Line Feed
ACTIVITY 3.4: WRITE A PROGRAM TO READ AND DISPLAY A CHARACTER.
;To read a character from keyboard
org 100h
.data
inMsg db "Enter a character ",'$'
newline db 0dh,0ah,'$'
outMsg db "User enter character ",'$'
.code
main proc

;To display input message


mov ax, @data
mov ds, ax
mov dx, offset inMsg
mov ah, 9
INT 21H

;Get input from keyboard


mov ah,1h
INT 21H
mov bl, al

;Move cursor to new line


mov ax, @data
mov ds, ax
mov dx, offset newline
mov ah, 9
INT 21H

;To display ouput message


mov ax, @data
mov ds, ax
mov dx, offset outMsg
mov ah, 9
INT 21H

;To diplay character placed in dl


mov dl, bl
mov ah,2h
INT 21H

mov ax,4C00h
INT 21H
main endp
end main

19 Experiment 3 – Register, Input/Output Characters and Display Strings


Execute and write the outcome of Activity 3.3 here: -

STRING OUTPUT
 A string is a list of characters treated as a unit.
 In 8086 assembly language, single or double quotes may be used to denote a string constant.
 For Defining String Variables following 3 definitions are equivalent ways of defining a string "abc":
 var1 db "abc" ; string constant
 var2 db ‘a’, ‘b’, ‘c’ ; character constants
 var3 db 97, 98, 99 ; ASCII codes
 The first version simply encloses the string in quotes. (preferred method)
 The second version defines a string by specifying a list of the character constants that make up the
string.
 The third version defines a string by specifying a list of the ASCII codes that make up the string
 In order to display string using MS-DOS subprogram (number 9h), the string must be terminated with
the ‘$’ character.
 In order to display a string we must know where the string begins and ends.
 The beginning of string is given by obtaining its address using the offset operator.
 The end of a string may be found by either knowing in advance the length of the string or by storing a
special character at the end of the string.

20 Experiment 3 – Register, Input/Output Characters and Display Strings


EXERCISE 3.1 [4]
Write a program that input a character from user is in lowercase, the program will convert it to uppercase
and will display it on console after conversion. Write program and outcome here: -
Hint: - The ASCII codes for lowercase letters (a-z) are 97-122. In order to convert a lowercase letter to
uppercase letter, just subtract 32 from its ASCII code.

21 Experiment 3 – Register, Input/Output Characters and Display Strings


EXERCISE 3.2 [3]

Write a program that input a character from user. The program will display it ten times on screen in newline.
Write program and outcome here: -

22 Experiment 3 – Register, Input/Output Characters and Display Strings


EXERCISE 3.3 [3]

Write a program that will display uppercase letters (A-Z), using loop on new line.

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

23 Experiment 3 – Register, Input/Output Characters and Display Strings


EXPERIMENT 4 – INPUT/ OUTPUT SINGLE AND MULTI-DIGIT NUMBER

OBJECTIVES
 To learn about signed and unsigned numbers
 Practice how to Input, Output single and multi-digit number.
 Addition and subtraction of numbers
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
CHARACTER STORAGE
 Computer stores binary numbers, so how character ‘A’ and ‘$’ are stored.
 Computer uses character encoding scheme that translate numbers into letters.
 Well-known scheme is ASCII (American Standard code for Information Interchange)
 ASCII is 7 bit code with range 0 to 127.
SIGNED NUMBERS
 Signed byte uses only 7 bits for its magnitude.
 Highest bit is reserved for the sign, where 0 indicates positive and 1 indicates negative.
Example:
11110110 = - 10
00001010 = + 10
TWO’S COMPLEMENT NOTATION
 Negative numbers use Two’s complement representation.
 Removes the need for separate digital circuits to handle both addition and subtraction.
Example: A – B = A + (-B)
To find Twos complement, toggle all bits and add 1.
00000001 = + 1
11111110 (toggle all bits)
+ 1 (add 1)
---------------
11111111 = - 1

24 Experiment 4 – Input/Output Single and Multi-digit Number


Example:
00001010 = + 10
11110101 (toggle all bits)
+ 1 (add 1)
---------------
11110110 = - 10
ACTIVITY 4.1
Show the working of following instruction by taking the two’s complement

 9–6 _________ - ____________ = ____________


 146 – 9 _________ - ____________ = ____________
TWO’S COMPLEMENT OF HEXADECIMAL
 To form the two’s complement of a hexadecimal integer, reverse all bits and add 1
 Easy way to reverse the bits of a hexadecimal digit is to subtract the digit from 15.
Example:
6A3D --> 95C2 + 1 --> 95C3
ACTIVITY 4.2:
95C3 --> -------- + 1 -->--------
DE10 --> -------- + 1 --> -------
ACTIVITY 4.3: ADDITION OF TWO SINGLE DIGITS NUMBERS
.data
Num1 db 0
Num2 db 0
msgNum1 db "Enter the 1st No: $"
msgNum2 db 10,13,"Enter the 2nd No: $"
msgAns db 10,13,"The answer is: $"

.code
;========================================
main proc
mov ax, @data
mov ds, ax
mov dx,offset msgNum1
mov ah, 09h
int 21h

mov ah, 1 ; accept 1 char from keyboard


int 21h
sub al,30h ; convert to number
mov Num1,al

;=======================================
mov ax, @data
mov ds, ax

25 Experiment 4 – Input/Output Single and Multi-digit Number


mov dx,offset msgNum2
mov ah, 09h
int 21h
mov ah, 1 ;accept 1 char from keyboard
int 21h
sub al,30h ;convert to number
mov Num2,al

;++++++++++++++++++++++++++++++++++++++++++
;ADD Num1,Num2 is illegal

mov bh, Num1


add bh, Num2

add bh, 30h ;convert back to ASCII equiv


mov ax, @data
mov ds, ax
mov dx,offset msgAns ;some message
mov ah, 09h
int 21h
mov dl,bh
mov ah,2 ;print answer
int 21h

mov ah,00h ;pause.... resume when press any key


int 16h
main endp
end main
Execute and write output of Activity 4.3 here: -

26 Experiment 4 – Input/Output Single and Multi-digit Number


ACTIVITY 4.4: ADDITION OF TWO MULTI-DIGITS NUMBERS
include emu8086.inc ;include library
.data
num1 dw 0
num2 dw 0
; get the multi-digit signed number from the keyboard, and store the
; result in cx register:
call scan_num ; Get first number

mov num1, cx ; store first number:

call scan_num ; Get second number

mov num2, cx ; store second number:

mov ax, num1 ; Can not add two memory variables


add ax, num2 ; add num2 in ax and store result in ax

call print_num ; print ax value.

DEFINE_PRINT_NUM ; used by print_num proc


DEFINE_PRINT_NUM_UNS ; used by print_num proc
DEFINE_SCAN_NUM ; used by scan_num proc
END
Execute and write output of Activity 4.4 here: -

27 Experiment 4 – Input/Output Single and Multi-digit Number


Exercise 4.1: Modify above program to format the output. [3]
Enter first number: 25

Enter second number: 50

The sum of two number is : 75

Note: - GOTOXY col, row- macro to sets cursor position.

Write code and outcome here: -

28 Experiment 4 – Input/Output Single and Multi-digit Number


EXERCISE 4.2 [3]

Write a program that reads five unsigned integers from user and display sum of those five integers on the
screen. Write program and outcome here: -

29 Experiment 4 – Input/Output Single and Multi-digit Number


EXERCISE 4.3 [4]

Write a program using instruction shown in table and fill in the table with the offsets of the instructions in the
code segment.

Instruction Segment and Offset

mov ax, number

add ax, 158

mov sum, ax

mov ax, 0

Write program and outcome here: -

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

30 Experiment 4 – Input/Output Single and Multi-digit Number


EXPERIMENT 5 - EXPLORING FLAG REGISTER AND DEFINING VARIABLES

OBJECTIVES
 Defining data word and data byte variable
 Identification of Flag register
 Type of flag register
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
IDENTIFIER
An identifier is a programmer-chosen name. It might identify a variable, a constant or a procedure. Things to
remember when creating an identifier
 Length between 1 and 247 characters.
 They are not case sensitive.
 The first character must be a letter (A..Z, a..z), underscore (_), @ , ?, or $. Subsequent characters may
also be digits.
 An identifier cannot be the same as an assembler reserved word
INSTRUCTIONS
An instruction is a statement that becomes executable when a program is assembled. Instruction contains four
basic parts
 Label (Optional)
 Instruction Mnemonic (required)
 Operands (normally required)
 Comment (optional)
[label:] mnemonic operand(s) [;comment]
DATA TYPES
Type Usage
BYTE 8-bit unsigned integer
SBYTE 8-bit signed integer
WORD 16-bit unsigned integer
SWORD 16-bit signed integer
DWORD 32-bit unsigned integer
SDWORD 32-bit signed integer
QWORD 64-bit integer

31 Experiment 5 – Exploring Flag Register and Defining Variables


DEFINING DATA
Data Definition Statement
 Data definition statements create variables based on intrinsic data types.
Syntax
[name] directive initializer
[,initializer]...
Example
count db 0 and num dw
146
 Emu8086 supports two types of variables: BYTE and
WORD.
Note:- The lower byte is stored at lower memory address. Consider for example, storing 0xA2B1C3D4 in
main memory.
MULTIPLE INITIALIZERS
 If multiple initializers are used in the same data definition, its label refers only to the offset of the first
initializer
Example
list db 10,20,30,40
list1 db 10, 32, 41h, 00100010b
list2 db 0Ah, 20h, 'A', 22h
Note:- assume list is located at offset 0000. If so, the value 10 is at offset 0000, 20 is at offset 0001, 30 is at
offset 0002, and 40 is at offset 0003
DEFINING STRING
To define a string of characters, enclose them in single or double quotation marks. Examples: -
 greeting1 db "Good afternoon",0
 greeting2 db 'Good night',0
 greeting1 db "Welcome “,0dh,0ah,0
Note: The hexadecimal codes 0Dh and 0Ah are alternately called CR/LF (carriage-return line-feed), 0 is
called a null-terminated string.
DUP OPERATOR
The DUP operator allocates storage for multiple data items
 number DUP ( value(s) )
 number - number of duplicate to make (any constant value).
 value - expression that DUP will duplicate.
Example

32 Experiment 5 – Exploring Flag Register and Defining Variables


ze db 20 DUP(0) ; 20 bytes, all equal to zero
ui db 20 DUP(?) ; 20 bytes, uninitialized
st db 4 DUP("STACK") ; 20 bytes:
"STACKSTACKSTACKSTACK”
ACTIVITY 4.1
Define and Display 8-bit Numbers. Write a program to do the following:
 Use the DB directive to define the following list of numbers and name it array.
31h, 32h, 33h, 34h
 Use the int 21h to display the array.
Execute and display outcome here: -

FLAG REGISTER
 Determines the current state of the processor.
 They are modified automatically by CPU after mathematical operations, this allows to determine the
type of the result, and to determine conditions to transfer control to other parts of the program.
 Generally, you cannot access these registers directly.
TYPES OF FLAGS: There are two types of flags
 Status Flags The Status flags reflect the outcomes of arithmetic and logical operations performed by
the CPU. You do not need to alter the value yourself.
 Carry flag (CF) is set when the result of an unsigned arithmetic operation is too large to fit into
the destination (unsigned overflow). For example, when you add bytes 255+1 (result is not in range
0...255). When there is no overflow this flag is set to 0. This flag uses bit 0
 Parity flag (PF) is set if the least-significant byte in the result contains an even number of 1 bits.
Even if result is a word only 8 low bits are analyzed. This flag uses bit 2
 Auxiliary Carry flag (AC) is set when an arithmetic operation causes a carry from bit 3 to bit 4.
(Unsigned over flow for low nibble -4 bits) This flag uses bit 4
 Zero flag (ZF) is set when the result of an arithmetic or logical operation generates a result of
zero. For none zero result this flag is set to 0. This flag uses bit 6
 Sign flag (SF) is set when the result of an arithmetic or logical operation generates a negative
result. Actually, this flag take the value of the most significant bit. This flag uses bit 7

33 Experiment 5 – Exploring Flag Register and Defining Variables


 Overflow flag (OF) is set when the result of a signed arithmetic operation is too large or too small
to fit into the destination (signed overflow). For example, when you add bytes 100 + 50 (result is
not in range -128...127). This flag uses bit 11
 Control Flags Control flags control the CPU’s operation
 Direction Flag (DF) this flag is used by some instructions to process data chains, when this flag
is set to 0- the processing is done forward, when this flag is set to 1 the processing is done
backward.
This flag
uses bit 10
 Interrupt
enable
Flag (IF)
when this
flag is set
to 1 CPU
reacts to
interrupts from external devices. This flag uses bit 9
 TF (Trap Flag) This flag is used to Control Permits operation of the processor in single-step mode.
This flag uses bit 8
EXERCISE 4.1 [3]
For each add instruction in this exercise, assume that AX contains the given contents before the instruction is
executed. Give the contents of AX as well as the values of the CF, OF, SF, PF, AF and ZF after the instruction
is executed. All numbers are in hex. (Hint: add ax, 45 adds 45 to the contents of register ax and stores the
result back in ax)
Contents of AX Instruction Contents of AX CF OF SF PF AF ZF
(Before) (After)

0045 add ax, 45


FF45 add ax, 45
0045 add ax, -45
FF45 add ax, -45
FFFF add ax, 1

34 Experiment 5 – Exploring Flag Register and Defining Variables


EXERCISE 4.2 [3]
Calculate the range of signed and unsigned numbers that db and dw directives can specify.

EXERCISE 4.3 [4]


Find the initial values that the assembler will generate for each of the directives below.
byte1 db 10110111b ____________
byte2 db 35q ____________
byte3 db 0E8h ____________
byte4 db 150 ____________
byte1 db -91 ____________
byte1 db ‘K’ ____________
byte1 db ‘k’ ____________
byte1 db “Ali’s book” ___________________________
byte1 db 5 DUP("< >") ___________________________
byte1 dw 1000000 ____________

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

35 Experiment 5 – Exploring Flag Register and Defining Variables


EXPERIMENT 6 – ARITHMETIC INSTRUCTIONS
OBJECTIVES
 Use of Arithmetic Operators
 Operation with signed and unsigned values
 Solve mathematical equations
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
INTEGER ADDITION AND SUBTRACTION INSTRUCTIONS (ADD, SUB)
In arithmetic instruction these types of operands are supported:
REG, memory
memory, REG
REG, REG
memory, immediate
REG, immediate
REG:AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: variable, [BX], etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
The integer at source is added to the integer at destination and the sum replaces the old value at destination.
The SF, ZF, OF, CF, PF and AF flags are set according to the value of the result of the operation.
MULTIPLICATION INSTRUCTIONS (MUL, IMUL)
There are two versions of multiplication instructions, MUL instruction is for unsigned multiplication and
IMUL instruction is for signed multiplication. These types of operands are supported by MUL and IMUL:
REG
memory
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: variable, [BX], etc...
MUL and IMUL instructions affect CF, OF flags only:
MUL -Unsigned multiply:
When operand is a byte:
AX = AL * operand.
When operand is a word:
(DX AX) = AX * operand.
Example: Multiply AX by 10h (16 bit)
.data
Var1 dw 2000h
Var2 dw 0010h
.code
36 Experiment 6 – Arithmetic Instructions
MOV ax, Var1
MUL var2 ;DX=0002h, AX=0000h
IMUL -Signed multiply:
When operand is a byte:
AX = AL * operand.
When operand is a word:
(DX AX) = AX * operand.
Example: Multiply AX by 10h (8 bit)
.code
MOV al,1
MOV bl,-1
IMUL bl ;AX=1111111111111111b
;CF=0, OF=0
ACTIVITY 6.1 WORD MULTIPLICATION
include emu8086.inc
.data
Var1 dw 2000h
Var2 dw 0010h
.code
MOV ax,Var1
MUL Var2 ;DX=0002h,
; AX=0000h
call print_num
DEFINE_PRINT_NUM ; used by print_num proc
DEFINE_PRINT_NUM_UNS ; used by print_num proc
end
Execute and write outcome here: -

DIVISION INSTRUCTIONS (DIV, IDIV)


There are two versions of division instructions, DIV instruction is for unsigned multiplication and IDIV
instruction is for signed multiplication. These types of operands are supported by DIV and IDIV:
REG
memory
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: variable, [BX], etc...

37 Experiment 6 – Arithmetic Instructions


The dividend is always double the size of divisor. The implicit dividends for these instructions are as bellow

DIV -Unsigned divide:


When operand is a byte:
AL = AX / operand
AH = remainder (modulus). .
When operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus).
Example: 8 bit division (83h/2=40h, remainder=3)
.code
MOV ax, 0083h ; dividend
MOV bl,2 ; divisor
DIV bl ; AL=40h, AH=03h

IDIV -Signed divide:


When operand is a byte:
AL = AX / operand
AH = remainder (modulus). .
When operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus). .
Example: Divide -48 by 5
.code
MOV ah, 0
MOV al,-48
MOV bl, 5 ; AX=00D0h
IDIV bl ; AL=40h, AH=03h
Result would be wrong. To correct this error, you will have used conversion instruction CBW (convert byte
to word) listed bellow.

38 Experiment 6 – Arithmetic Instructions


ACTIVITY 6.2 USE OF CBW
include emu8086.inc
.code
MOV AX, 0 ; AH = 0, AL = 0
MOV AL, -5 ; AX = 000FBh (251)
CBW ; AX = 0FFFBh (-5)
call print_num
DEFINE_PRINT_NUM ; used by print_num proc
DEFINE_PRINT_NUM_UNS ; used by print_num proc
Execute and write outcome here: -

ACTIVITY 6.3 SIMPLE PROGRAM FOR CELSIUS – FAHRENHEIT CONVERSION


The working formula is f = (9/5) *c + 32
.DATA
cTemp DW 35 ; Celsius temperature
fTemp DW ? ; Fahrenheit temperature
f1 dw 0
.CODE
main PROC
mov ax, cTemp ; start with Celsius temperature
mov f1,9
imul f1 ; C*9
add ax,2 ; rounding factor for division
mov bx,5 ; divisor
cwd ; prepare for division
idiv bx ; C*9/5
add ax,32 ; C*9/5 + 32
mov fTemp, ax ; save result
main ENDP
END

39 Experiment 6 – Arithmetic Instructions


Execute and write outcome here: -

40 Experiment 6 – Arithmetic Instructions


EXERCISE 6.1 [5]
Write a program to use the SCAN_NUM to input prompt for values for three variables x, y and z and the PRINT_NUM
to display an appropriate label and value of the expression x – y + 2z – 1. Write code and outcome here: -

41 Experiment 6 – Arithmetic Instructions


EXERCISE 6.2 [5]
Write an assembly language program that prompts for and inputs the length, width, and height of a box and
calculates and displays its surface area.
surface area = 2 * (length * width + length * height + width * height)
Write code and outcome here: -

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

42 Experiment 6 – Arithmetic Instructions


EXPERIMENT 7 – BOOLEAN OPERATORS
OBJECTIVES
 Learn how to use of Boolean operators
 Learn which flag bits are affected by different Boolean instructions

TIME REQUIRED : 3 hrs


PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
AND INSTRUCTION
AND instruction performs Boolean (bitwise) AND operation b/w each pair of matching bits in two operands
and place the result in dest. operand.
AND dest, source
AND instruction can be b/w
AND reg, reg
AND reg, mem
AND reg, imm
AND mem, reg
AND mem,imm
Note:- Operand can be 8, 16 or 32 bits and they must be of same size.
Flags: It always clears the overflow and carry flags. It modifies SIGN, ZERO and PARITY flags according
to the value of the destination operand.
OR INSTRUCTION
OR instruction performs boolean (bitwise) OR operation b/w each pair of matching bits in two operands and
place the result in dest. operand.
OR dest, source
OR instruction can be b/w
OR reg, reg
OR reg, mem
OR reg, imm
OR mem, reg
OR mem,imm
Note:- Operand can be 8, 16 or 32 bits and they must be of same size.
flags: It always clears the overflow and carry flags. It modifies SIGN, ZERO and PARITY flags according
to the value of the destination operand.
XOR INSTRUCTION
XOR instruction performs boolean (bitwise) XOR operation b/w each pair of matching bits in two operands
and place the result in dest. operand.
XOR dest, source

43 Experiment 7 – Boolean Operators


Note:- XOR instruction uses the same operand combinations as AND and OR instructions.
Flags: It always clears the overflow and carry flags. It modifies SIGN, ZERO and PARITY flags according
to the value of the destination operand.
NOT INSTRUCTION
NOT instruction toggles all bits in an operand.
NOT reg
NOT mem
NOT instruction does not affect any flags.
NEG INSTRUCTION
Make operand negative (two's complement). Actually, it reverses each bit of operand and then adds 1 to it.
For example, 5 will become -5, and -2 will become 2.
These types of operands are supported:
NEG reg
NEG mem
FLAGS: CF, ZF, SF, OF, PF, AF flags are affected by this instruction
ACTIVITY 7.1: TO SUBTRACT TWO NUMBERS USING NEG OPERATOR
org 100h
.data
num1 db 10
num2 db 5
.code
neg num2
mov al, num1
add al,num2
Execute and write outcome here: -

TEST INSTRUCTION
TEST instruction performs an implied AND operation b/w each pair of matching bits in two operands and set
the flag accordingly.
TEST operand1, operand2
The difference b/w TEST and AND instruction is that TEST Does not modify the destination operand. TEST
instruction permits the same operand combinations as the AND instruction.
Flags: CF, ZF, SF, OF, PF, AF.

44 Experiment 7 – Boolean Operators


CMP INSTRUCTION
CMP (compare) instruction performs an implied subtraction of a source from destination operand i.e. Subtract
second operand from first for flags only. Neither operand is modified. CMP instruction can be b/w
CMP reg, reg
CMP reg, mem
CMP reg, imm
CMP mem, reg
CMP mem, imm
CMP instructions affect flags only and do not store a result (these instructions are used to make decisions
during program execution).
Flags: - CF, ZF, SF, OF, PF, AF.
CMP Result ZF CF
Destination < Source 0 1
Destination > Source 0 0
Destination = Source 1 0
Examples:
Destination < Source:
mov ax, 5
cmp ax, 10 ;CF = 1
Destination = Source
mov ax, 1000
mov cx, 1000
cmp cx, ax ; ZF = 1
Destination > Source
mov si, 105
cmp si, 0 ; ZF = 0 and CF = 0

EXERCISE 7.1 [2.5]


In the following instruction sequence. Show the changed value of AL where indicated, in binary:
mov a1, 00001111b
and al, 00111011b ;a
mov al, 6Dh
and al, 4Ah ;b
mov al, 00001111b
or al, 61h ;c
mov al, 94h
xor al, 37h ;d

45 Experiment 7 – Boolean Operators


EXERCISE 7.2 [2.5]
In the following instruction sequence, show the changed value of AL where indicated, in hexadecimal:
mov a1, 7Ah
not a1 ;a
mov a1, 3Dh
and al, 74h ;b
mov al, 9Bh
or al, 35h ;c
mov al, 72h
xor al, odch ;d

EXERCISE 7.3 [2.5]


In the following instruction sequence, show the values of the Carry, Zero, and Sign flags where indicated:
mov al,00001111b
test al,00000010b ; a. CF= ZF= SF=
mov al,00000110b
cmp al,00000101b ; b. CF= ZF= SF=
mov al,00000101b
cmp al,00000111b ; c. CF= ZF= SF=

46 Experiment 7 – Boolean Operators


EXERCISE 7.4 [2.5]
Write a single instruction that clears the high 8 bits of AX and does not change the low 8 bits.

EXERCISE 7.5 [2.5]


Write a single instruction that sets the high 8 bits of AX and does not change the low 8 bits.

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

47 Experiment 7 – Boolean Operators


EXPERIMENT 8 – DECISION INSTRUCTIONS

OBJECTIVES
 Learn how to control the flow of program.
 Learn how to transfer control conditionally and unconditionally.
 Learn how to implement if statement

TIME REQUIRED : 3 hrs


PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
UNCONDITIONAL JUMP INSTRUCTIONS
The JMP instruction causes an unconditional transfer to a target location inside the code segment. This is
similar to goto in a High Level Language. The location must be identified by a code label which is translated
by the assembler into an offset. The syntax is:
JMP targetLabel
When the CPU executes this instruction, the offset of target Label is moved into the instruction pointer causing
execution to immediately continue at the new location.
ACTIVITY 8.1: PRINT THE SERIES 1, 2, 3, … N
include emu8086.inc
.DATA
.CODE
main PROC
mov bx,0 ; number := 0
mov ax,0 ; sum := 0
forever: inc bx ; add 1 to number
mov ax, bx ; add number to sum
call print_num
print ‘,’
jmp forever ; repeat

main ENDP

DEFINE_PRINT_NUM ; used by print_num proc


DEFINE_PRINT_NUM_UNS ; used by print_num proc
END main
Execute and write outcome here: -

48 Experiment 8 – Decision Instructions


CONDITIONAL JUMP INSTRUCTION
A conditional jump instruction branches to a destination label when a flag condition is true. If the flag
condition is false, the instruction immediately following the conditional jump is executed. The syntax is:
Jcond destination
cond refers to a flag condition, identifying the state of one or more flags. For example:
jc ;jump if carry (Carry flag set)
jnc ;jump if not carry (Carry flag clear)
jz ;jump if zero (Zero flag set)
jnz ;jump if not zero (Zero flag clear)
Flags are set by arithmetic, comparison, and Boolean instructions. Each conditional jump instruction checks
one or more flags, returning a result of true or false. If the result is true, the jump is taken; otherwise, the
program skips the jump and continues to the next instruction. Most common way to set flags for conditional
branches is to use compare instruction that has the following format:
cmp operand1, operand2

Instruction Description signed Flags

JO Jump if overflow OF = 1

JNO Jump if not overflow OF = 0

JS Jump if sign SF = 1

JNS Jump if not sign SF = 0

JE Jump if equal ZF = 1
JZ Jump if zero

JNE Jump if not equal ZF = 0


JNZ Jump if not zero

JB Jump if below unsigned CF = 1


JNAE Jump if not above or equal
JC Jump if carry

JNB Jump if not below unsigned CF = 0


JAE Jump if above or equal
JNC Jump if not carry

JBE Jump if below or equal unsigned CF = 1 or ZF = 1


JNA Jump if not above

JA Jump if above unsigned CF = 0 and ZF = 0


JNBE Jump if not below or equal

JL Jump if less signed SF <> OF


JNGE Jump if not greater or equal

49 Experiment 8 – Decision Instructions


Instruction Description signed Flags

JGE Jump if greater or equal signed SF = OF


JNL Jump if not less

JLE Jump if less or equal signed ZF = 1 or SF <> OF


JNG Jump if not greater

JG Jump if greater signed ZF = 0 and SF = OF


JNLE Jump if not less or equal

JP Jump if parity PF = 1
JPE Jump if parity even

JNP Jump if not parity PF = 0


JPO Jump if parity odd

JCXZ Jump if CX register is 0 CX = 0


JECXZ Jump if ECX register is 0 ECX = 0
Example:
Let's implement the following pseudo-code in include emu8086.inc
assembly language. .DATA
smallCount dw 1
if value < 10 then largeCount dw 1
add 1 to smallCount; .CODE
print smallCount main PROC
else mov ax,@data
add 1 to largeCount; mov ds,ax
mov bx,5 ; number := 0
print largeCount cmp bx, 10
end if; jnl elseLarge
mov dx,offset smallcount
inc smallCount
If the value is in BX and smallCount and largeCount
mov ax, smallCount
are in memory, the corresponding assembly language call print_num
coding is shown below: jmp endValueCheck
elseLarge:
mov dx,offset largecount
cmp bx, 10 inc largeCount
jnl elseLarge mov ax, smallCount
inc smallCount call print_num
endValueCheck:
jmp endValueCheck
main ENDP
elseLarge: DEFINE_PRINT_NUM
inc largeCount DEFINE_PRINT_NUM_UNS
endValueCheck: END main

Execute and write outcome here: -

50 Experiment 8 – Decision Instructions


Example: include emu8086.inc
.DATA
As another example, consider the following total dw 119
value dw 1200
pseudo-code: .CODE
if (total >= 100) or main PROC
(count = 10) then mov ax,@data
add value to mov ds,ax
total; mov cx,10 ; number := 10
cmp total, 100
end if; jge addValue
cmp cx, 11

jne endAddCheck
Assuming total and value are in memory and addValue:
count in CX, the assembly code is shown below: mov dx,offset value
mov bx, value
mov dx,offset total
cmp total, 100 add total, bx
jge addValue mov ax, total
cmp cx, 10 call print_num
jne endAddCheck endAddCheck:
main ENDP
addValue: DEFINE_PRINT_NUM
mov bx, value DEFINE_PRINT_NUM_UNS
add total, bx END main
endAddCheck:

Execute and write outcome here: -

51 Experiment 8 – Decision Instructions


Activity 8.2: Print the larger of two integers
include emu8086.inc
.DATA
.CODE
main PROC
mov bx,25 ; number := 0
mov ax,10 ; sum := 0

mov dx, ax ; assume ax is larger


cmp ax, bx ; if AX >= BX then
jae L1 ; jump to L1
mov dx, bx ; else move bx to dx because it is larger than ax
L1:
print "The larger number is= "
mov ax, dx
call print_num

main ENDP

DEFINE_PRINT_NUM ; used by print_num proc


DEFINE_PRINT_NUM_UNS ; used by print_num proc
END main
Execute and write outcome here: -

52 Experiment 8 – Decision Instructions


EXERCISE 8.1: [2]
Write a program to print the sum of series 1+3+6+ … n and also print the value of ax and bx after each
step.
Note:- ax contains the sum of series and bx used as counter.

EXERCISE 8.2: [2]


Write a program to print the smallest number from three integers.

53 Experiment 8 – Decision Instructions


EXERCISE 8.3: [2]
Write an assembly language program that will repeatedly prompt for a number. After each number is entered,
display the sum and average of all the numbers entered so far.

EXERCISE 8.4: [2]


Write an assembly language code that implements the following if statement. Input an integer from user and
store in DX.
if (value < -1000) or (value > 1000) then
value := 0;
end if;

54 Experiment 8 – Decision Instructions


EXERCISE 8.5: [2]
Write an assembly language code that takes input an integer (less than 10) from user and print it in English
word e.g.
Please Input an integer (less than 10) = 6
You have Entered = Six

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

55 Experiment 8 – Decision Instructions


EXPERIMENT 9 – IMPLEMENTATION OF LOOP STRUCTURES

OBJECTIVES
 Learn how to repeat execution of a program fragment.
 Learn the working of loop instruction
 Learn implementation of while, until, and for loops.

TIME REQUIRED : 3 hrs


PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
LOOP INSTRUCTION
The LOOP instruction provides a simple way to repeat a block of statements a specific number of times. CX
is automatically used as a counter and is decremented each time the loop repeats. Its syntax is:
LOOP destination
The execution of the LOOP instruction involves two steps: First, it subtracts 1 from CX. Next, it compares
CX to zero. If CX is not equal to zero; a jump is taken to the label identified by destination. Otherwise, if CX
equals zero, no jump takes place and control passes to the next instruction. In the following example, we add
1 to AX each time the loop repeats. When the loop ends, AX = 5 and CX = 0:
mov ax, 0
mov cx,5
L1:
inc ax
loop L1
ACTIVITY 9.1: PRINT THE SERIES 1, 2, 3, … N
include emu8086.inc
.DATA
.CODE
main PROC
mov bx,0 ; number := 0
mov ax,0 ; sum := 0
forever: inc bx ; add 1 to number
mov ax, bx ; add number to sum
call print_num
print ‘,’
jmp forever ; repeat

main ENDP

DEFINE_PRINT_NUM ; used by print_num proc


DEFINE_PRINT_NUM_UNS ; used by print_num proc
END main

56 Experiment 9 – Implementation of Loop Structures


Implementation of while Loop
A while loop can be indicated by the following pseudocode design:
while continuation condition loop
... { body of loop }
end while;
A while loop is a pre-test loop – the continuation condition, a Boolean expression, is checked before the loop
body is executed. Whenever it is true the loop body is executed and then the continuation condition is checked
again. When it is false execution continues with the statement following the loop. It may take several 80x86
instructions to evaluate and check a continuation condition. In 80x86 implementation of a while loop follows
a pattern much like this one:
while1: . ; code to check Boolean expression
.
.
body: . ; loop body
.
.
jmp while1 ; go check condition again
endWhile1:
Example:
include emu8086.inc
consider the following code .DATA
while (sum < 100) sum dw 0
loop .CODE
sum=sum+count; main PROC
count=count+1; mov ax,@data
mov ds,ax
end while; mov cx, 0
whileSum: cmp sum, 100
Assuming sum in memory and count in CX, jnl endWhileSum
add sum, cx
the corresponding assembly code follows: inc cx
mov ax, sum
whileSum: cmp sum, call print_num
print 10
100
print 13
jnl endWhileSum jmp whileSum
add sum, cx endWhileSum:
inc cx main ENDP
jmp whileSum DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
endWhileSum:
END main

IMPLEMENTATION OF FOR LOOP


A for loop can be indicated by the following pseudocode design:
for index := initialValue to finalValue loop
... { body of loop }
end for;
A for loop is a counter-controlled loop that executes once for each value of a loop index (also known as loop
counter) in a given range. Often the number of times the body of a loop must be executed is known in advance,
57 Experiment 9 – Implementation of Loop Structures
either as a constant that can be coded when a program is written, or as the value of a variable that is assigned
before the loop is executed. In 80x86 implementation of a for loop follows a pattern much like this one:
mov cx, number ; number of iterations
jcxz endFor ; skip loop if number = 0
forIndex: . ; loop body
.
.
loop forIndex ; repeat body number times

Example:
Consider the following code
for index := 1 to 50 loop
... { loop body using index }
end for;
BX can be used to store index counting from 1 to 50, while the CX register counts down from 50 to 1.
The corresponding assembly code is as follows:
mov bx, 1 ; index := 1
mov cx, 50 ; number of iterations
forNbr: . ;
. ; use BX for index
.
inc bx ; add 1 to index
loop forNbr ; repeat
IMPLEMENTATION OF UNTIL LOOP
An until loop is a post-test loop – the condition is checked after the body of loop is executed. In general, an
until loop can be represented as follows:
repeat
... { body of loop }
until termination condition;
Termination condition is checked after the loop body is executed. If it is true, execution continues with the
statement following until loop. Otherwise, the loop body is executed again. Thus, loop body is executed at
least once. An 80x86 implementation of until loop follows:
until: . ; start of loop body
.
.
body: . ; code to check termination condition
enduntil:
Example:
consider the following code
repeat
add 2*count to sum;
add 1 to count;
until (sum > 1000);

58 Experiment 9 – Implementation of Loop Structures


If the sum references a word in memory, the following 80x86 code implements the pseudocode, using the CX
register for count.
repeatLoop: add sum, cx
inc cx
cmp sum, 1000
jng repeatLoop
endUntilLoop:

EXERCISE 9.1 [2.5]


Write a program to add first ten numbers. The result should be stored in BL register.

59 Experiment 9 – Implementation of Loop Structures


EXERCISE 9.2 [2.5]
Implement the following loop in assembly language
while (sum < 1000) and (count < 24) loop
. . . {loop body}
end while;
whileSum: cmp sum, 1000 ; sum < 1000?
jnl endWhileSum ; exit if not
cmp cx, 24 ; count <= 24?
jnle endWhileSum ; exit if not
. ; loop body
.
.
jmp whileSum ; go check condition
again
endWhileSum:

60 Experiment 9 – Implementation of Loop Structures


EXERCISE 9.3 [2.5]
Write an assembly language program that will prompt for an integer n, compute the factorial of n and display
it on screen.

EXERCISE 9.4 [2.5]


Write an assembly language program that will prompt for an integer n and the print the table of n upto 20 on
screen

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

61 Experiment 9 – Implementation of Loop Structures


EXPERIMENT 10 – ARRAY PROCESSING AND INDIRECT ADDRESSING

OBJECTIVES
 Learn how to define and access arrays.
 Learn how to search and modify the elements of an array.
 Learn how different addressing modes are used.

TIME REQUIRED : 3 hrs


PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
ARRAY can be seen as chains of variables. A text string is an example of a byte array; each character is
presented as an ASCII code value (0 to 255). Here are some array definition examples:
a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h
b DB 'Hello', 0
Array b is an exact copy of the array a, when compiler sees a string inside quotes it automatically converts it
to set of bytes.
You can access the value of any element in array using
square brackets, for example:
MOV AL, a[3]
You can also use any of the memory index registers BX, SI, DI, BP, for example:
MOV SI, 3
MOV AL, a[SI]
Programs often use arrays to store collections of data values. Loops are commonly used to manipulate the data
in arrays. Storage for an array can be reserved using the DUP directive in the data segment of a program. For
example:
a1 DW 25, 47, 15, 50, 32 ; creates array of 5 words with default
values
a2 DW 1000 DUP (?) ; creates array of 1000 uninitialized
words.
There is LEA (Load Effective Address) instruction and alternative OFFSET operator. Both OFFSET and LEA
can be used to get the offset address of the variable. LEA is more powerful because it also allows you to get
the address of an indexed variable. Getting the address of the variable can be very useful in some situations,
for example when you need to pass parameters to a procedure. It has the format:
LEA destination, source
The destination is usually a 32-bit general register; the source is any reference to memory. The address of the
source is loaded into the register. Contrast this with the instruction mov destination, source where the value at
the source address is copied to the destination.
62 Experiment 10 – Array Processing and Indirect Addressing
In order to say the compiler about data type, these prefixes should be used:
BYTE PTR is for byte or WORD PTR is for word (two bytes).
For example:
BYTE PTR [BX] ; byte access.
WORD PTR [BX] ; word access.
Example 1: Use of LEA with memory variables
ORG 100h
.data
VAR1 DB 22h
.code
MOV AL, VAR1 ; check value of VAR1 by moving it to
AL.
LEA BX, VAR1 ; get address of VAR1 in BX.
MOV BYTE PTR [BX], 44h ; modify the contents of VAR1.
MOV AL, VAR1 ; check value of VAR1 by moving it to
AL.
END
Note:- Please note that only these registers can be used inside square brackets (as memory pointers): BX,
SI, DI, BP.
ACTIVITY 10.1: HOW TO DEFINE AND ACCESS ARRAYS

org 100h
.data
array db 10h,20h,30h,40h
.code
main proc
mov al, array
mov bl, array+1
mov cl, array+2
mov dl,array+3
main endp
end main
Execute and write outcome here: -

63 Experiment 10 – Array Processing and Indirect Addressing


ACTIVITY 10.2 :
Suppose the n number of word integers stored in memory and Arr contains the numbers are stored in a word
in memory. Find average of number and then adding 10 to each number that is smaller than the average.

include emu8086.inc
.DATA
arr dw 25, 47, 15, 50, 32, 95 DUP (?)
n dw 5
.CODE
main PROC
mov ax, @data
mov ds, ax

; find sum and average


mov ax,0 ; sum := 0
lea bx,arr ; get address of Array
mov cx,n ; count := n
jcxz quit ; quit if no numbers

forCount1:
add ax,[bx] ; add number to sum
add bx,2 ; get address of next array elt
loop forCount1 ; repeat n times
print 10
print 13
call print_num

idiv n ; calculate average


print 10
print 13
call print_num

; add 10 to each array element below average


lea bx,arr ; get address of Array
mov cx,n ; count := n

forCount2: cmp [bx],ax ; number < average ?


jnl endIfSmall ; continue if not less
add PTR [bx], 10 ; add 10 to number
endIfSmall:
add bx,4 ; get address of next array elt
loop forCount2 ; repeat
print 10
print 13
call print_num

quit: mov ax, 0 ; exit with return code 0

main ENDP

DEFINE_PRINT_NUM ; used by print_num proc


DEFINE_PRINT_NUM_UNS ; used by print_num proc
end main
64 Experiment 10 – Array Processing and Indirect Addressing
Execute and write outcome here: -

EXERCISE 10.1 [3]


Write an assembly language program that will prompt for and input each number. Assume that no more than
10 numbers will be entered. Display the sum of the numbers, how many numbers were entered, the average
of the numbers, and the count of array entries that are greater than or equal to the average value.

65 Experiment 10 – Array Processing and Indirect Addressing


EXERCISE 10.2 [3]
Write an assembly language program that input a string of characters into charStr, by using GET_STRING.
Process this string as an array of characters, replacing each uppercase letter in charStr by its lowercase
equivalent and Display the updated string using PRINT_STRING method.

66 Experiment 10 – Array Processing and Indirect Addressing


EXERCISE 10.3 [4]
Write an assembly language program that inputs a collection of integers into an array and implements
sequential search for a particular integer entered by the user.

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

67 Experiment 10 – Array Processing and Indirect Addressing


EXPERIMENT 11 – OPERATIONS ON STACK

OBJECTIVES
 Learn how to use the stack.
 Learn how to perform different operation on stack.
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
STACK is an area of memory for keeping temporary data. The stack uses LIFO (Last in First Out) algorithm,
this means that if we push values one by one into the stack. Stack is used by CALL instruction to keep return
address for procedure, RET instruction gets this value from the stack and returns to that offset. Quite the same
thing happens when INT instruction calls an interrupt, it stores in stack flag register, code segment and offset.
IRET instruction is used to return from interrupt call.
We can also use the stack to keep any other data; there are two instructions that work with the stack:
PUSH - stores 16-bit value in the stack.
POP - gets 16-bit value from the stack.
Syntax for PUSH instruction:
PUSH REG
PUSH SREG
PUSH memory
PUSH immediate
REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, CS.
Memory: [BX], [BX+SI+7], 16 bits variable, etc...
Immediate: 5, -24, 3Fh, 10001101b, etc...
Syntax for POP instruction:
POP REG
POP SREG
POP memory
REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, (except CS).
memory: [BX], [BX+SI+7], 16 bits variable, etc...
The stack memory area is set by SS (Stack Segment) register, and SP (Stack Pointer) register. Generally,
operating system sets values of these registers on program start.
"PUSH source" instruction does the following:
Subtract 2from SP register.

68 Experiment 11 – Stack Operations


Write the value of source to the address SS:SP.
"POP destination" instruction does the following:
Write the value at the address SS:SP to destination.
Add 2to SP register.
Note: - The current address pointed by SS:SP is called the top of the stack.
ACTIVITY 11.1
org 100h
.code
main proc
MOV AX, 1234h
PUSH AX ; store value of AX in stack.
MOV AX, 5678h ; modify the AX value.
POP AX ; restore the original value of AX.
main endp
end main
Execute and write outcome here: -

ACTIVITY 11.2: EXCHANGING THE VALUES


org 100h
.code
main proc
MOV AX, 1212h ; store 1212h in AX.
MOV BX, 3434h ; store 3434h in BX
PUSH AX ; store value of AX in stack.
PUSH BX ; store value of BX in stack.
POP AX ; set AX to original value of BX.
POP BX ; set BX to original value of AX.
main endp
end main
Execute and write outcome here: -

69 Experiment 11 – Stack Operations


ACTIVITY 11.3 SAVING THE REGISTER VALUES
org 100h
.data
Message db "This is a Message.$"

.code
main proc
mov ax,48H
mov dx,49H
push ax ;save ax
push dx ;save dx
mov ah,9 ;function display string
mov dx,offset message ;dx points to the string
INT 21H ;call DOS
pop dx ;restore dx
pop ax ;restore ax
mov ax,4C00h
INT 21H
main endp
end main
Execute and write outcome here: -

70 Experiment 11 – Stack Operations


ACTIVITY 11.4: STRING HANDLING

include emu8086.inc
.data
buffer db 100 dup(?),0
size = $ - offset buffer

.code
mov ax, @data
mov ds,ax

; get string to ds:di


lea di, buffer ; buffer offset.
mov dx, size ; buffer size.
call get_string

putc 0Dh
putc 10 ; next line.

; print using macro:


print "You've entered: "

; print string in ds:si using procedure:


mov si, di
call print_string

DEFINE_GET_STRING
DEFINE_PRINT_STRING
end
Execute and write outcome here: -

71 Experiment 11 – Stack Operations


Exercise 11.1. [5]
Write an assembly language program to push the following values on stack. Show code and outcome here:
450, 0, 487, 101, 500, 0, 359, 0, 458

Now write a code to delete the items having quantity zero and update the stack. Show code and outcome here:

72 Experiment 11 – Stack Operations


Exercise 11.2. [5]
Write an assembly language program that input characters string and prints them in reverse order. Use a
stack. Show code and outcome here: -

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

73 Experiment 11 – Stack Operations


EXPERIMENT 12 – BIOS AND DOS INTERRUPTS PROGRAMMING

OBJECTIVES
 Lear how to use BIOS Interrupts in the 8086 processor
 Lear how to use DOS Interrupts in the 8086 processor
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
There are some extremely useful subroutines within BIOS and DOS that are available to the user through the
INT instruction. The INT instruction is somewhat like a FAR call. When it is invoked, it saves CS:IP and then
flags on the stack and goes to the subroutine associated with that interrupt. The INT instruction has the
following format:
INT xx ; the interrupt number xx can be 00 – FF
Since interrupts are numbered 00 to FF, this gives a total of 256interrupts in 80x86 microprocessors. From
these 256 interrupts, two are the most widely used: INT10 and INT 21. Each one can perform many functions.
You can find a list of these interrupts in emu8086 help tutorial (The List of Interrupts is given in a file). Before
the service of INT 10H or INT 21H is requested, certain registers must have specific values in them, depending
upon the function being requested. Various functions of INT 21H and INT 10H are selected by the value put
in the AH register.
Note:- Near calls & returns transfer control between procedures in the same code segment. Far calls &
returns pass control between different segments.
BIOS INTERRUPT: INT 10H subroutines are burned into the ROM BIOS of the 8086 based and compatibles
and are used to communicate with the computer user screen video. Much of the manipulation of screen text
or graphics is done through INT 10H. Among them are changing the color of characters or background,
clearing screen, and changing the locations of cursor. Below are two examples that use BIOS interrupts.
 Clearing the screen:
MOV AX, 0600H ;scroll entire screen
MOV BH, 07 ;normal attribute
MOV CX, 0000 ;start at 00,00
MOV DX, 184F ;end at 18, 4F
INT 10H ;invoke the interrupt
 Setting the cursor position:
MOV AH, 02 ;set cursor option
MOV BH, 00 ;page 0
MOV DL, 25 ;column position
MOV DH, 15 ;row position
INT 10H

74 Experiment 12 – BIOS and DOS Interrupt Programming


DOS INTERRUPTS: INT 21h is provided by DOS, When MS-DOS is loaded into the computer, INT 21H
can be invoked to perform some extremely useful functions. These functions are commonly referred to as
DOS INT 21H function calls. Data input and output through the keyboard and monitor are the most commonly
used functions. Below are two examples that use DOS interrupts.
 Display the message defined with variable VAR_MSG DB ‘Hello World’,’$’
MOV AH,09 ;option 9 to display string of data
MOV DX, OFFSET VAR_MSG ;DX= offset address of data
INT 21H ; invoke the interrupt
 Inputting a single character, with echo.
MOV AH, 01 ;option 01 to input one character
INT 21H ;invoke the interrupt
ACTIVITY 13.1
org 100h
.data ; Declare variables
hello_message db 'Hello','World',0dh,0ah,'$'
.code ; Write code
main proc
mov ax,@data ; Copy the address of data
mov ds,ax ; Segment into DS register

;clear Screen
MOV AX, 0600H ;scroll entire screen
MOV BH, 07 ;normal attribute
MOV CX, 0000 ;start at 00,00
MOV DX, 184Fh ;end at 18, 4F
INT 10H ;invoke the interrupt
;set cursor position
MOV AH, 02 ;set cursor option
MOV BH, 00 ;page 0
MOV DL, 25 ;column position
MOV DH, 15 ;row position
INT 10H
;print message
mov dx,offset hello_message
mov ah,9 ; MS-Dos Function to display string
INT 21H
mov ax,4C00h ; Halt the program and return control to OS
INT 21H
main endp
end main ; Mark the end of the source file
Execute and write outcome here: -

75 Experiment 12 – BIOS and DOS Interrupt Programming


EXERCISE 12.1. [2.5]
Write a program that displays the current time on the screen. Ignore the fractions of seconds. Show code and
outcome here: -
Hint:-
INT 21h / AH=2Ch - get system time;
Return: CH = hour. CL = minute. DH = second. DL = 1/100 seconds.

EXERCISE 12.2. [2.5]


Write a program that define and call three subroutines: clear the screen, centre the position of the cursor on
the display, and display a text (“I Love Pakistan”) on the monitor. Show code and outcome here: -
Hint:-
INT 10h / AH=06 ;clear screen and AH=02 ;set cursor position

76 Experiment 12 – BIOS and DOS Interrupt Programming


EXERCISE 12.3. [2.5]
Write a program to: Clear the screen. Set the graphical mode 40x25 resolution and display a char ‘C’ in the
middle of screen. Show code and outcome here: -

EXERCISE 12.4. [2.5]


Write a program to: Clear the screen. Set the mode CGA of 320 X 200 resolution. And lastly draw a red
horizontal line starts at point 50,100 and ends at point 50,150. Show code and outcome here: -
Hint:-
INT 10h / AH = 0Ch - change color for a single pixel.
Input:
AL = pixel color
CX = column.
DX = row.

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

77 Experiment 12 – BIOS and DOS Interrupt Programming


EXPERIMENT 13 – STATIC AND DYNAMIC MULTIPLICATION

OBJECTIVE: To understand static and dynamic multiplication using loops in assembly language.
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
COMMAND/SYNTAX USED:
 Use main Proc to start the procedure.
 Use mov ah, 01h to take input from user.
 Use Mov ah, 02h to display a single character.
 Use the next line command and previous output hold command to make the display readable.
MOV AH, 0AH
MOV AH, 0DH
 Use LEA (LOAD EFFECTIVE ADDRESS) command to display the string on screen.
LEA DX, VARIABLE
 Use interrupt command to exit the previous command.
Int 21h
 Use DOS exit command to exit the Programme.
Mov ah, 4ch
Int 21h
 Take input from user as much as you want. When number is entered by the user it automatically goes
into AL register and machine added 30hex in the data by itself. To get the required decimal number
30h must be subtracted from AL. The input must be taken in loop.
 Syntax for multiplication in loop (user defined): The destination register must be AL.
Mov cx, number
l1:
MUL source register
Dec cx
Jnz l1
 The result is stored in AL. To display the result, add 30h in the result because processor only
understand hex values. To get the desired output result, one must add 30h in the result.
If the result is >=10 use the div command
The divisor will be 10 and the dividend must be in “AL
register
If the result is >=100 use the div command, then use div
command twice.
 Multiplication in loop(static): In static, input is given in stack segment all other procedure is same as
above

78 Experiment 13 – Static and Dynamic Multiplication


EXERCISE 13.1. [5]
Write code to simulate dynamic multiplication in assembly language. Show code and outcome here: -

79 Experiment 13 – Static and Dynamic Multiplication


EXERCISE 13.2. [5]
Write code to simulate dynamic multiplication of Signed numbers. Show code and outcome here: -

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

80 Experiment 13 – Static and Dynamic Multiplication


EXPERIMENT 14 – SQUARES, CUBE AND FACTORIALS
OBJECTIVE: To understand and write code for square , cube and factorials.
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : Assembly Language
SOFTWARE REQUIRED : Emulator 8086, DOS, Debug
HARDWARE REQUIRED : Core i5 in Computer Labs, MDA – Win8086
COMMAND/SYNTAX USED:
 Use main Proc to start the procedure.
 Use mov ah, 01h to take input from user.
 Use Mov ah, 02h to display a single character.
 Use the next line command and previous output hold command to make the display readable.
MOV AH, 0AH
MOV AH, 0DH
 Use LEA (LOAD EFFECTIVE ADDRESS) command to display the string on screen.
LEA DX, VARIABLE
 Use interrupt command to exit the previous command.
Int 21h
 Use DOS exit command to exit the Programme.
Mov ah, 4ch
Int 21h
 Take input from user.When number is entered by the user it automatically goes into AL register and
machine added 30hex in the data by itself. To get the required decimal number 30h must be
subtracted from AL.
FACTORIAL:
Mov register, number
Dec register
loop1:
Mul register
Dec register
Jnz loop1
Execute and write outcome her: -

81 Experiment 14 – Square, Cube and Factorials


COMMAND FOR CUBE:
Mov register, 02
Dec register
loop1:
Mul register
Dec register
Jnz loop1
Execute and write outcome her: -

COMMAND FOR SQUARE:


Mov register, 01
Dec register
loop1:
Mul register
Dec register
Jnz loop1
Execute and write outcome her: -

82 Experiment 14 – Square, Cube and Factorials


Exercise 14.1. [10]
Write code that takes an array, prints out squares and cubes of each number in the array. Show code and
outcome here:

RESOURCES
http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf
http://www1.frm.utn.edu.ar/arquitectura/t86.pdf
https://www.academia.edu/6933756/8086_Assembler_Tutorial_for_Beginners
http://people.vts.su.ac.rs/~pmiki/STOREnGO/ASM%20zbirka/emu8086.pdf

83 Experiment 14 – Square, Cube and Factorials


“I insist you to strive. Work, Work
and only work for satisfaction with
patience, humbleness and serve thy
nation”.
Muhammad Ali Jinnah

“A scientist in his laboratory is not


a mere technician: he is also a
child confronting natural
phenomena that impress him as
though they were fairy tales”.
Marie Curie

“I have not failed. I've just found


10,000 ways that won't work”.
Thomas Alva Edison

You might also like