Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman 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 8086 Flags Bit Positions and Names Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Name CF Carry PF Parity AF Auxiliary Carry ZF SF TF IF DF OF Zero Sign Trap Interrupt Enable Direction Overflow Debug Flag Mnemonics Debug Flag Mnemonics F=0 F=1 NV UP OV Overflow DI DN Direction PL EI Interrupt Enable NZ NG Sign (Negative) NA ZR Zero PE AC Auxiliary Carry NC PO Parity CY Carry 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. 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) 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) 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. 9 Program Control Instructions • Instructions that direct the flow of a program and allow the flow to change. • Unconditional Jump (jmp) • Conditional Jumps Jumps Based on Specific Flags Jumps Based on Equality Jumps Based on Unsigned Comparisons 13 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 Jumps Based on Signed Comparisons 15 Difference In Interpretation Of Signed And Unsigned Numbers .MODEL SMALL .STACK 100H .DATA MSG1 DB 13,10,"YES JUMP HAPPENS IN SING$" MSG2 DB 13,10,"JUMP HAPPENED FOR UNSIGNED$" .CODE START: MOV AX,@DATA MOV DS,AX MOV BH,10000000B ;CMP BH,11111111B CMP BH,01111111B JG SIGNM JA UNSIGN SIGNM: MOV AH,9 LEA DX,MSG1 INT 21H JMP TERM UNSIGN: MOV AH,9 LEA DX,MSG2 INT 21H TERM: MOV AX,4C00H INT 21H END START END Conditional Jump Instructions CMP Instruction (1 of 3) mov al,5 cmp al,5 ; Zero flag set • Example: destination < source mov al,4 cmp al,5 ; Carry flag set CMP Instruction (2 of 3) • Example: destination > source mov al,6 cmp al,5 ; ZF = 0, CF = 0 (both the Zero and Carry flags are clear) CMP Instruction (3 of 3) The comparisons shown here are performed with signed integers. • Example: destination > source mov al,5 cmp al,-2 ; Sign flag == Overflow flag • Example: destination < source mov al,-1 cmp al,5 ; Sign flag != Overflow flag 20 Example-1 • Example : Using the jz instruction. mov ax, 2 ; ax = 2 sub ax, bx ; ax = 2 - bx jz nextl ; jump if (ax-bx) == 0 inc ax ; ax = ax + 1 nextl: inc bx • The above is equivalent to: ax = 2; if ( ax != bx ) { ax = ax + 1 ; } bx = bx + 1 ; 21 Example-2 • C version if ( i == 10 ) { i=i+5; j=j+5; } /* Rest of program */ • Assembly version cmp i, 10 jne rest ; if i != 10 goto rest add i, 5 ; otherwise do action part add j, 5 rest: ; rest of program 22 Practice Program in class • Write a program that inputs a character and prints whether it is uppercase or lowercase English alphabet. 23 Assignment 4a • Write a program that inputs a character and prints it is a digit, or uppercase/lowercase English alphabet or some other character.