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

Lab Manual - Computer Organization and Assembly Language - Fall 23

This document appears to be a lab manual for a course on computer organization and assembly language. It provides an outline of 14 labs covering topics like instruction cycles, basic instructions, addressing modes, conditional jumps, and subroutines. It also includes rubrics for evaluating student work and lists 3 course learning outcomes related to assembly language programming, interfacing assembly subroutines to high-level languages, and communicating knowledge of assembly language. The first lab is introduced as an introduction to computer organization using the Little Man Computer simulator to study instruction cycles and basic instructions.

Uploaded by

Anas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
323 views

Lab Manual - Computer Organization and Assembly Language - Fall 23

This document appears to be a lab manual for a course on computer organization and assembly language. It provides an outline of 14 labs covering topics like instruction cycles, basic instructions, addressing modes, conditional jumps, and subroutines. It also includes rubrics for evaluating student work and lists 3 course learning outcomes related to assembly language programming, interfacing assembly subroutines to high-level languages, and communicating knowledge of assembly language. The first lab is introduced as an introduction to computer organization using the Little Man Computer simulator to study instruction cycles and basic instructions.

Uploaded by

Anas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 76

Department of Computer Science,

Faculty of Computing, Capital University of Science and Technology


_____________________________________________________________________________________

Lab Manual

Computer Organization and


Assembly Language
(CS2523)

Fall 2023

1
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Contribution

Prepared By: Dr. Umair Rafique


Reviewed By: Ms. Tayyaba Zaheer
Last updated on: 7th Dec, 2023

2
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Outline
Topic Page No.
Lab-1: Introduction to Computer Organization Using the Simulator Little Man Computer (LMC) 7
Lab-2: Instruction Cycle and Working with Some Basic Instructions in the LMC Simulator 11
Lab-3: Loops in Little Man Computer Simulator 18
Lab-4: Implementing Decimal to Binary Conversion in LMC (Part 1) 23
Lab-5: Implementing Decimal to Binary Conversion in LMC (Part 1) 28
Lab-6: Introduction to Intel 8086/8088 Assembly Language 32
Lab-7: Basic Instructions in Intel 8086/8088 37
Lab-8: Addressing Modes, Arrays 42
Lab-9: Conditional Jumps 46
Lab-10: Unconditional Jumps, Sorting Algorithm 51
Lab-11: Instructions Related to Shifting and Rotation: Implementing Multiplication Algorithm 57
Lab-12: Bit-wise logical operations 63
Lab-13: Subroutines, Stack 67
Lab 14: Parameter Passing through Stack 71

3
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
Rubric for the Evaluation of Lab Tasks
(a) PSYCHOMOTOR
Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total
Score
(Max Marks) (0%) 0% ≤ S < 50% 51% ≤ S < 65% 66% ≤ S < 80% 81% ≤ S < 100%
(S)

Understanding of Don’t know Inadequate Brief Adequate Complete


problem about understanding of the understanding of understanding of the understanding of
problem provided problem in the provided provided problem in the provided
(20)
understanding practice task problem in practice practice task exists problem in practice
task but missing some task
points

Program Logic Don’t know Poor logic in Logic corrects to Correct logic but with Program Logic
about the program some extent but minor mistakes completely correct
(20)
programming with major
logic mistakes

Program No task is Makes several Makes few critical Makes some non- Applies the
Implementation critical errors in errors in syntax critical errors in syntax procedural
implemented
syntax and in and in applying and in applying knowledge and
(20)
applying procedural procedural procedural knowledge syntax in perfect
knowledge knowledge ways

Program Program Program does not Program produces Program produces Program produces
Correctness totally produce correct answers or correct answers or
correct answers or
incorrect appropriate results for appropriate results
(10) correct answers or
appropriate results most inputs. for all inputs tested
appropriate results
for most inputs,
for most inputs.
but contains
miscalculations in
some cases

Use of Software Don’t know Uses software tool, Uses software tool, Uses software tool, Uses software tool,
Tool about the tool with limited with some with considerable with a high degree
competence competence competence of competence
(10)

Sub-Total Marks 80
Sub-Total Marks Obtained (PD)

4
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

(b) AFFECTIVE

Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total


(Max (0%) 0% ≤ S < 50% 51% ≤ S < 65% 66% ≤ S < 80% 81% ≤ S < 100% Score
Marks) (S)

Viva All answers Answered for less Answered for 60% of the Answered for Answered for more
are incorrect than
(20) questions. But 80% of the than 90% of the
40% of the questions incomplete
questions. Still questions correctly,
indicating a lack of understanding of results
need some good understanding
is still
understanding of
improvements of results is
results evident
conveyed

Sub- 20 Sub-Total Marks Obtained (AD)


Total

5
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Course Learning Outcomes

1. Assembly language programming knowledge to write moderately complex assembly language


subroutines. [2. Knowledge for Solving Computing Problems]

2. Assembly language subroutines interfacing to a high-level language i.e., C++. [4.


Design/Development of Solutions]

3. Participate and answer the questions related to Assembly language. [7. Communication]

6
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-1
Introduction to Computer Organization
Using the Simulator Little Man Computer (LMC)

7
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
In this lab we will learn about the basic structural components of a computer and will work with a simulator to
understand how a computer functions.

2. Concept Map
The very first computer designed by von Neumann and his colleagues is called IAS computer. It is the
prototype of all subsequent general- purpose computers. With rare exceptions, all of today’s computers have
this same general structure and function and are thus referred to as von Neumann machines.

Generally, there are four main structural components of a computer:

 Central processing unit (CPU): Controls the operation of the computer and performs its data
processing functions; often simply referred to as processor.
 Main memory: Stores data.
 I/O: Moves data between the computer and its external environment.
 System interconnection: Some mechanism that provides for communication among CPU, main
memory, and I/O. A common example of system interconnection is by means of a system bus,
consisting of a number of conducting wires to which all the other components attach.

A CPU has following structural components.

 Control unit: Controls the operation of the CPU and hence the computer.
 Arithmetic and logic unit (ALU): Performs the computer’s data processing functions.
 Registers: Provides storage internal to the CPU.
 CPU interconnection: Some mechanism that provides for communication among the control unit, ALU,
and registers.

We will use a simulator to understand the inner workings of a computer. This simulator is called Little Man
Computer.

Little Man Computer - CPU simulator

The Little Man Computer (LMC) is an instructional model of a computer, created by Dr. Stuart Madnick in
1965. It models the architecture of a simple computer and has all the basic features.

https://www.peterhigginson.co.uk/lmc

Inside the CPU there are:

 100 Memory locations (numbered 0 to 99).


 An Arithmetic Unit to do calculations.
 An Accumulator to store the result of the last operation or calculation.
 A Program Counter to store the address (in memory) of the next instruction to do.
 An Instruction Register to hold the top digit of the instruction read from memory.
 An Address Register to hold the bottom two digits of the instruction read from memory.
 An Input tray into which a number can be typed when needed.
 An Output area where any numbers output are printed.

8
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
The Program Counter holds only 2 digits (0 to 99) and the Accumulator holds 3 digits and a sign (-999 to 999).
The Output area holds a certain amount of information in two columns and then scrolls (and old information
disappears).

When an instruction is read from memory the top digit is used to decide what to do. To help us write
programs more easily each instruction type is given a name which the ASSEMBLE function converts to the
corresponding code. The bottom two digits are used as an address associated with the instruction.

3. Walkthrough Task
There is a SELECT button which will allow you to select from the supplied programs including the following
two.

Add: Output the sum of two numbers. Requests input twice

Add/subtr: Input three numbers. Output the sum of the first two and the third minus the first.

To start using this LMC SELECT the add program from the drop down list.

Click RUN to start execution. Execution starts at a medium/slow speed and you can use the >> button to speed
it up or the << button to slow it down. The lowest five speeds show the flow of execution of an instruction as
follows:

FETCH Get the contents of the next instruction to be executed. In parallel add 1 to the Program Counter.

9
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
EXECUTE Decode the instruction loaded and perform the operation requested.

The STEP button does one instruction only (at a slow speed). You can RESET to go back to the beginning (which
is always with Program Counter = 0 on the LMC) or set the Program Counter to any memory address.

You can input into the INPUT box (when the running program requests it) and (when the program is not
running) into the individual memory locations, the Program Counter or the Assembly Language area. You
cannot alter the Code area or the CPU registers apart from the Program Counter. For memory, the INPUT box
and the Program Counter only numbers are accepted.

4. Practice Task

Practice Task 1 [Expected time = 20mins]


Select the add/subtr program from the select menu and complete its execution using the step function of the
simulator. Observe how the program is loaded in the memory and how the steps are performed.

Practice Task 2 [Expected time = 30mins]


Modify the above program so that now it takes 4 inputs. It outputs the sum of the first two inputs and the
difference of the last two inputs.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

10
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-2
Instruction Cycle and Working with Some Basic Instructions
in the LMC Simulator

11
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
In the previous lab we learned about the basic structural components of a computer and worked with a basic
simulator, Little Man Computer (LMC), to understand how a computer functions. In this lab we will continue
with the same simulator and will focus on its instruction set.

2. Concept Map
In the LMC simulator, when an instruction is read from memory, the top digit is used to decide what to do. To
help us write programs more easily each instruction type is given a name. The ASSEMBLE function converts
this name to the corresponding code. The bottom two digits are used as an address associated with the
instruction.

In other words, the first digit is the “opcode” and the other two digits represent the address.

Instruction Cycle

The simulator has two steps in its instruction cycle.

FETCH Get the contents of the next instruction to be executed. In parallel add 1 to the Program Counter.

EXECUTE Decode the instruction loaded and perform the operation requested.

Instruction Set

The simulator has the following instructions.

Code Name Description


0 HLT Stop (Little Man has a rest).
1 ADD Add the contents of the memory address to the Accumulator
2 SUB Subtract the contents of the memory address from the Accumulator
3 STA or STO Store the value in the Accumulator in the memory address given.
4 This code is unused and gives an error.
5 LDA Load the Accumulator with the contents of the memory address
given
6 BRA Branch - use the address given as the address of the next
instruction
7 BRZ Branch to the address given if the Accumulator is zero
8 BRP Branch to the address given if the Accumulator is zero or positive
9 INP or OUT Input or Output. Take from Input if address is 1, copy to Output if
address is 2.
9 OTC Output accumulator as a character if address is 22. (Non-standard
instruction)

12
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Labels

The assembler allows us to give names (called labels) to addresses. Any word that is not a recognized
instruction is assumed to be a label. We can use these labels with branch instructions to go to a certain part of
the program if the accumulator is 0 (BRZ) or if the accumulator is zero or positive (BRP).

Comments

You can also put comments into the program. Any line starting with // is ignored by the assembler.

Input

You can input into the INPUT box (when the running program requests it) and (when the program is not
running) into the individual memory locations.

3. Walkthrough Task
We want to write a program that can find the bigger of two numbers stored at given locations in the memory.

We could do the same thing in C++ using the following code. The two numbers are stored in variables x and y.

int x=5;
int y=6;
if(x>=y)
cout<<x;
else
cout<<y;

However, in this case we are writing the assembly language program using the instructions available to us in
the simulator. We have only a limited set of instruction so we need to build the logic accordingly.

Open the simulator using the following link.

https://www.peterhigginson.co.uk/lmc

The first thing is that we want the two numbers to be stored at some locations. We choose location 90 and 91.
We can simply type the two numbers there. We can type 5 at address 90 and 6 at address 91.

13
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

That means now we have the two numbers at known addresses (corresponding to the two variables x and y as
given in the program above).

Now we need to compare the two numbers. We do not have any instruction in LMC’s instruction set that can
compare two numbers. However, we have an instruction that can subtract from the accumulator (SUB) and
we also have an instruction that can go to a certain address depending upon the value of the accumulator
(BRZ if the accumulator is 0 or BRP if the accumulator is zero or positive). We will use these instructions to
develop the logic of the program.

We write our program in the left most part of the interface.

14
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

We will first load the first number in the accumulator. We use the following instruction for this purpose. It will
load 5 into accumulator.

LDA 90

To compare it with the other number stored at 91, we subtract that number from the accumulator using the
following instruction

SUB 91

Depending on which number is bigger, the content of the accumulator will be negative (if number1 <
number2) or 0 (if number1 and number2 are equal) or positive (if number1>number2).

We want to print the bigger number. That means if the accumulator is 0 or positive, we want to print the
number at address 90. If that is not the case, then we want to print the number at address 91.

To be able to execute a set of instructions based on a decision, we use a label to identify that group of
instructions. Then we go to that label if a certain condition holds.

Let’s write the set of instructions that would print number1 (stored at 90). Note that OUT instruction only
prints what is in the accumulator, so we need to first load the number into the accumulator and then write
OUT.

LDA 90

15
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
OUT

We assign a label L1 to these two instructions and the resulting instructions will look like.

L1 LDA 90

OUT

We will go to these instructions (identified by L1) if the accumulator is zero or positive. We use BRP in the
following way.

BRP L1

This means branch to L1 if the accumulator is zero or positive.

And if that is not the case, we want to print the other number (stored at 91). We can do so in the following
way.

LDA 91

OUT

The complete program looks like the following. We add HLT to the point where we want to stop the program.

LDA 90

SUB 91

BRP L1

LDA 91

OUT

HLT

L1 LDA 90

OUT

HLT

Click on submit to assemble and load the program.

16
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Click on step to run the program step by step.

Focus on the changes made to Program Counter, Instruction Register, Address Register and Accumulator.

4. Practice Task
Practice Task 1 [Expected time = 20mins]
Write a program to find the smaller of the two numbers stored at addresses 90 and 91.

Practice Task 2 [Expected time = 30mins]


Write a program to find the bigger of three numbers stored at addresses 90, 91 and 92.

5. Evaluation Task (Unseen) [Expected time = 30 mins]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

17
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-3
Loops in Little Man Computer Simulator

18
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
In the previous lab we learnt the instruction set of Little Man Computer (LMC). We used it to write a simple
program to find bigger of two numbers stored in the memory. We learnt how we can use basic instructions to
implement decision making logic in our program using BRP (branch if accumulator is 0 or positive) instruction
and labels.

In this lab (and next 2 labs) we will continue to work with LMC and will use its basic instruction set to
implement increasingly complex logics. Our focus in this lab is to learn how to implement loops.

Although LMC has a very basic instruction set, the concepts we are learning are relevant for the more
advanced 8086/8088 assembly language which we will start in Lab 6.

2. Concept Map
LMC has the following instructions as we have seen in the previous lab.

Code Name Description


0 HLT Stop (Little Man has a rest).
1 ADD Add the contents of the memory address to the Accumulator
2 SUB Subtract the contents of the memory address from the Accumulator
3 STA or STO Store the value in the Accumulator in the memory address given.
4 This code is unused and gives an error.
5 LDA Load the Accumulator with the contents of the memory address
given
6 BRA Branch - use the address given as the address of the next
instruction
7 BRZ Branch to the address given if the Accumulator is zero
8 BRP Branch to the address given if the Accumulator is zero or positive
9 INP or OUT Input or Output. Take from Input if address is 1, copy to Output if
address is 2.
9 OTC Output accumulator as a character if address is 22. (Non-standard
instruction)

We have two types of branches here. Instruction 6 is an “unconditional branch”. That means it does not
depend on any condition. Whatever address we specify in the instruction, that address will be taken as the
address of the next instruction. The jump to this new instruction is not dependent on any condition.

Instruction 7 and 8 are “conditional branches”. That means the branch to the specified address is made only if
the corresponding condition is true. In case of BRZ, if the accumulator is 0, the branch is taken. In case of BRP
if the Accumulator is 0 or positive, the branch is taken. We used BRP in the previous lab to implement a simple
if structure.

We can use the same logic to implement a loop. This is demonstrated using the walkthrough task.

19
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

3. Walkthrough Task
In this task we will write a basic loop to print numbers from 1 to 5.

Open the simulator using the following link. https://www.peterhigginson.co.uk/lmc

We can start with the starting number (1), increment it by 1 in each iteration and will continue till this number
is equal to the ending number (5).

We only have an instruction which can add to the accumulator the value given at the specified address. That
means we need to store this “1” in the memory too.

We will store the starting number at address 80.

Ending number at address 81. We will store 6 here as we want to print till 5.

The increment value (1) at address 82.

We will use address 83 as a temporary storage so that we do not change the original numbers.

Steps of the program

1. Load the starting number from address 80 (LDA 80)


2. Save it to address 83 which we will use as temporary storage. (STA 83)
3. Load the number from address 83 to the accumulator. (LDA 83)
4. Print it (OUT)
5. Add to the accumulator whatever is at address 82 (increment value) (ADD 82)
6. Store the accumulator at address 83 (STA 83)
7. Sub from the accumulator whatever is at address 81 (6 which is ending value + 1) (SUB 81)
8. End the program if the accumulator is 0.
9. Go to step 3.
Note that if step 9 will execute only if line 8 did not terminate the program.

The complete code for the above program is given below. We have used LOOP and END as labels.

LDA 80

STA 83

LOOP LDA 83

OUT

ADD 82

STA 83

SUB 81

BRZ END

BRA LOOP

END
20
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
HLT

Write the program in the left most box and then press submit.

Update the values at addresses 80, 81 and 82 after clicking on submit. Our memory will look like the following.

Run the program step by step and trace its execution. The final output will be the following.

21
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 30mins]
Change the above program so that it prints the number in the descending order like

5
4
3
2
1

Practice Task 1 [Expected time = 30mins]


Change the above program so that it prints the number in the descending order like

5
4
3
2
1

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

22
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-4
Implementing Decimal to Binary Conversion in LMC
Part 1

23
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
In this lab (and the next) we will implement decimal to binary conversion in LMC.

2. Concept Map
To convert a decimal number into binary, we repeatedly divide the decimal integer by 2. Each remainder is a
binary digit in the translated value.

We want to implement this logic in Little Man Computer (LMC).

One main issue here is that there is no division instruction in the LMC. Hence, we need to implement division
using repeated subtraction. In this lab we will focus on this task. The procedure will be explained in the
walkthrough task. The conversion will then be discussed in the next lab (Lab 5).

3. Walkthrough Task
In this lab we are going to focus on implementing division by 2 using repeated subtraction.

Open the simulator using the following link. https://www.peterhigginson.co.uk/lmc

We will use the following addresses for special purposes.

Will store dividend at address 80

Divisor at address 81

Quotient at address 82. Initially 0 will be stored here.

1 will be stored at address 83. We will use it when we want to increment the quotient.

Address 84 will be used as temporary storage.

24
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

The basic idea of the program is as following.

1. Load the dividend in the accumulator. Subtract from it the divisor.


2. If the dividend is 0 or positive, increment the quotient. In the following program the increment
quotient part has been identified with the label INCQ.
3. If this jump to the INCQ does not happen, that means the dividend is now negative. Hence, we do not
increment the quotient and go to the label END.
4. If the jump to INCQ happened, then we increment the quotient and then jump (branch) to the label
loop. That means going back to step 1 above.
5. Once we reach the label END, we load the current value from address 84. Actually, it represents the
remainder but the divisor has been subtracted from it one extra time in the previous loop (we come
to know about it only when we get negative value at address 84). To correct it we add the divisor one
time (ADD 81) to this value and then store it at address 84 (STA 84).

The complete program is as shown below. The first two lines of the program where we copy the dividend to
address 84 is to keep a copy of the dividend which we work with and do not destroy the original. This program
will print Remainder (address 84) and then Quotient (address 82) in the output.

LDA 80
STA 84
LOOP LDA 84
SUB 81
STA 84
BRP INCQ
BRA END
INCQ LDA 82
ADD 83
STA 82 //INCQ ends
BRA LOOP
END LDA 84 //Remainder
ADD 81 //subtr correction
STA 84
OUT
LDA 82 //Quotient
OUT
HLT
//80 stores dividend
//81 stored divisor
//82 is quotient. init 0
//83 stores 1 as inc
//84 temp storage. Stores Remainder.

Write the program in the left most box in the simulator and then click submit.

25
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Update the values in the memory storing dividend at 80, divisor at 81. 0 at 82 and 1 at 83. For dividing 15 by 2,
the memory will look like the following.

Run the program and see how it updates different memory locations.

26
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 50mins]
Implement multiplication as repeated addition using a logic similar to the previous program.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

27
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-5
Implementing Decimal to Binary Conversion in LMC
Part 2

28
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
In the previous lab, we implemented division as repeated subtraction and multiplication as repeated addition
because we do not have any division or multiplication instruction in Little Man Computer (LMC). In this lab we
will use this implementation of division to implement decimal to binary conversion.

2. Concept Map
The following figure illustrates the method of decimal to binary conversion.

We have already implemented the division as repeated subtraction using the following code in the previous
lab.

LDA 80
STA 84
LOOP LDA 84
SUB 81
STA 84
BRP INCQ
BRA END
INCQ LDA 82
ADD 83
STA 82 //INCQ ends
BRA LOOP
END LDA 84 //Remainder
ADD 81 //subtr correction
STA 84
OUT
LDA 82 //Quotient
OUT
HLT
//80 stores dividend
//81 stored divisor
//82 is quotient. init 0
//83 stores 1 as inc

29
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
//84 is temp storage. Stores Remainder
Using this implementation whenever we need division, we will implement decimal to binary conversion. The
logic of the program is explained in the following walkthrough task.

3. Walkthrough Task
The basic idea of the program is as following.

1. Load the number to be converted in the accumulator from some fixed memory location. We will use
address 90 for this purpose.
2. Store this number at address 80 as our division algorithm uses this address to store the dividend.
3. Store value 2 at address 81. This is the address where the division algorithm stores divisor.
4. Store value 1 at address 83. This is where the division algorithm stores increment 1.
5. Mark the division algorithm with a label. We will use label DIV here. Branch to the DIV
a. It is like calling a function for division which will divide the value stored at address 80 with
value 2 stored at address 81.
b. Once the division completes, it will store the remainder at address 84 and the quotient at
address 82. We print this remainder and store the quotient at address 90 for the next
iteration.
c. An important point here is that in the previous lab we did not set quotient back to its initial
value 0 because we performed only one division. Now as this division algorithm will be
called again and again, we need to set the quotient to 0 before next iteration.
The complete program with description of each line is given below.

Line Description
1 START LDA 90 //number Number to be converted
2 STA 80 //Divisor Store the number at address 80
3 BRA DIV Branch to Division part
4 DIVRET BRZ ENDPROG Label to return from division. End if Acc 0
5 BRA START Otherwise branch to start for next iteration
6 DIV LDA 80 Div algorithm starts here. Load dividend
7 STA 84 Store it at 84 as temp value
8 LOOP LDA 84 Loop for div as repeated subtraction
9 SUB 81
10 STA 84
11 BRP INCQ
12 BRA END
13 INCQ LDA 82
14 ADD 83
15 STA 82 //INCQ ends
16 BRA LOOP
17 END LDA 84 //Remainder Division is complete now. Load remainder
18 ADD 81 //subtr corr Add divisor for correction (see last lab)
19 STA 84 //Remainder Update remainder to 84
20 OUT Print the remainder
21 LDA 82 //Quotient Load quotient
22 STA 90 //next time Store it to address 90 for next time
23 LDA 99 //0 value Load value 0 from address 99
24 STA 82 //init Q Store 0 to address 82 for next iteration.
25 LDA 90 //quotient Load quotient back to accumulator
30
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
26 BRA DIVRET Branch to DIVRET. return from function
ENDPROG HLT

As an example let’s convert 8 to binary which will be 1000. The memory will look like the following.

Address 90 stores the number.

Address 81 stores 2 for division with 2 each time.

Address 83 stores 1 as an increment value of 1.

Run the program and observe its behavior.

4. Practice Task
Practice Task 1 [Expected time = 50mins]
Write a program to convert binary back to decimal.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
31
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
Total 100

32
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-6
Introduction to Intel 8086/8088 Assembly Language

33
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
By now we are familiar with computer organization and even have written simple programs in a basic
simulator. Hence it is now time to move to the assembly language we will be using in this course which is Intel
8086/8088 Assembly Language. In this lab we will learn how to setup the software and will write a simple
program in this language.

2. Concept Map
We need the following software.

 Notepad (for writing programs)


 NASM (Assembler). Netwide Assembler
 AFD (Debugger). Advanced Full-screen Debugger
 DOSBox (DOS emulator)

Follow the following steps to download and install the software

 Download the DOS version of nasm (Netwide Assembler) from


https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/dos/
(Download nasm-2.15.05-dos.zip)
or
https://www.dropbox.com/s/vooq8cvomi4l49c/nasm-2.15.05-dos.zip?dl=0

 Unzip nasm folder to a drive (C: or D:). Rename it to "Assembly-DOS".

 AFD is the debugger (to view program execution). Download AFD.exe from
https://www.dropbox.com/s/q7p2g78ya4rt05x/AFD.EXE?dl=0
Copy AFD.exe to the folder "Assembly-DOS" we created above.

 AFD Tutorial can be downloaded from the following link


https://www.dropbox.com/s/a6a1u2lj3x7yi6g/AFD_Tutorial.pdf?dl=0

 Download and Install DOSBox from


https://www.dropbox.com/s/dk3vokjvk8jh9l4/DOSBox0.74-win32-installer.exe?dl=0

How to run a program?


1. Write your program in notepad. Save your program file to the “Assembly-DOS” folder using .asm
extension (e.g. file name is "ex01.asm").

2. Run DOSBox and mount the folder "Assembly-DOS" as a drive using the following command. Here D:\
Assembly-DOS is the path to the folder

mount X D:\Assembly-DOS

34
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

3. Navigate to Assembly-DOS folder (drive X) from DOSBox by writing the following command
X:

4. Use the following command to assemble the program


nasm ex01.asm –o ex01.com –l ex01.lst

5. Use the following command to run the program


afd ex01.com

3. Walkthrough Task
The first program that we will write will only add three numbers. It will clarify most of the basic concepts of
assembly language. In plain English, the steps of our program are listed below.

Here ax and bx are two general purpose registers which can hold values. 8086/8088 architecture has many
registers and we will discuss these later.

Write the following code in a .asm file.

Description of the code is given below.

35
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Use the following command to assemble our program.

nasm ex01.asm –o ex01.com

Use the following command to run the program.

Afd ex01.com

The debugger shows the values of registers, flags, stack, our code, and one or two areas of the system
memory as data. Debugger allows us to step through our program one instruction at a time and observe its
effect on the registers and program data.

Use F1 and F2 to step through the program

36
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

• Use quit command to exit from the shell.

• On DOS command prompt type cls and press Enter to clear the screen

4. Practice Task
Practice Task 1 [Expected time = 20mins]
Modify the above program to add 5 numbers.

Practice Task 2 [Expected time = 30mins]


Using the add instruction, write a program that can multiply 6 with 10.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

37
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-7
Basic Instructions in Intel 8086/8088

38
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
In this lab we will learn about the registers in 8086/8088 architecture and some basic instructions.

2. Concept Map

Registers

The following figure shows the registers of 8086/8088 architecture.

The registers AX, BX, CX, and DX behave as general-purpose registers in Intel architecture and do some
specific functions in addition to it. X in their names stand for extended meaning 16bit registers.

All general-purpose registers can be accessed as one 16bit register or as two 8bit registers.

For example, the two registers AH and AL are part of AX.

39
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Data Movement Instructions

These instructions are used to move data from one place to another. These places can be registers, memory,
or even inside peripheral devices. For example

mov ax, bx ;move whatever is in bx to ax

Arithmetic Instructions

Arithmetic instructions like addition, subtraction, multiplication, division

add ax, 1234 ;copy 1234 to the register ax

add bx, [1200] ;The bracketed form means add the data placed at address 1200.

Miscellaneous Instructions

For writing even some basic programs, we need to implement some form of decision making. Although we
have not covered this topic in the theory lectures till now, it is useful to work with a few basic instructions so
that we can write some interesting programs in the labs.

For this purpose, we will work with the following instructions as a pair.

CMP ax, 0 ;compares the contents of ax with value 0

JNE Label ;jumps to label “Label” if ax is not equal to 0.

We will study later how CMP actually works and what other options we have available apart from JNE. For
now, we will use these two instructions as a pair for writing some basic programs.

3. Walkthrough Task
We will write a program to implement multiplication as repeated addition to demonstrate the working of the
above-mentioned instructions.

The steps of the program will be the following.

1. Store the multiplier in ax.


2. Store the multiplicand in bx.
3. We have to accumulate ax by adding ax to itself bx times. Hence, we will decrement bx each time we add
ax to itself and will terminate the loop when bx is 0.

The complete code of the program is given below.

40
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Write the program using notepad and save it as “Lab7P1.asm” in the NASM folder.

Open DOSBox. Mount the NASM folder as drive X: (See Lab 6 for detailed instructions).

Assemble and run the program using AFD. It will open the following screen. Trace the program focusing on the
registers ax, bx and cx.

41
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 20mins]
Write a program to implement division as repeated subtraction. Do not use the DIV instruction.

Practice Task 2 [Expected time = 30mins]


Write a program to convert a decimal number to binary. Use the same division code you wrote in Practice Tsk
1. Do not use DIV instruction.

5. Evaluation Task (Unseen) [Expected time = 30 mins]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

42
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-8
Addressing Modes, Arrays

43
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
In this lab we will learn how to reserve cells in memory as a byte or as a word. We will use this concept to
store an array of numbers in the memory and then will perform different operations on this array.

2. Concept Map
We can reserve a cell in memory using “db” (define byte) and give it some value. For example “db 5” will
reserve a byte and store 5 in it. We can use dw to reserve a word (or 2 bytes)

To refer to this variable later in the program, we need the address occupied by this variable. We can associate
a symbol with any address and then use that symbol in the rest of the code.

In direct addressing the memory address is fixed and is given in the instruction. The actual data used is placed
in memory. For example, to move data stored at label num1 to ax, we can use the following instruction.

mov ax, [num1]

This is called direct addressing mode.

The following program presents an example. In line 13 we have declared 4 numbers using dw. The last one
will be used to store the result of the addition of the first three. The label we have used is num1. In line 3, we
move the contents of memory address represented by num1 to ax. This will move the first number, 5, to ax.
We then store it at the desired location which is num1+6. Line 5 moves the next number to ax and line 6 adds
it to the result stored at [num1+6]. Line 7 moves the last number to ax and line 8 adds it to the result.

3. Walkthrough Task
In the following program we will work with the following instructions as a pair. We have used the same pair of
instructions in Lab 7.

CMP ax, 0 ;compares the contents of ax with value 0

JNE Label ;jumps to label “Label” if ax is not equal to 0.

The following program finds the sum of an array declared at num1 and stores this sum in the register ax.

44
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 30mins]
Write a program that declares an array of 10 numbers. It then finds the largest of these numbers and stores it
at cx. You have to use CMP and JNE which have been discussed in the previous task.

Practice Task 2 [Expected time = 30mins]


Write a program that declares two arrays of 10 numbers each. Compare the arrays element by element and
store 1 in cx if the arrays are identical (all corresponding elements match) and 0 if they are not.

Practice Task 3 [Expected time = 30mins]


Write a program that declares an array of 10 numbers. Then reverse the array and store it at the same place.
For example, if the array was 1 2 3 4 5 6 7 8 9 10, the program will reverse it to 10 9 8 7 6 5 4 3 2 1.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
45
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

46
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-9
Conditional Jumps

47
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
We use comparisons and boolean expressions extensively in higher level languages. They must be available is
some form in assembly language, otherwise they could not possibly be made available in a higher-level
language. In this lab we are going to see how conditional jumps can be written in Intel 8088 assembly
language.

2. Concept Map

The basic root instruction for all comparisons is CMP standing for compare. The operation of CMP is to
subtract the source operand from the destination operand, updating the flags without changing either the
source or the destination. CMP is one of the key instructions as it introduces the capability of conditional
routing in the processor.

A closer thought reveals that with subtraction we can check many different conditions. For example, if a larger
number is subtracted from a smaller number, then borrow is needed. The carry flag plays the role of borrow
during the subtraction operation. And in this condition the carry flag will be set. If two equal numbers are
subtracted the answer is zero and the zero flag will be set. Every significant relation between the destination
and source is evident from the sign flag, carry flag, zero flag, and the overflow flag.

The following tables list the jump instructions available in 8088 assembly language for different scenarios. The
first table lists the instructions for unsigned source and unsigned destination while the second table lists the
instructions for signed source and unsigned destination.

Unsigned Source and Unsigned Destination

Signed Source and signed Destination

48
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Miscellaneous Jumps

3. Walkthrough Task
The following program adds 10 numbers and stores the result in ax. The sum is then moved to the location
‘total’. The program uses jne (jump if not equal) to jump to label”l1” if bx is not equal to 20 (line 8).

49
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Tasks

Practice Task 1 [Expected time = 20mins]


Write a program to find the maximum number and the minimum number from an array of ten numbers.

Practice Task 2 [Expected time = 30mins]


Write a program to find all prime numbers from 1 to 100.

50
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
5. Evaluation Task (Unseen) [Expected time = 30 min]
The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

51
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-10
Unconditional Jumps, Sorting Algorithm

52
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
The purpose of this lab is to introduce the students to unconditional jump. Additionally, we will write a
complete program to sort an array of unsigned integers.

2. Concept Map
Till now we have been placing data at the end of the code. There is no such restriction and we can define data
anywhere in the code. However, if we place data at the start of code the debugger is intending to start
execution at our data since the COM file definition said that the first executable instruction is at offset 0100
and we have placed data there instead of code.

We introduce a new instruction called JMP. It is the unconditional jump that executes regardless of the state
of all flags.

So, we write an unconditional jump as the very first instruction of our program and jump to the next
instruction that follows our data declarations.

Now that we are familiar with both conditional and unconditional jumps, we will use these to write a
complete program to sort an array of unsigned integers using bubble sort.

Bubble Sort
In this algorithm we compare consecutive numbers. If they are in required order then we leave them as it is
and if they are not in order, we swap them. Then we do the same process for the next two numbers and so on
till the last two are compared and possibly swapped. A complete iteration is called a pass over the array. We
need N passes at least in the simplest algorithm if N is the number of elements to be sorted. A finer algorithm
is to check if any swap was done in this pass and stop as soon as a pass goes without a swap.

53
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

3. Walkthrough Task
The following is c++ implementation of the bubble sort sorting algorithm.

54
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

To convert this program into an equivalent assembly language program, it is always a good idea to write the
program using only go-to statements as conditional jumps in assembly language work in a similar way. The
equivalent program using only go-to statements is given below.

55
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

The equivalent assembly language program has been discussed in the lecture and is given below for your
reference.

56
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 30mins]
Write a program to calculate the factorial of a number where factorial is defined as:

factorial(x) = x*(x-1)*(x-2)*...*1

factorial(0) = 1

Store the number in the register AX. Store the result (factorial) in the register BX. Implement the logic without
using recursion (as we have not studied subroutines in assembly language till now).

Practice Task 2 [Expected time = 20mins]


Write a program to compute the sum of first 50 even numbers.

5. Evaluation Task (Unseen) [Expected time = 30 min ]


57
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

58
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-11
Instructions Related to Shifting and Rotation

59
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
The purpose of this lab is to introduce the students to an important set of instructions in our processor used
for shifting and rotating. We will study several instructions in this lab and will use some of these to implement
multiplication of two 8-bit numbers.

2. Concept Map
A brief summary of the operation of instructions related to shifting and rotation is given below.

Shift Logical Right (SHR)

The shift logical right operation inserts a zero from the left and moves every bit one position to the right and
copies the rightmost bit in the carry flag.

Shift Logical Left (SHL) / Shift Arithmetic Left (SAL)

The shift logical left operation is the exact opposite of shift logical right. In this operation the zero bit is
inserted from the right and every bit moves one position to its left with the most significant bit dropping into
the carry flag. Shift arithmetic left is just another name for shift logical left.

Shift Arithmetic Right (SAR)

A signed number holds the sign in its most significant bit. If this bit is 1 a logical right shifting will change the
sign of this number because of insertion of a zero from the left. The operation of shift arithmetic right is
therefore to shift every bit one place to the right with a copy of the most significant bit left at the most
significant place. The bit dropped from the right is caught in the carry.

Rotate Right (ROR)

In the rotate right operation every bit moves one position to the right and the bit dropped from the right is
inserted at the left. This bit is also copied into the carry flag.

60
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Rotate Left (ROL)

In the operation of rotate left instruction, the most significant bit is copied to the carry flag and is inserted
from the right, causing every bit to move one position to the left.

It is the reverse of the rotate right instruction. Rotation can be of eight or sixteen bits.

Rotate Through Carry Right (RCR)

In the rotate through carry right instruction, the carry flag is inserted from the left, every bit moves one
position to the right, and the right most bit is dropped in the carry flag.

Effectively this is a nine bit or a seventeen bit rotation instead of the eight or sixteen bit rotation as in the case
of simple rotations

Rotate Through Carry Left (RCL)

The exact opposite of rotate through carry right instruction is the rotate through carry left instruction.

In its operation the carry flag is inserted from the right causing every bit to move one location to its left and
the most significant bit occupying the carry flag.

61
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Multiplication Algorithm

• Set the result to zero.

• Then we check the right most bit of the multiplier (If we test the carry flag using JC (and JNC) after a
right shift, we are effectively testing the right most bit of the multiplier).

• If it is 1 then add the multiplicand to the result

• if it is 0 then no addition must be performed.

• Left shift the multiplicand before the next bit of multiplier is tested.

• The left shifting of the multiplicand is performed regardless of the value of the multiplier’s
right most bit.

• Check the next bit of the multiplier and if it is 1 add the shifted value of the multiplicand to the result.

• Repeat for as many digits as there are in the multiplier (4 in the example below)

Example:

62
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
3. Walkthrough Task
Using the above-mentioned multiplication algorithm, in the following we have implemented the multiplication
algorithm.

63
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 20mins]
Store a number in between 0 and 15 in Ax. Then write a program that can set the corresponding bit in BX
(initially BX contains 0). For example, if AX contains 6; your program sets the 6th bit of BX.

Hint: Use shift left to set a particular bit in BX.

Practice Task 2 [Expected time = 30mins]


Write a program that does the following.

Store a non-zero number in AX. Count the number of ones in it and store the result back in AX. Repeat the
process on the result (AX) until AX contains one. Calculate in BX the number of iterations it took to make AX
one.

For example,

AX = 1100 0101 1010 0011 (input – 8 ones)

AX = 0000 0000 0000 1000 (after first iteration – 1 one)

AX = 0000 0000 0000 0001 (after second iteration – 1 one) STOP

BX should contain 2 for the above-mentioned example.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

64
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-12
Bit-wise Logical Operations

65
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
The purpose of this lab is to introduce the students to bitwise logical operations in 8086/8088 Assembly
language.

2. Concept Map
The 8088 processor provides us with a few logical operations that operate at the bit level. The four basic
operations are AND, OR, XOR, and NOT. The important thing about these operations is that they are bitwise.
This means that if “and ax, bx” instruction is given, then the operation of AND is applied on corresponding bits
of AX and BX. There are 16 AND operations as a result; one for every bit of AX. Bit 0 of AX will be set if both its
original value and Bit 0 of BX are set, bit 1 will be set if both its original value and Bit 1 of BX are set, and so on
for the remaining bits. These operations are conducted in parallel on the sixteen bits.

AND performs the logical bitwise and of the two operands (byte or word) and returns the result to the
destination operand.

OR performs the logical bitwise “or” of the two operands (byte or word) and returns the result to the
destination operand.

XOR (Exclusive Or) performs the logical bitwise “exclusive or” of the two operands and returns the result to
the destination operand.

NOT inverts the bits (forms the one’s complement) of the byte or word operand. Unlike the other logical
operations, this is a single operand instruction.

3. Walkthrough Task
The following simple program demonstrates the use of all of the above-mentioned instructions.

66
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 10mins]
Store a number in between 0 and 15 in Ax. Then write a program that can set the corresponding bit in BX
(initially BX contains 0000). For example, if AX contains 6; your program sets the 6th bit of BX.

Use OR instruction to set the corresponding bit using some appropriate mask.

Practice Task 2 [Expected time = 10mins]


Store a number in between 0 and 15 in Ax. Then write a program that can clear the corresponding bit in BX
(initially BX contains FFFF). For example, if AX contains 6; your program clears the 6th bit of BX.

Use AND instruction to clear the corresponding bit using some appropriate mask.

Practice Task 3 [Expected time = 10mins]


Store a number in between 0 and 15 in Ax. Then write a program that invert the corresponding bit in BX
(initially BX contains 00FF). For example, if AX contains 6; your program inverts the 6th bit of BX.

Use XOR instruction to clear the corresponding bit using some appropriate mask.

Practice Task 4 [Expected time = 20mins]

67
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
AND can be used to check whether particular bits of a number are set or not. AND is applied on a destination
with a 1-bit in the desired position and a source (which is to be checked). AND would update the destination
as a result.

If the destination is zero as a result, (which can be checked with a JZ instruction) the bit at the desired position
in the source was clear. See the following examples.

Source (to be checked): 1010 1010

Destination: (Mask) 0000 1000

Destination After AND: 0000 1000 (not zero)

Source (to be checked): 1010 0010

Destination: (Mask) 0000 1000

Destination After AND: 0000 0000 (zero)

Using this logic, write a program that does the following.

Store a non-zero number in AX. Count the number of ones in it and store the result back in AX.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

68
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-13
Subroutines, Stack

69
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
The purpose of this lab is to learn how to write subroutines and the working of stack in 8086/8088 assembly
language.

2. Concept Map
In every processor, instructions are available to divert temporarily and to divert permanently. The instructions
for permanent diversion in 8088 are the jump instructions. The instruction for temporary diversion is the CALL
instruction.

The CALL instruction allows temporary diversion and therefore reusability of code. We can use CALL
instruction followed by a label. This label will indicate the start of the subroutine. Execution will divert to the
code following the label. When the subroutine completes, we need to return and the RET instruction is used
for this purpose. No explicit destination is required and it is implicit that we need to return where we came
from. Therefore, RET takes no arguments and transfers the control back to the instruction following the CALL
that took us to this subroutine.

Note:

In AFD, using F2 to step through the program will not show us the detailed execution of the sub-routine. Use
F1 instead.

3. Walkthrough Task
The following program demonstrates how to declare and call a subroutine “test”. test (line 5) is the start of
our subroutine. The main difference is that we do not use jump to go to this label. We use CALL. CALL will save
the address of the instruction from where we went to this sub-routine. Then when we use RET at the end of
the subroutine, it will use that address to go back to the point just after the call instruction.

70
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 30mins]
Write a subroutine that can find the maximum number in an array of size 10. Call this subroutine for the
following 5 arrays (declare these arrays using appropriate labels at the start of the program. Use db to declare
a number).

1234567320

32 43 12 1 78 45 34 52 76 34

22 33 11 19 87 43 4 2 6 84

54 67 43 29 85 68 72 39 91 20

0 1 2 3 4 5 6 7 20 9

Pass the address of an array to the subroutine using ax register. Declare a label “results” in your program to
store the results.

At the end of the subroutine, save the largest number at the label results as a 16-bit number. For each
subsequent subroutine call, store the number at the subsequent address. Hence result will store 7 (the largest
number in the first array, results+2 will store 78 and so on.

At the end of the program, add the 5 numbers stored at the label “results”. Store the sum of these numbers in
ax. Verify your results. (Your answer should be 7 + 78 + 87 + 91 + 20 = 283)

Practice Task 2 [Expected time = 30mins]


Stack is a data structure that behaves in a Last in First Out manner (LIFO). When an element is inserted, it sits
on top of all other elements and when an element is removed the one sitting at the top is removed first. We
can push any data onto the stack using PUSH. We can use POP to pop data from the top of the stack.

For example, “push ax” will push the current value of the AX register on the stack.

The operation of POP is the reverse of this. A copy of the element at the top of the stack is made in the
operand. For example, pop ax will pop whatever is the top of the stack and will store it in ax.

Write a program that does the following.

Declares an array of 10 elements in the memory. You can use the following array for reference.

1 2 3 4 5 6 7 8 9 10

Your program then stores this array in reverse order at some memory address identified by label “result”. Use
PUSH and POP to reverse the array along with some loop to iterate over the array.

71
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

72
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

Lab-14
Parameter Passing through Stack

73
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________
1. Introduction
The purpose of this lab is to introduce the students to the concept of passing parameters to subroutines
through Stack.

2. Concept Map
When we write a subroutine, we may need to pass it some parameters. Due to the limited number of
registers, parameter passing by registers is constrained. The maximum parameters a subroutine can receive
are seven when all the general registers are used. It becomes more complicated when the subroutine has to
make a nested call thereby using certain registers as its parameters. Hence parameter passing by registers is
not expandable and generalizable. We can use stack as an alternative.

Before calling a subroutine, we can push the required parameters onto the stack. We can then call the
subroutine. Inside the subroutine when we try to access these parameters, there is a small problem. When we
call the subroutine using CALL, the return address (the address where this subroutine needs to return) will
also be put on the stack as CALL and RET also use Stack.

Hence inside the subroutine, if we use pop, the first thing popped off the stack would be the return address
and not the parameters. This is because the arguments were first pushed on the stack and the subroutine was
called afterwards. The arguments cannot be popped without first popping the return address.

We can use BP to access the parameters. Default segment association of the BP register is the stack segment
It allows us to peek inside the stack and read the parameters without removing them and without touching
the stack pointer. The base pointer is provided as a replacement of the stack pointer so that we can peek
inside the stack without modifying the structure of the stack.

When a subroutine is called, the stack pointer is pointing to the return address (which is at the top of the
stack). Two bytes below it (+2) is the second parameter and four bytes below (+4) is the first parameter.

If the value of SP is captured in BP, then the return address is located at [bp+0], the second parameter is at
[bp+2], and the first parameter is at [bp+4]. This copying of SP into BP is like taking a snapshot. However, we
must save the old value of BP before copying SP to it.

The first two instructions of any subroutine accessing its parameters from the stack are given below.

push bp

mov bp, sp

With this we have saved the old value of BP but it will also push the return address further down. This old
value of BP will be contained at [bp] and the return address now will be at [bp+2]. The second parameters
now will be [bp+4] while the first one will be at [bp+6].

We must also save any register we use in our subroutine onto the stack and then at the end of the subroutine
we must restore it.

The following walkthrough task explains all these concepts using an example.

74
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

3. Walkthrough Task
In this task we will write a subroutine and pass two parameters to it using the stack. Inside the subroutine we
will get these parameters.

The two parameters are address of an array and the element count.

75
Department of Computer Science,
Faculty of Computing, Capital University of Science and Technology
_____________________________________________________________________________________

4. Practice Task
Practice Task 1 [Expected time = 30mins]
Write a subroutine that can find the maximum number in an array of any size. Call this subroutine for the
following multiple arrays (declare these arrays using appropriate labels at the start of the program. Use db to
declare a number).

Pass the address of the array and its element count to the subroutine using the stack.

Declare a label “results” in your program to store the results.

At the end of the subroutine, save the largest number at the label results as a 16-bit number. For each
subsequent subroutine call, store the number at the subsequent address.

Practice Task 2 [Expected time = 30mins]


Write a subroutine to calculate the factorial of a number where factorial is defined as:

factorial(x) = x*(x-1)*(x-2)*...*1

factorial(0) = 1

Implement the logic using recursion.

Call the subroutine with different values and verify your results.

5. Evaluation Task (Unseen) [Expected time = 30mins for tasks]


The lab instructor will give you unseen task depending upon the progress of the class.

6. Evaluation criteria

Sr. No. Description Marks


1 Understanding of Problem 20
2 Program Logic 20
3 Program Implementation 20
4 Program Correctness 10
5 Use of Tool 10
6 Viva 20
Total 100

76

You might also like