National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
Department of Computer Science
CS-235: Computer Organization and Assembly Language (COAL)
Class: BSCS 2021 Batch
Lab Manual # 08: Branching in Assembly Language
Date: 08th Dec 2022
Time: 11 am - 01 pm
Instructor: Mr Najeeb Ullah
Lab Engineer: Engr Muhammad Abid Hussain
CS235: Computer Organization and Assembly Language Page 1
National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
Lab 08: Branching in Assembly Language
Introduction
This lab consists of the if-else, Loops and arrays and prints the instruction of Assembly
Language on NASM AFD.
Objectives:
The if-else statement executes a block of code if a specified condition is true. If the
condition is false, another block of code can be executed. The if/else statement is a part of
JavaScript's "Conditional" Statements, which are used to perform different actions based on
different conditions, Loops and Arrays on performed in this lab.
Tools/Software Requirement:
Linux (Ubuntu 20.04.3 LTS) NASM + AFD DOSBox
Helping Material:
Lecture slides, Textbook and Internet
Description:
In this task, we had to check whether the given piece of code was true or false with the help of an if-
else statement.
CS235: Computer Organization and Assembly Language Page 2
National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
Q 01: What is branching in detail?
Branching is a basic concept in computer science. It means an instruction that tells a computer to
begin executing a different part of a program rather than executing statements one-by-one.
Branching is implemented as a series of control flow statements in high-level programming
languages. These can include:
If statements
For loops
While loops
Goto statements
Branching instructions are also implemented at the CPU level, though they are much less
sophisticated than the kinds of instructions found in high-level languages. In assembly language
programming there are some instructions that are used for branching or switching the execution to a
different instruction sequence as a result of executing a branch instruction.
There are three types of branching instructions:
1. Jump (unconditional and conditional).
2. Call (unconditional and conditional).
3. Return (unconditional and conditional).
1. Jump Instructions: The jump instruction transfers the control flow or sequence of execution
to the desired memory address from where the user wants to continue the execution of the
program. Jump instructions are of two types: Unconditional and Conditional Jump.
a) Unconditional Jump: In this type of jump the program sequence is transferred to the
described memory address (label) without any check or condition.
Ex: JMP add (or) JMP FA11 ; Jumps to the label add.
b) Conditional Jump: Transfers the program sequence to the described memory address
only if the condition is true.
Ex: JC add ; Jumps to the address if carry flag is 1
Ex: JNC add ; Jumps to the address if carry flag is 0
Ex: JZ add ; Jumps to the address if zero flag is 1
Ex: JNZ add ; Jumps to the address if zero flag is 0
2. Call Instructions: The call instruction calls near procedures using a full pointer. Call causes
the procedure named in the operand to be executed. When the called procedure completes,
execution flow resumes at the instruction following the call instruction (see
the return instruction).
(a) Unconditional Call Instructions: It transfers the program sequence to the memory address given
in the operand. Ex: CALL add
(b) Conditional Call Instructions: Only if the condition is satisfied, the instructions executes.
Ex: CC ; Call if carry flag is 1
Ex: CNZ ; Call if carry flag is 0
CS235: Computer Organization and Assembly Language Page 3
National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
Ex: CZ ; Call if zero flag 1
3. Return Instructions: The return instruction transfers the program sequence from the
subroutine to the calling program. Return instructions are 2 types: Unconditional Jump
Instructions and Conditional Jump Instructions
a) Unconditional Return Instruction: The program sequence is transferred unconditionally
from the subroutine to the calling program. Ex: RET; Return from the subroutine
unconditionally.
b) Conditional Return Instruction: The program sequence is transferred unconditionally from
the subroutine to the calling program only is the condition is satisfied.
Ex: RC ; Return from the subroutine if carry flag is 1
Ex: RNC ; Return from the subroutine if carry flag is 0
Q 02: Difference between CMP & SUB using an example with a screenshot of AFD?
The CMP is used for comparing the numeric value with the source value to the
destination value in the Microprocessor 8086 whereas sub is used for subtract the
source value to the destination numeric value in the Microprocessor 8086.
In the CMP the comparison is done in the subtraction form also we set the OF, SF, ZF
and CF flag according the value whereas in the sub the F flag is used for the borrow
in the subtraction.
CMP is used for comparing 2 registers by subtraction them from one another, but
answer is not saved, whereas SUB subtracts 2 registers and saves the answer
SUB instruction:
CS235: Computer Organization and Assembly Language Page 4
National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
CMP instruction:
CS235: Computer Organization and Assembly Language Page 5
National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
Q 03: Write a program using Conditional and unconditional jumps.
Code screenshot:
CS235: Computer Organization and Assembly Language Page 6
National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
Reference link:
1. https://theiteducation.com/how-to-install-nasm-on-windows-10-how-to-type-and-run-
assembly-language-program/
2. https://phoenixnap.com/kb/install-ubuntu-20-04
3. https://www.tutorialspoint.com/assembly_programming/assembly_addressing_modes.htm
4. https://github.com/soothscier/assembly-nasm
5. https://www.hostinger.com/tutorials/linux-commands
Deliverables:
Compile a single word document by filling in the solution part and submitting this
Word file to the MS Team. This lab grading policy is as follows: The lab is graded between 0
to 10 marks. The submitted solution can get a maximum of 5 marks. At the end of each lab
or in the next lab, there will be a viva related to the tasks. The viva has a weightage of 5
marks. Insert the solution/answer in this document. You must show the implementation of
CS235: Computer Organization and Assembly Language Page 7
National University of Sciences & Technology (NUST)
NUST Balochistan Campus (NBC), Quetta
the tasks in the designing tool, along with your complete Word document to get your work
graded. You must also submit this Word document to the MS Team. In case of any problems
with submissions on the MS Team, submit your Lab Manual by emailing them to Engr.
Muhammad Abid Hussain [email protected]
CS235: Computer Organization and Assembly Language Page 8