CEN 226 : Computer Organization & Assembly Language : CSC 225 (Lec#5)

24
CEN 226 : Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman

description

CEN 226 : Computer Organization & Assembly Language : CSC 225 (Lec#5). By Dr. Syed Noman. Flag Register. Flag is a bit of special information usually implemented with flip flop Total 9 Flags 6 Status Flags: C, A, S, Z, P, O 3 Control flags: I, T, D. - PowerPoint PPT Presentation

Transcript of CEN 226 : Computer Organization & Assembly Language : CSC 225 (Lec#5)

Page 1: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

CEN 226: Computer Organization & Assembly Language :CSC 225(Lec#5)

ByDr. Syed Noman

Page 2: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

2

Flag Register

•Flag is a bit of special information usually implemented with flip flop

• Total 9 Flags ▫6 Status Flags: C, A, S, Z, P, O ▫3 Control flags: I, T, D

Page 3: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

8086 Flags - Bit Positions and Names

Bit Name0 CF Carry12 PF Parity34 AF Auxiliary Carry5 6 ZF Zero7 SF Sign8 TF Trap9 IF Interrupt Enable

10 DF Direction11 OF Overflow12 131415

Page 4: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Debug Flag Mnemonics F=0

F=1

NV UP DI PL NZ NA PE NCOV DN EI NG ZR AC PO CY

Overflow Direction InterruptEnable

CarryCarry(Negative)

Sign Zero Auxiliary Parity

Debug Flag Mnemonics      

Page 5: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Status Flags

• Sign(SF) – set when the most significant bit is a one.

• Zero(ZF) – set when the result of an arithmetic or logical operation is zero.

• Carry (CF) – set when the result of an unsigned arithmetic operation produces a carryout.

• Overflow(OF) – set when the result of a signed arithmetic operation is in error.

• Auxilary (AF) – set when carry is generated from bit 3 to 4 in addition or borrow is taken during subtraction from bit 4 to 3.

• Parity (PF) – set when number of 1’s are odd in the answer.

Page 6: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Problem•What are the flag settings of the result of

the following 8-bit HEX addition?▫D7h + CAh▫D7h + CAh = A1h ▫Zero (0) ▫Negative(1)▫Carryout (1) ▫Overflow (0)▫Auxiliary Carry (1)▫Parity (1)

Page 7: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Problem•What are the flag settings of the result of

the following 8-bit HEX addition?▫38h + C8h▫38h + C8h = 00h▫Zero (1) ▫Negative(0)▫Carryout (1) ▫Overflow (0)▫Auxiliary Carry (1)▫Parity (0)

Page 8: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

8

Overflow flag

•A negative result out of positive operands (or vice versa) is an overflow

•if we add 127 and 127 using 8-bit registers. 127+127 is 254, but using 8-bit arithmetics the result would be 1111 1110 binary, which is -2 in two's complement, and thus negative.

Page 9: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

9

Program Control Instructions

•Instructions that direct the flow of a program and allow the flow to change.

•Unconditional Jump (jmp)•Conditional Jumps

Page 10: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Jumps Based on Specific Flags

Page 11: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Jumps Based on Equality

Page 12: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Jumps Based on Unsigned Comparisons

Page 13: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Jumps Based on Signed Comparisons

Page 14: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

Conditional Jump Instructions

Page 15: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

15

The Compare Command

•Compares the destination operand to the source operand▫Nondestructive subtraction of source

from destination (destination operand is not changed)

•Syntax: CMP destination, source•Example: destination == source

Page 16: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

CMP Instruction (1 of 3)

mov al,5cmp al,5 ; Zero flag set

• Example: destination < source

mov al,4cmp al,5 ; Carry flag set

Page 17: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

CMP Instruction (2 of 3)

• Example: destination > source

mov al,6cmp al,5 ; ZF = 0, CF = 0

(both the Zero and Carry flags are clear)

Page 18: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

CMP Instruction (3 of 3)

• Example: destination > source

mov al,5cmp al,-2 ; Sign flag == Overflow flag

The comparisons shown here are performed with signed integers.

• Example: destination < source

mov al,-1cmp al,5 ; Sign flag != Overflow flag

Page 19: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

19

Difference In Interpretation Of Signed And Unsigned Numbers

.MODEL SMALL

.STACK 100H

.DATAMSG1 DB 13,10,"YES JUMP HAPPENS

IN SING$"MSG2 DB 13,10,"JUMP HAPPENED

FOR UNSIGNED$".CODESTART:MOV AX,@DATAMOV DS,AX

MOV BH,10000000B;CMP BH,11111111BCMP BH,01111111BJG SIGNMJA UNSIGN

SIGNM:MOV AH,9LEA DX,MSG1INT 21HJMP TERM

UNSIGN:MOV AH,9LEA DX,MSG2INT 21H

TERM:MOV AX,4C00HINT 21HEND STARTEND

Page 20: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

20

Example-1• Example : Using the jz instruction.

mov ax, 2 ; ax = 2sub ax, bx ; ax = 2 - bxjz nextl ; jump if (ax-bx) == 0inc ax ; ax = ax + 1nextl:inc bx

• The above is equivalent to:ax = 2;if ( ax != bx ){ax = ax + 1 ;}bx = bx + 1 ;

Page 21: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

21

Example-2

•C versionif ( i == 10 ){i = i + 5 ;j = j + 5 ;}/* Rest of program */• Assembly versioncmp i, 10jne rest ; if i != 10 goto restadd i, 5 ; otherwise do action partadd j, 5rest: ; rest of program

Page 22: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

22

Practice Program in class

•Write a program that inputs a character and prints whether it is uppercase or lowercase English alphabet.

Page 23: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

23

Practice Program solution.model small

.stack 100h

.datamsg0 db 13,10, "Enter a character:: $"msg1 db 13,10,"it is an uppercase letter.$"msg2 db 13,10,"it is a lowercase letter.$"msg3 db 13,10,"it is NOT a letter!$"

.codestart:mov ax, @datamov ds, ax

mov ah,9lea dx,msg0int 21h

mov ah,1int 21hmov bh, al

cmp bh,'A'jb not_a_lettercmp bh,'Z'jbe uppercase

cmp bh,'a'

jb not_a_letter; means between 91 - 96 included

cmp bh,'z'

jbe lowercase

not_a_letter:

mov ah,9

lea dx,msg3

int 21h

jmp finish

uppercase:

mov ah,9

lea dx, msg1

int 21h

jmp finish

lowercase:

mov ah,9

lea dx, msg2

int 21h

finish:

mov ax,4c00h

int 21h

end start

Page 24: CEN 226 : Computer Organization  &  Assembly Language : CSC 225 (Lec#5)

24

Assignment 2a

•Write a program that inputs a character and prints it is a digit, or uppercase/lowercase English alphabet or some other character.