You are on page 1of 15

NAME: Sheikh Abuzar Amjad

REG NO: 19-CP-6

LAB MANUAL # 03

Implementation of Flag Registers

FLAG REGISTER

8086 has 16 flag registers among which 9 are active. The purpose of the FLAG register is to
indicate the status of the processor. It does this by setting the individual bits called flags. There are
two kinds of FLAGS;

Status FLAGS and Control FLAGS. Status FLAGS reflect the result of an operation executed by
the processor. The Control FLAGS enable or disable certain operations of the processor.

1. Carry Flag(CF): holds the carry out after addition or borrow after subtraction. Also indicates
error condition.

2. 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.

3. Auxiliary Flag(AF): The auxiliary carry holds the carry (half-carry) after addition or the
borrow after subtraction between bit positions 3 and 4 of the result.

4. Zero Flag (ZF): set to 1 when result is zero. For non-zero result this flag is set to 0.

5. Sign Flag (SF): set to 1 when result is negative. When result is positive it is set to0. (This
flag takes the value of the most significant bit.)

6. Trap Flag (TF): Used for on-chip debugging.

7. Interrupt enable Flag (IF): when this flag is set to 1 CPU reacts to interrupts from external
devices.

8. Direction Flag (DF): this flag is used by some instructions to process data chains, when
this flag is set to 0 - the processing is done forward, when this flag is set to 1the processing
is done backward.

9. Overflow Flag (OF): An overflow indicates that the result has exceeded the capacity of the
machine.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Describe briefly:

1. What is Emulator and how it helps a hardware/embedded system designer?


Answer:
An emulator is a hardware kit or a software program or can be both which emulates the
functions of one computer system (the guest) in another computer system (the host),
different from the first one, so that the emulated behavior closely resembles the behavior
of the real system (the guest).
Emulation refers to the ability of a computer program in an electronic device to emulate
(imitate) another program or device. Emulation focuses on recreating an original computer
environment. Emulators have the ability to maintain a closer connection to the authenticity
of the digital object. An emulator helps the user to work on any kind of application or
operating system on a platform in a similar way as the software runs as in its original
environment.

How it Helps Hardware Designers:


As emulator is a software based tool so it helps in a way that a hardware/embedded system
designers can make a program according to upcoming new system before the hardware
implementation of the system in order to test its working and errors. So it saves a lot of cost and
efforts. An emulator regenerates an original computer environment with the help of
software and hardware. The process of creating an authentic emulator is complex and
time consuming. But once created, it provides the authenticity of the original computer
environment/digital object without the need for the original system.Emulation
techniques are applied to re-create the hardware and software environment of a
computer system on a different machine. Once the emulator is complete, users can
access applications or the OS on the emulated system and the original software can
run on the host system. To the users, the experience is the same as if they were using
ch

Emulators are usually composed of three components:

 CPU emulator (the most complex part)


 Memory sub-system emulator
 Different input/output device emulators
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

2. What is the difference between machine code and assembly language?


Answer:
Machine Code Assembly Language

1. Machine code is a language consisting of binaries 1. Assembly language is an intermediate language


that can be directly executed by a computer . between high-level language and machine code. It is
one level above machine code and one level below
high-level languages.
2. Machine language is only understand by 2. Assembly language is only understand by
the computers. human beings not by the computers.
3. Modifications and error fixing cannot be 3. Modifications and error fixing can be done
done in machine language. in assembly language by means of compiler.
4. Machine language is very difficult to 4. Easy to memorize the assembly language
memorize so it is not possible to learn the because some alphabets and mnemonics
machine language. are used.
5. Execution is fast in machine language as 5. Execution is slow as compared to machine
all instruction is present in binary format language.
already.
6.It is hardware dependent. 6. It is machine dependent.

3.Define Purpose of IP register?


Answer:
The IP register contains the address of the next instruction to fetch and execute. Normally,
IP is incremented by the number of bytes in the instruction after execution of that
instruction, unless a transfer of control occurs, in which case IP is loaded with a new value.

4.Write down the purpose of AX,BX, CX and DX Registers?


Answer:
8086 has four 16-bit general-purpose registers AX, BX, CX and DX. These are available to the
programmer, for storing values during programs. Each of these can be divided into two 8-bit
registers such as AH, AL; BH, BL; CL, CH and DL, DH. 

 AX Register (16-Bits): It holds operands and results during multiplication and division
operations. All IO data transfers using IN and OUT instructions use A register (AL/AH or
AX). It functions as accumulator during string operations.
 BX Register (16-Bits): It holds the memory address (offset address), in Indirect Addressing
modes.
 CX Register (16-Bits): It holds count for instructions like: Loop, Rotate, Shift and String
Operations.
 DX Register (16-Bits): It is used with AX to hold 32 bit values during Multiplication and
Division. It is used to hold the address of the IO Port in indirect IO addressing mode.

5.How flags in a microprocessor help a hardware designer?


Answer:
Flags are a modified kind of register that record the condition of a microprocessor's calculation. For
instance, a "zero status" flag is activated only when the microprocessor's calculation concludes with
a "zero" status. The status of each flag determines the microprocessor's next action, thus enabling it
to make decisions. Flag register does the process of setting individual bit. Flags are an important
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

component of microprocessors that helps the hardware designers in the designing of


physical memory allocation as the flags, register the outcomes of calculations and
actions.
Lab Tasks

Execute the following tasks CLO


[1]

Task 1
What effect on Conditional Flags will happens after the addition of 10110001 and 10101011? (write
state of each of the flag as observed, note values of flags after execution of every single instruction
in the program).

Solution:
Step1:
In this first step we loaded the program for execution and as it loaded into memory it sets “IF
(Interrupt Flag)” to 1 and shows it is enabled.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Step2:
After that in next step, the command “MOV” command is executed and value is loaded in “AX”
register in form of Hex value and value of all Flags remains set to zero except “IF”.

Step3:
In next step, we add 10101011b into AX. Only the parity flag and Auxiliary Flag is set to 1 because
the number of One’s in the answer is 5 (odd number) this shows that my system is odd. All other
flags are set to 0.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Step 4:
In next step, only the value of “IP” changes all the rest remains the same.

Step 5:
In the last step , only PF and AF is active and set to “1” and all the rest set to zero.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Task 2
Add two numbers in BIN, save the result in AX register and observe the value of flags.
1000 0000 0000 0000
1100 1000 0000 0000
(write state of each of the flag as observed, note values of flags after execution of every single
instruction in the program

Solution:
Step1:
In First step I used “BX” register to store values and loaded the program for execution with “IF” set
to 1.

Step2:
In next step command is executed and value is moved into “BX” register but no changes in the
values of flag registers.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Step3:
In the next step, the value 1100100000000000 is added to “BX” and “CF (Carry Flag)” and “OF
(Overflow Flag)” and “PF” are changes value and set to “1”. In the last step we add AX and BX.
so all the flags are again set to 0. In this step Cary flag is set to 1 because result of the addition is
14800H only 4800 is shown and 1 is the carry. Also Overflow flag is set to 1 indicating that the result
has exceeded the capacity of the machine. Also Parity flag is set to 1 because number of One’s in
the answer is 3 (odd number) this shows that my system is odd. All other flags are set to 0.

Step4:
In the last step the final result is stored in “AX” and there are no changes except “IF” set to 0.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Task 3
Check out the status of flags for the following examples?

Example 1:-
MOV DX, 126FH;
ADD DX, 3465H;
MOV BX, 0FFFFH;
ADD BX, 1;
Use Single Step and observe changes in flags after executing every single statement. Also, convert
the numbers to binary and prove the results(manually) as observed using emulator.

Solution:
Step1:
In first step we move 12 6FH into DX. All flags are set to 0 except “IF”.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Step2:
In step 2 Parity and Auxiliary flags are set to the 1.

Step3:
In step we move FFFFH into BX the flag values are same as in step 2.

Step4:
In last step we add 1H into BX and see that Zero and Cary flags are set to 1. Zero flag indicates
that BX contains 0 value and Carry flag indicates that carry obtained as a result of addition.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Example 2:-
MOV BL,+8
MOV DH,+4
ADD BL,DH

Solution:
In This Example the value with + sign are loaded into registers and displayed as “00” and their
addition makes changes in the “Parity Flag (PF)” and “Zero Flag (ZF)” as set to “1”.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Example 3:-
MOV AL, +66
MOV CL, +69
ADD CL,AL

Solution:
Step1&2:
In first two steps values are loaded to respective registers only and all flags are set to “0”.
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Step3:
In last step, the “Parity Flag” and “Zero Flag” are set to 1.

Example 4:-
MOV AL, -12
MOV BL, +18
ADD BL,AL

Solution:
After successful execution of the code Sign Flag is set to 1 showing that answer is some negative
value
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

Example 5:-
MOV AH, -30
MOV DL, +14
ADD DL, AH

Solution:
After successful execution of the code Sign and Parity flags are set to 1
NAME: Sheikh Abuzar Amjad
REG NO: 19-CP-6

The End

You might also like