You are on page 1of 46

COMPUTER LABORATORY MANUAL

Computer Organization and Architecture


(EE – 321)
Spring Semester

Version-2.1

Name :kaleem ullah - Beses 21-B

DEPARTMENT OF COMPUTER SOFTWARE ENGINEERING


Military College of Signals
National University of Sciences and Technology
www.mcs.nust.edu.pk
1 Computer Organization and Architecture Lab Manual
PREFACE
This lab manual has been prepared to facilitate the students of software engineering in studying insight into
working of computer systems. The obvious objective of studying computer architecture is to learn how to
design one. Writing machine dependent software such as compilers, operating systems, and device drivers, need
knowledge of possible structural and functional organization of computer architectures. A software engineer or
scientific programmer interested in high performance studies computer architecture to learn how to design
programs to gain maximum performance from a given architecture. Working with systems that involve a variety
of interfaces, equipment and communication facilities require knowledge of computer organization.

PREPARED BY
Lab manual is prepared by Lecturer Rabia Khan and Demonstrator Kabeer Ahmed, updated by Mehreen Ahmed
under the supervision of Head of Department Dr. Adil Masood Siddiqi in year 2016.

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. Lab session details will
be given in training schedule.
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. In the exercises, you have to put the output in your Lab report.
k. Name your reports using the following convention:
Lab#_Rank_YourFullName
(1) ‘#’ replaces the lab number.
(2) ‘Rank’ replaces Maj/Capt/TC/NC/PC
(3) ‘YourFullName’ replaces your complete name.
l. You need to submit the report even if you have demonstrated the exercises to the lab engineer/instructor or
shown them the lab report during the lab session.

VERSION HISTORY
Date Update By Details
July 2013 Demo Kabeer Ahmed First Version Created
Feb 2014 Demo Kabeer Ahmed Second Version Created
Feb 2016 TA Mehreen Ahmed 2.1 Version Created

2 Computer Organization and Architecture Lab Manual


MARKS

Exp Date Experiment Title Max. Marks Instructor


# Conducted Marks Obtained Sign
1 INTRODUCTION TO EMU8086
2 REGISTER, INPUT/ OUTPUT
CHARACTERS & DISPLAY STRINGS
3 INPUT/ OUTPUT SINGLE AND
MULTI-DIGIT NUMBER
4 ARITHMETIC INSTRUCTIONS
5 IMPLEMENTATION ARITHMETIC
INSTRUCTIONS
6 BOOLEAN OPERATORS
7 EXPLORING FLAG REGISTER
8 DECISION INSTRUCTIONS
9 IMPLEMENTATION OF LOOP
STRUCTURES
10 IMPLEMENTATION OF LOOP
STRUCTURES
11 ARRAY PROCESSING, INDIRECT
ADDRESSING IN X86 ASSEMBLY
LANGUAGE
Grand Total

3 Computer Organization and Architecture Lab Manual


LIST OF EXPERIMENTS
EXPERIMENT 1 – INTRODUCTION TO EMU8086.........................................................................................................
5
EXPERIMENT 2 –REGISTER, INPUT/ OUTPUT CHARACTERS & DISPLAY STRINGS............................................
8
EXPERIMENT 3 –INPUT/ OUTPUT SINGLE AND MULTI-DIGIT NUMBER.............................................................
14
EXPERIMENT 4 – ARITHMETIC INSTRUCTIONS.......................................................................................................
19
EXPERIMENT 5 – IMPLEMENTATION ARITHMETIC INSTRUCTIONS...................................................................
21
EXPERIMENT 6 – BOOLEAN OPERATORS..................................................................................................................
24
EXPERIMENT 7 – EXPLORING FLAG REGISTER.......................................................................................................
27
EXPERIMENT 8 – DECISION INSTRUCTIONS............................................................................................................
31
EXPERIMENT 9 – IMPLEMENTATION OF LOOP STRUCTURES..............................................................................
36
EXPERIMENT 10 – IMPLEMENTATION OF LOOP STRUCTURES.............................................................................
40
EXPERIMENT 11 – ARRAY PROCESSING, INDIRECT ADDRESSING IN X86 ASSEMBLY LANGUAGE.............
42
Lab 02

Exercise 2.1

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.

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.

Code:

org 100h

.data

inMsg db "Enter a character ",'$'

newline db 0dh,0ah,'$'

outMsg db "Entered character in Uppercase: ",'$'

.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

sub bl, 32

;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

Output:
Exercise 2.2 Write a program that input a character from user. The program will display it ten
times on screen in newline.

Code:

org 100h

.data

inMsg db "Enter a character ",'$'

newline db 0dh,0ah,'$'

outMsg db "Entered 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

mov cx, 10

repitition:

;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

loop repitition

mov ax,4C00h

INT 21H

main endp

end main

Output:

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

org 100h

.data

newline db 0dh,0ah,'$'

outMsg db "Character : ",'$'

.code

main proc

mov bl, 'A'

mov cx, 26

repitition:

;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

add bl, 1

loop repitition

mov ax,4C00h

INT 21H

main endp

end main

Output:
Lab 03

Exercise 3.1

Code:

include emu8086.inc

.data

num1 dw 0

num2 dw 0

print 'Enter first number: '

call scan_num

mov num1,cx

printn

print 'Enter second number: '

call scan_num

mov num2,cx
mov ax,num1

add num2

printn

print 'Sum of the numbers is: '

call print_num

DEFINE_PRINT_NUM

DEFINE_PRINT_NUM_UNS

DEFINE_SCAN_NUM

proc END

ret

Output:

Exercise 3.2:

Code:
include emu8086.inc

.data

num1 dw 0

num2 dw 0

num3 dw 0

num4 dw 0

num5 dw 0

print 'Enter first number: '

call scan_num

mov num1,cx

mov ax,num1

printn

print 'Enter second number: '

call scan_num

mov num2,cx

add ax,num2

;----
printn

print 'Enter third number: '

call scan_num

mov num3,cx

add ax,num3

printn

print 'Enter fourth number: '

call scan_num

mov num4,cx

add ax,num4

printn

print 'Enter fifth number: '

call scan_num

mov num5,cx

add ax,num5
printn

print 'Sum of the numbers is: '

call print_num

DEFINE_PRINT_NUM

DEFINE_PRINT_NUM_UNS

DEFINE_SCAN_NUM

proc END

ret

Output:
Lab 04

Exercise 4.1

Code:

include emu8086.inc

.data

num1 dw 0

num2 dw 0

print 'Enter first number: '


call scan_num

mov num1,cx

printn

print 'Enter second number: '

call scan_num

mov num2,cx

mov ax,num1

mul num2

printn

print 'Multiplication result is: '

call print_num

DEFINE_PRINT_NUM

DEFINE_PRINT_NUM_UNS

DEFINE_SCAN_NUM

proc END

ret
Output:

Exercise 4.2

Code:

;x+2z-y-1

include emu8086.inc

.data

x dw 0

y dw 0

z dw 0

m dw 2

;================

print 'Enter value of x, y & z in expression as, '

printn

printn

print '" x + 2z - y - 1 "'

printn

printn
print 'Enter value of x: '

call scan_num

mov x,cx

printn

print 'Enter value of y: '

call scan_num

mov y,cx

printn

print 'Enter value of z: '

call scan_num

mov z,cx

;================

mov ax,z

mul m

sub ax,y
sub ax,1

add ax,x

printn

print 'The answer is: '

call print_num

DEFINE_PRINT_NUM

DEFINE_PRINT_NUM_UNS

DEFINE_SCAN_NUM

proc END

ret

Output:

Exercise 4.3:
Code:

include emu8086.inc

.data

h dw 0

w dw 0

l dw 0

a dw 0

b dw 0

c dw 0

num dw 2

;================

print 'Enter value of height, width & length of box to calculate its surface area from
formula as, '

printn

printn

print '" surface area = 2 * (length * width + length * height + width * height) "'

printn

printn

print 'Enter value of height: '

call scan_num

mov h,cx
printn

print 'Enter value of width: '

call scan_num

mov w,cx

printn

print 'Enter value of length: '

call scan_num

mov l,cx

;================

mov ax,l

mul w

mov a,ax

mov ax,l

mul h
mov b,ax

mov ax,w

mul h

mov c,ax

mov ax,a

add ax,b

add ax,c

mul num

printn

print 'After applying formula the result is: '

call print_num

DEFINE_PRINT_NUM

DEFINE_PRINT_NUM_UNS

DEFINE_SCAN_NUM
proc END

ret

Output:

Lab 05
Exercise 5.1: [5]
Write a program that Divide two word variables Var1=-500 and Var2=2 and stores the
result in memory.
Solution:

Code:

org 100h
include emu8086.inc
.data
var1 dw 500
var2 db 2
.code
mov ax, var1
mov bl, var2
div bl
call PRINT_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
ret

Output:

Exercise 5.2: [5]


Write an assembly language program that prompts for inputs %marks of four exams
M1, M2, M3, and M4. Suppose that M4 is a final exam that counts twice as much
as the other three. Calculate the sum (adding the last grade twice) and the average
(sum/5). Display the sum and average on two lines of a message box, each line
with an appropriate label.

Solution:
Code:

org 100h
include emu8086.inc
.data
sum dw 0
avg dw 0
.code
print "Enter marks for M1: "
call SCAN_NUM
printn ""
add ax, cx

print "Enter marks for M2: "


call SCAN_NUM
printn ""
add ax, cx

print "Enter marks for M3: "


call SCAN_NUM
printn ""
add ax, cx

print "Enter marks for M4: "


call SCAN_NUM
printn ""
add ax, cx
add ax, cx

mov sum, ax
mov bl, 5
div bl
mov avg, ax

print "Sum is : "


mov ax, sum
call PRINT_NUM
printn ""

print "Average is : "


mov ax, avg
call PRINT_NUM

DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
DEFINE_SCAN_NUM
ret
Output:

Exercise 5.3: [5]


Write a program that takes input from user in Celsius and Convert the temperature to
Fahrenheit.
F = (9/5) *C + 32

Solution:
Code:

org 100h
include emu8086.inc

.code
print "Enter temperature in C: "
call SCAN_NUM
printn ""

mov ax, 9h
mov bl, 5h
div bl

mov dl, al
mov dh, 0
mov ax, 0
add ax, dx

mul cx
add ax, 32
print "Temperature in F is : "
call PRINT_NUM

DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
DEFINE_SCAN_NUM

ret

Output:
Lab 06
Exercise:
Write a single instruction that clears the high 8 bits of AX and does not change the
low 8 bits.

Solution:
Code:

org 100h
include emu8086.inc
.code
mov ax, 1111111111111111b
and ax, 0000000011111111b
ret

Output:

Exercise: [6]
Write a single instruction that sets the high 8 bits of AX and does not change the
low 8 bits.

Solution:
Code:
org 100h
include emu8086.inc
.code
mov ax, 0000000000000000b
or ax, 1111111100000000b
ret

Output:

Lab 07

Exercise: 7.1 [5]


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)

Content Instructio Contents of CF OF SF PF AF ZF


s of AX n AX
(Before) (After)
0045 add ax, 5A(dec=90) 0 0 0 1 1 0
45
FF45 add ax, FF72(dec=653 0 0 1 1 1 0
45 94)
0045 add ax, - 0 1 0 0 1 1 1
45
FF45 add ax, - FF18(dec=653 1 0 1 1 0 0
45 04)
FFFF add ax, 0 1 0 0 1 1 1
1
Exercise 7.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=0
mov al, 00000110b
cmp al, 00000101b ; b. CF= ZF= SF=0
mov al, 00000101b
cmp al, 00000111b ; c. CF= 1, ZF= 0, SF= 1
Lab 08

Exercise 8.1:
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.

Code:

include emu8086.inc
.DATA
.CODE
main PROC
mov bx,0 ; number := 0
mov ax,0 ; sum := 0
forever: inc bx ; add 1 to number
add 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

Output:

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

Code:

include emu8086.inc
.DATA
.CODE
main PROC
mov bx,2 ; number := 0
mov ax,11 ; sum := 0
mov cx,7
mov dx, ax ; assume ax is larger
cmp ax, bx ; if AX >= BX then
jle L1 ; jump to L1
mov dx, bx ; else move bx to dx because it is larger than ax
L1: print "The smallest number is= "

cmp dx, cx
jle l2
mov dx, cx
l2:
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

Output:
Lab 09

Exercise 9.1: [5]


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

Code:
include emu8086.inc
.data
.code
main PROC
mov dl, 0 ; num
mov bl, 0 ; sum
for:
inc dl
add bl, dl
cmp dl, 10
jl for
mov ax, bx
print 'Sum is: '
call print_num
main ENDP

DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
Output:

Exercise 9.2: [5]


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

Code:
include emu8086.inc
.data
.code
main PROC

print 'Enter number for factorial: '


call scan_num
printn ''
mov ax, 1 ; factorial
for:
mul cx
sub cx, 1
cmp cx, 1
jg for
print 'Factorial is : '
call print_num
main ENDP

DEFINE_PRINT_NUM
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM_UNS
Output:
Lab 10

Exercise 10.1: [3]


The binomial coefficient is defined for integers 0<= k <=n by
Assuming values for n and k are stored in byte in memory. Write an assembly
language program that will prompt for input n and k, compute using the above
formula and display the binomial coefficient. (Hint: Do not calculate n! and k!
separately. Instead, calculate as).

Code:
include emu8086.inc
.data
.code
main PROC
mov bx, 6 ; N
mov cx, 3 ; K
mov si, bx
sub si, cx
mov ax, 1
for1:
mul bx
sub bx, 1
cmp bx, 1
jnle for1
mov bx, ax ; N!
mov ax, 1
for2:
mul cx
sub cx, 1
cmp cx, 1
jnle for2
mov cx, ax ; K!
mov ax, 1
for3:
mul si
sub si, 1
cmp si, 1
jnle for3
mov si, ax ; N-K!
mov ax, si
mov ax, si
mul cx
mov si, ax ; K!(N-K)!
mov ax, bx
mov bx, si
div bx
mov ah, 0
print 'binomial coefficient is :'
call print_num
main ENDP
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS

Output:

Exercise 10.2: [3]


Write an assembly language program that will for an integer n, compute the sum of
the squares of all integers from 1 to n and display the sum.

Code:
include emu8086.inc
.data
.code
main PROC
print 'Enter a number n: '
call scan_num
printn ''
mov si, 0 ; sum of squares
for:
mov ax, 1
mul cx
mul cx
add si, ax
sub cx, 1
cmp cx, 0
jnle for
print 'Sum of sqaures upto number is: '
mov ax, si
call print_num
main ENDP
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
Output:

Exercise 10.3: [4]


Write assembly language program, which swaps the values stored at these two
different addresses.

Code:
include emu8086.inc
org 100h
.data
var1 dw 11h
var2 dw 23h
.code
main PROC
mov ax, var1
print 'var1 before sawpping : '
call print_num
printn ''
mov ax, var2
print 'var2 before sawpping : '
call print_num
printn ''
mov ax, var1
mov bx, var2
mov var1, bx
mov var2, ax
mov ax, var1
print 'var1 after sawpping : '
call print_num
printn ''
mov ax, var2
print 'var2 after sawpping : '
call print_num
printn ''
main ENDP
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
Output:
Lab 11

Exercise 11.1: [5]


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.

Code:
include emu8086.inc
org 100h
.data
arr dw 10 dup (?)
.code
main PROC
mov bx, 10
lea si, arr
l1:
print 'Enter number : '
call scan_num
printn ''
mov word ptr[si], cx
add si, 2
sub bx, 1
cmp bx, 1
jge l1

mov bx, 10
mov ax, 0 ; sum of numbers
lea si, arr
l2:

add ax, word ptr[si]


add si, 2
sub bx, 1
cmp bx, 1
jge l2

mov di, ax

print 'Sum of the numbers is: '


call print_num
printn ''

mov bx, 10
mov cx, 0 ; number of numbers
lea si, arr
l3:

cmp word ptr[si], 0


je ne
add cx, 1
ne:
add si, 2
sub bx, 1
cmp bx, 1
jge l3
mov ax, cx

print 'Number of the numbers is: '


call print_num
printn ''

mov ax, di
div cx
mov ah, 0
print 'Average of the numbers is: '
call print_num
printn ''

;;;;;;
mov bx, 10
mov cx, 0 ; number of numbers greater than or equal to avg
lea si, arr
l4:

cmp word ptr[si], ax


jnge ge
add cx, 1
ge:
add si, 2
sub bx, 1
cmp bx, 1
jge l4

mov ax, cx

print 'Number of the numbers greater than or equal to avg is: '
call print_num
printn ''

main ENDP
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
Output:

Exercise 11.2: [5]


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.

Code:
include emu8086.inc
org 100h
.data
arr dw 5 dup (?)
.code
main PROC

mov bx, 5
lea si, arr
l1:
print 'Enter number : '
call scan_num
printn ''
mov word ptr[si], cx
add si, 2
sub bx, 1
cmp bx, 1
jge l1

print 'Enter a number to search : '


call scan_num
printn ''

mov bx, 5
lea si, arr
l2:
cmp cx, word ptr[si], cx
jne n
mov sp, 1
n:
add si, 2
sub bx, 1
cmp bx, 1
jge l2

cmp sp, 1
jne notfound
print 'Number found'
jmp e
notfound:
print 'Number not found'
e:

main ENDP
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
Output:

You might also like