Transcript

Name ID

DULAL CHANDRA BARMAN 00805010

MD.JAHURUL ISLAM 00805034

MD.EMDADUL HUQ 00805038

MD.FARUQ AHMED 00805047

ASHOK KUMAR SARKER 00805049

FLAGS Register:

Individual bits control the action or represent the status of the processor

Control flags (TF, IF, DF):

Determine how the processor responds to certain situations

Status flags (CF, PF, AF, ZF, SF, OF):

Set to represent the result of certain operations

Used to control conditional jump instructions

FLAG REGISTER

FLAG Register Bits:Status Flags:

Bit Name Symbol

0 Carry flag CF

2 Parity flag PF

4 Auxiliary carry flag AF

6 Zero flag ZF

7 Sign flag SF

11 Overflow flag OF

Bit Name Symbol

8 Trap flag TF

9 Interrupt flag IF

10 Direction flag DF

Control Flags:Control Flags:

Status Flags:

The Carry Flag (CF): CF = 1 if there is a carry out from the msb (most

significant bit) on addition, or there is a borrow into the msb on subtraction

CF = 0 otherwise CF is also affected by shift and rotate instructions

The Parity Flag (PF): PF = 1 if the low byte of a result has an even number of

one bits (even parity) PF = 0 otherwise (odd parity)

The Auxiliary Carry Flag (AF): AF = 1 if there is a carry out from bit 3 on addition, or there is

a borrow into the bit 3 on subtraction AF = 0 otherwise AF is used in binary-coded decimal (BCD) operations

The Zero Flag (ZF): ZF = 1 for a zero result ZF = 0 for a non-zero result

The Sign Flag (SF): SF = 1 if the msb of a result is 1; it means the result is

negative if you are giving a signed interpretation SF = 0 if the msb is 0

The Overflow Flag (OF): OF = 1 if signed overflow occurred OF = 0 otherwise

Example 1. ADD AX,BX , where AX contains FFFFh , BX contains FFFFh.

Solution: FFFFh+ FFFFh1FFFEh

The result stored in AX is FFFEh=1111 1111 1111 1110SF = 1 because the msb is 1PF = 0 because there are 7(odd number) of 1 bits in the low byte of

the result .ZF = 0 because the result is nonzero.CF = 1 because there is a carry out of the msb on addition.OF = 0 because the sign of the stored result is the same as that of

the numbers being added (as a binary addition, there is a carry into the msb and a carry out).

Example 2. ADD AX, BX, where AX contains 7132h , BX contains 7000h

Solution: 7132h + 7000h E132h The result stored in AX is E132h=1110 0001 0011 0010 SF = 1 because the msb is 1 PF = 0 because there are 3(odd number) of 1 bits in the

low byte of the result . ZF = 0 because the result is nonzero. CF = 0 because there is no a carry out of the msb on

addition. OF = 1

Example-3: SUB AX, BX , where AX contains 8000h and BX contains 0001h.

Solution: 8000h -0001h 7FFFh The result stored in AX is FFFEh=0111 1111 1111 1111 SF = 0 because the msb is 0 PF = 1 because there are 8(even number) of 1 bits in the low byte

of the result . ZF = 0 because the result is nonzero. CF = 0 because a smaller unsigned number is being subtracted

from a larger one.

Now for OF. In a signal sense, we are subtracting a positive number from a negative one, which is like adding two negatives. Because the result is positive (the wrong sign), OF = 1.

OVERFLOW

The overflow flag is set when the Most Significant Bit (MSB) is set or cleared.

For example, take the addition of 127 and 127. The 8 bit signed binary number of 127 is represented as 0111 1111.

The MSB (the bit to the far left) is 0. When these two 8 bit numbers are added the result is 254, or 1111 1110. Notice now that the MSB is now 1 and not 0.

Therefore, the overflow flag has been set. Here, 1111 1110 would be interpreted as a negative number.

The addition test shows whether the overflow flag has been set.

If two positive operands generate a negative sum. For example, 0101 1100 and 0100 1010 is 1010 0110, or -90.

The MSB changed from 0 to 1. And the two negative operands generate a

positive sum. For example 1100 0010 and 1000 0100 is

01101000 or 104. The MSB changed from 1 to 0, so the overflow

flag is being set. Overflow never occurs when the sign of two

addition operands are different.

(Signed) Overflow

Can only occur when adding numbers of the same sign (subtracting with different signs)

Detected when carry into MSB is not equal to carry out of MSB

Easily detected because this implies the result has a different sign than the sign of the operands

Programs can ignore the Flags

Example 4. ADD AX,BX , where AX and BX both contains 7FFFh .

Solution: Hex Binary 7FFFh 0111 1111 1111 1111 7FFFh 0111 1111 1111 1111 FFFEh 1111 1111 1111 1110

The signed and unsigned interpretation of 7FFFh is 32767. Thus for both signed and unsigned

addition,7FFFh+7FFFh=32767+32767=65534.This is out of range for signed numbers, the signed interpretation of the stored answer FFFEh is -2,so signed overflow occurred .However the unsigned interpretation of FFFEh is 65534,which is the right answer, so there is no unsigned overflow.

Unsigned overflow: On addition, unsigned overflow occurs when there is a carry out of

the msb. On subtraction unsigned overflow occurs when there is a borrow

into the msb.

Example 5. ADD AX, BX, where AX contains FF12h, BX contains 1ACBh.

Solution: FF12h + 1ACBh

119DDhThe result is stored in AX is 19DDh=0001 11 111 1101 FF12h=-1 and 1ACBh=1, and FF12h+1ACBh=-1+1=0, so sign overflow

didnot occur. Here a 1 is carried out of the msb, so unsigned overflow

occurred.

The importance of flag registers:

Flag register has it's importance by following resion.Flag register has it's importance by following resion.1. Sign flag: 1. Sign flag: use to store the sign of any number under processing i.e use to store the sign of any number under processing i.e when arithmetical logic has negative sign it is in set condition. and when arithmetical logic has negative sign it is in set condition. and rest in reset.rest in reset.22.. Zero flag: Zero flag: when ALU operation is zero it is in reset condition.when ALU operation is zero it is in reset condition.3. Auxiliary carry flag: 3. Auxiliary carry flag: it hold the carry during internal processing of it hold the carry during internal processing of addition ,sub , mul and div.addition ,sub , mul and div.44.. Parity flag: Parity flag: hold 1 for even parity,0 for odd parity. hold 1 for even parity,0 for odd parity.5. Carry flag:5. Carry flag: hold carry after end of processing of operation. hold carry after end of processing of operation.

FLAGS Register & Flow control Instruction

Flags Register

Individual bits control the action or represent the status of the processor

Control flags (TF, IF, DF): Determine how the processor responds

to certain situations

Status flags (CF, PF, AF, ZF, SF, OF):

      Set to represent the result of certain operations

        Used to control conditional jump instructions

Flow Control Instructions:

In this assignment we will practice how to control the flow of an assembly language

program using the compare instruction, the different jump instructions and the loop instructions.

Objectives:

1- Jump Instructions.

2- Compare Instruction.

3- Loop Instructions.

Jump Instructions:

The jump instructions are used to transfer the flow of the program to the indicated Operator.

Here we discus about various categories.

Conditional Jumps:

There are Three categories of Conditional Jumps:

 a)Singed Jumps

b)Unsigned Jumps:

c) Single-Flag Jumps:

The Unconditional Jump:

The unconditional jump may be used to make infinite loops. Though the use of such instructions is not

recommended in high level languages, due to the availability of

program control structures, in assembly however, most of the time

one must use the unconditional jump.

The JMP Instructions

The JMP instruction causes an unconditional transfer of

control(unconditional jump).

The syntax is

IF-THEN:

• The IF-THEN structure may be expressed in pseudocode as follow

IF (condition is true) THEN

Execute true-branch statementsEND_IF

IF-THEN-ELSE

IF condition is true

THENExecute true-branch statements

ELSEExecute false-branch statements

END-IF

Loop Loops represent the final basic control structure (sequences, decisions, and loops) which make up a typical program. Program loops consist of three components: an optional initialization component, a loop termination test, and the body of the loop.

FOR LOOP

FOR LOOP_COUNT times DO

Statements

END_FOR

false

true

Initializecount

Statement

Count=count-1

count=o

EXAMPLE

Write a count-controlled loop to display A to Z 26 character

The code is MOV CX, 26

MOV AH, 2

MOV DL, ’A’

TOP:INT 21H

LOOP TOP

While Loops

WHILE condition

Do

Statements

END_WHILE

Example

I = 0;

WHILE (I<100)

I = I + 1;

mov I, 0

WhileLp: cmp I, 100

jge WhileDone inc I

jmp WhileLp

REPEAT LOOP

REAPET

Statements

UNTLIE condition

false

true

Statement

count=o

Example

MOV AH,1

LOOP1:

INT 21H

CMP al, '.'

JE EndLoop

JNE LOOP1

EndLoop:

top related