COMPUTER ORGANIZATION
AND ASSEMBLY LANGUAGE
LAB MANUAL 2
DEPARTMENT OF COMPUTING
28
© Copy Rights, Department of Computer Science, STMU Islamabad
All Rights Reserved
Prepared By: Mr. Shahid Raza
Revised By. Engr. Muhammad Haris Farooq
DEPARTMENT OF COMPUTING
29
List of Experiments
• How General purpose Registers Works
• FLAG Registers
• Status Registers
• Control Registers
30
Lab Number: 2
Lab Title:
To implement Arithmetic Operation and check
status of Flag Registers
v
DEPARTMENT OF COMPUTING
31
To implement Arithmetic Operation and check status of Flag Registers
Simple Assembly Language Programs 8086
The assembly language programming 8086 has some rules such as:
• The assembly level programming 8086 code must be written in upper case
letters
• The labels must be followed by a colon, for example: label:
• All labels and symbols must begin with a letter
• All comments are typed in lower case
• The last line of the program must be ended with the END directive
• 8086 processors have two other instructions to access the data, such as WORD
PTR – for word (two bytes), BYTE PTR – for byte.
Figure [Link]-Code and Operand
Op-code: A single instruction is called as an op-code that can be executed by
the CPU. Here the ‘MOV’ instruction is called as an op-code.
Operands: A single piece data are called operands that can be operated by the op-code.
Example, subtraction operation is performed by the operands that are subtracted by the
operand.
Syntax: SUB b, c
32
General purpose registers in 8086 microprocessor
General-purpose registers are used to store temporary data within the microprocessor.
There are 8 general-purpose registers in the 8086 microprocessor.
Figure 2. 8086 Registers
33
Figure 3. CPU Architecture
AX: This is the accumulator. It is of 16 bits and is divided into two 8-bit registers
AH and AL to also perform 8-bit instructions. It is generally used for arithmetical
and logical instructions but in 8086 microprocessor it is not mandatory to have an
accumulator as the destination operand. Example:
ADD AX, AX (AX = AX + AX)
BX: This is the base register. It is of 16 bits and is divided into two 8-bit registers
BH and BL to also perform 8-bit instructions. It is used to store the value of the
offset. Example:
MOV BL, [500] (BL = 500H)
CX: This is the counter register. It is of 16 bits and is divided into two 8-bit registers
CH and CL to also perform 8-bit instructions. It is used in looping and rotation. Example:
MOV CX, 0005
LOOP
34
DX: This is the data register. It is of 16 bits and is divided into two 8-bit registers DH and
DL to also perform 8-bit instructions. It is used in the multiplication and input/output port
addressing. Example:
MUL BX (DX, AX = AX * BX)
SP: This is the stack pointer. It is of 16 bits. It points to the topmost item of the stack. If the
stack is empty the stack pointer will be (FFFE)H. Its offset address is relative to the stack
segment.
BP – This is the base pointer. It is of 16 bits. It is primarily used in accessing parameters
passed by the stack. Its offset address is relative to the stack segment.
SI – This is the source index register. It is of 16 bits. It is used in the pointer addressing of
data and as a source in some string-related operations. Its offset is relative to the data
segment.
DI – This is the destination index register. It is of 16 bits. It is used in the pointer
addressing of data and as a destination in some string-related operations. Its offset is
relative to the extra segment.
Flag Registers
The flag register is a 16-bit register in the Intel 8086 microprocessor that contains
information about the state of the processor after executing an instruction. It is sometimes
referred to as the status register because it contains various status flags that reflect the
outcome of the last operation executed by the processor.
The flag register is an important component of the 8086 microprocessor because it is used
to determine the behavior of many conditional jump and branch instructions. The various
flags in the flag register are set or cleared based on the result of arithmetic, logic, and other
instructions executed by the processor.
The flag register is divided into various bit fields, with each bit representing a specific
flag. Some of the important flags in the flag register include the carry flag (CF), the zero
flag (ZF), the sign flag (SF), the overflow flag (OF), the parity flag (PF), and the auxiliary
carry flag (AF). These flags are used by the processor to determine the outcome of
35
conditional jump instructions and other branching instructions.
The flag register is a 16-bit register in the Intel 8086 microprocessor that contains
information about the state of the processor after executing an instruction. It is sometimes
referred to as the status register because it contains various status flags that reflect the
outcome of the last operation executed by the processor.
The flag register is an important component of the 8086 microprocessor because it is used
to determine the behavior of many conditional jump and branch instructions. The various
flags in the flag register are set or cleared based on the result of arithmetic, logic, and other
instructions executed by the processor.
Purpose Register. Depending upon the value of result after any arithmetic and logical
operation the flag bits become set (1) or reset (0).
Figure 4. Format of flag register
There are total 9 flags in 8086 and the flag register is divided into two types:
Status Flags
There are 6 flag registers in 8086 microprocessor which become set(1) or reset(0)
depending upon condition after either 8-bit or 16-bit operation. These flags are
conditional/status flags. 5 of these flags are same as in case of 8085 microprocessor and
their working is also same as in 8085 microprocessor. The sixth one is the overflow flag.
The 6 status flags are:
Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow. For example
when you add bytes 255 + 1 (result is not in range 0...255). When there is no overflow this
flag is set to 0.
Zero Flag (ZF) - set to 1 when result is zero. For none zero result this flag is set to 0.
Sign Flag (SF) - set to 1 when result is negative. When result is positive it is set to 0.
Actually this flag take the value of the most significant bit.
36
Parity Flag (PF) - this flag is set to 1 when there is even number of one bits in result, and to 0
when there is odd number of one bits. Even if result is a word only 8 low bits are analyzed!
Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low nibble (4 bits).
Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to interrupts from external
devices.
Overflow Flag (O) – This flag will be set (1) if the result of a signed operation is too large
to fit in the number of bits available to represent it, otherwise reset (0). After any operation,
if D[6] generates any carry and passes to D[7] OR if D[6] does not generates carry but D[7]
generates, overflow flag becomes set, i.e., 1. If D[6] and D[7] both generate carry or both
do not generate any carry, then overflow flag becomes reset, i.e., 0.
Example: On adding bytes 100 + 50 (result is not in range -128…127), so overflow
flag will set.
MOV AL, 50 (50 is 01010000 which is positive)
MOV BL, 32 (32 is 00110010 which is positive)
ADD AL, BL (82 is 10000010 which is negative)
Overflow flag became set as we added 2 +ve numbers and we got a -ve number.
Control Flags
The control flags enable or disable certain operations of the microprocessor. There
are 3 control flags in 8086 microprocessor and these are:
Directional Flag (D) – This flag is specifically used in string instructions. If
directional flag is set (1), then access the string data from higher memory location
towards lower memory location. If directional flag is reset (0), then access the string
data from lower memory location towards higher memory location.
Interrupt Flag (I) – This flag is for interrupts. If interrupt flag is set (1), the
microprocessor will recognize interrupt requests from the peripherals. If interrupt
37
flag is reset (0), the microprocessor will not recognize any interrupt requests and will
ignore them.
Trap Flag (T) – This flag is used for on-chip debugging. Setting trap flag puts the
microprocessor into single step mode for debugging. In single stepping, the
microprocessor executes a instruction and enters into single step ISR. If trap flag is
set (1), the CPU automatically generates an internal interrupt after each instruction,
allowing a program to be inspected as it executes instruction by instruction. If trap
flag is reset (0), no function is performed.
Uses of Flag register in 8086 microprocessor:
The flag register in the 8086 microprocessor has several important uses, including:
Conditional branching: The flags in the flag register can be used to control conditional
branching in assembly language programming. Conditional jump instructions allow a
program to take different paths based on the state of the flags in the flag register.
Arithmetic and logic operations: The flag register is used to store the results of
arithmetic and logic operations. The flags in the flag register provide information about
the outcome of these operations, such as whether a result is negative or zero, or
whether there was an overflow or carry.
Error detection and handling: The flag register can be used to detect errors and
exceptions, such as overflow or divide-by-zero errors. This allows programs to handle
these errors gracefully and to take appropriate corrective action.
Debugging: The flag register provides a convenient way to access important
information about the status of the processor after executing an instruction. This
information can be used to debug programs and to optimize performance.
Optimization: The flag register can be used to optimize the performance of assembly
language programs by avoiding unnecessary instructions or reducing the number of
conditional jumps required.
38
Advantages:
The flag register in the 8086 microprocessor provides several advantages, including:
Efficient conditional branching: The flag register enables efficient conditional
branching in assembly language programming. Programmers can use conditional jump
instructions to make decisions based on the state of the flags in the flag register,
allowing for more efficient and optimized code.
Improved arithmetic and logic operations: The flag register is used to store the results
of arithmetic and logic operations, allowing for more complex calculations to be
performed efficiently. The various flags in the flag register provide information about
the outcome of these operations, such as whether a result is negative or zero, or
whether there was an overflow or carry.
Easy access to processor status information: The flag register provides a convenient
way to access important information about the status of the processor after executing an
instruction. This information can be used to debug programs and to optimize
performance.
Improved error handling: The flag register can be used to detect errors and exceptions,
such as overflow or divide-by-zero errors. This allows programs to handle these errors
gracefully and to take appropriate corrective action.
Dis-advantages:
There are not many disadvantages of the flag register in the 8086 microprocessor, but
some potential drawbacks include:
Limited number of flags: The 8086 flag register has a limited number of flags, which
can make it difficult to handle complex calculations or to detect certain types of errors
or exceptions.
Limited precision: The flags in the flag register are often limited in precision, which
can lead to inaccuracies or errors in certain types of calculations or operations.
39
Difficulty in understanding and using: The flag register can be difficult for beginners to
understand and use effectively, which can lead to mistakes or errors in programming.
Overhead in execution time: Accessing the flag register can add overhead to program
execution time, which can impact performance in certain types of applications.
Add instruction.
Example:
ORG 100h
MOV AL, 5h
ADD AL, -3 h
RET
Sub Instruction
Example:
ORG 100h
MOV AX, 1234H
MOV BX, 3222H
SUB AX, BX
SBB Instruction (Subtraction with borrow)
Example:
ORG 100h
MOV AX, 1234H
MOV BX, 3222H
SUB AX, BX
SBB AX, 1404H
40
Multiplication Instruction
The MUL instruction deals with the multiplication of two unsigned numbers.
Example:
ORG 100h
MOV AL, 2H
MOV BL, 2H
MUL BL
RET
Example:
ORG 100h
MOV AL, 12H
MOV BL, 20H
MUL AL
RET
IMUL Instruction
The IMUL instruction allows the multiplication of two signed operands. The operands can be
positive or negative. The operation of MUL and IMUL instructions is same. The only difference
between two is one deal with the multiplication of unsigned numbers and the other deals with
signed operands.
If the product of multiplier and multiplicand produce result that fits into the destination register
DX and AX with some of the bits left unused. Then these unused bits are filled with the copies of
signed bit and clear CF and OF flags to zero.
ORG 100h
MOV AL, 2AH ; Load 1st
operand to AX MOV BX, -26CH
; Load 2nd operand in BX
IMUL BX
RET
41
Lab Activities
Arithmetic and Logic Instructions
The 8086 processes of arithmetic and logic unit has separated into three groups such as addition,
division, and increment operation. Most Arithmetic and Logic Instructions affect the processor
status register.
The assembly language programming 8086 mnemonics are in the form of op-code, such as MOV,
MUL, JMP, and so on, which are used to perform the operations. Assembly language
programming 8086 examples
Addition
ORG0000h
MOV DX, #07H // move the value 7 to the register AX//
MOV AX, #09H // move the value 9 to accumulator AX//
Add AX, 00H // add CX value with R0 value and stores the result in AX//
END
Multiplication
ORG0000h
MOV DX, #04H // move the value 4 to the register DX//
MOV AX, #08H // move the value 8 to accumulator AX//
MUL AX, 06H // Multiplied result is stored in the Accumulator AX //
END
Subtraction
ORG 0000h
MOV DX, #02H // move the value 2 to register DX//
MOV AX, #08H // move the value 8 to accumulator AX//
SUBB AX, 09H // Result value is stored in the Accumulator A X//
END
Division
ORG 0000h
MOV DX, #08H // move the value 3 to register DX//
MOV AX, #19H // move the value 5 to accumulator AX//
42
DIV AX, 08H // final value is stored in the Accumulator AX //
END
1. Write a Program in emu 8086 assembly language to demonstrate the
functionality and overview the working of status flags?
;
Program to demonstrate the functionality and working of status flags
; without using the data segment
.code
main:
; Demonstrate Carry Flag (CF)
mov ax, 0FFFFh ; Load maximum value in AX
add ax, 1 ; Add 1 to AX, which causes overflow, CF = 1
; Demonstrate Zero Flag (ZF)
mov ax, 1 ; Load 1 in AX
sub ax, 1 ; Subtract 1 from AX, AX becomes 0, ZF = 1
; Demonstrate Sign Flag (SF)
mov ax, 0 ; Clear AX
sub ax, 1 ; Subtract 1 from AX, AX becomes -1 (FFFFh), SF = 1
; Demonstrate Overflow Flag (OF)
mov ax, 7FFFh ; Load maximum positive value in AX
add ax, 1 ; Add 1 to AX, which causes overflow, OF = 1
; Demonstrate Parity Flag (PF)
mov ax, 5 ; Load 5 in AX (binary: 0000 0101, odd parity)
add ax, 0 ; No change, PF = 0 (odd parity)
; Halt the program
mov ah, 4Ch
int 21h
end main
2. Explain the purpose of each flag and provide examples of instructions that affect
these flags.
43
Lab Exercise and Summary
Summary should cover Introduction, Procedure, Data Analysis and Evaluation.
44
Student’s Signature: Date:
45
Evaluation Criteria
Method of Evaluation: Viva, Practical Quiz
Excellent Good Satisfactory Unsatisfactory Poor Marks
Obtained
10 9-7 6-4 3-1 0
Assignment All tasks Most tasks Some tasks Most tasks All tasks were
completed were were were incomplete or
correctly in completed completed incomplete or incorrect.
given time correctly. correctly and incorrect and
and have a Tasks could have an have no Didn’t
complete be improved incomplete understanding perform tasks
understanding further and understanding and have no
understanding
have a
complete
understanding
Total
46