Presentation on theme: "Arithmetic Flags and Instructions"— Presentation transcript:
1 Arithmetic Flags and Instructions Assembly 05Arithmetic Flags and Instructions
2 Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag has a zero result, ZF = 1 for zero result and ZF = 0 otherwise.Carry flag (CF)Indicates that the result of an arithmetic operation on unsigned numbers is out of range of the destination (register or memory).Overflow flag (OF)Indicates whether an operation on signed numbers has produced a result that is out of range.Sign flag (SF)Indicates the sign of the result of an operation, 0 for positive numbers and 1 for negative numbers.Auxiliary flag (AF)Indicates whether an operation has produced a result that has generated a carry out of or a borrow into the low-order four bitsParity flag (PF).Indicates the parity of the 8-bit result produced by an operation; The parity flag is set if the byte contains an even number of 1 bits and cleared if there are an odd number of 1bits
3 Zero Flag Example mov AL,0FH add AL,0F1H mov EAX,1 dec EAX Set the ZF sub EAX, EAXmov EDX,ML1:mov ECX, NL2:inc EAXloop L2dec EDXjnz L1Exit:mov sum, EAX
4 Carry Flag Other Usage 100H require 9 bit to store Rang of values The CF is set as the result is too small for unsigned numberInc and Dec instructions does not affect the CFOther UsageTo propagate carry or borrow in multiword addition or subtraction operations.To detect overflow/underflow conditions.To test a bit using the shift/rotate family of instructions.
5 Overflow Flag The overflow flag is also affected by Range of valuesSets the OFmov AL, 72H ; 72H = 114Dadd AL, 0EH ; 0EH = 14Dand thismov AX, 0FFFBH ; AX = -5sub AX, 7FFDH ; subtract 32,765 from AXThe overflow flag is also affected byshift, multiply, and divide operations.UsageThe main purpose of the overflow flag is to indicate whether an arithmetic operation on signed numbers has produced an out-of-range result.
6 The Sign Flag Clear SF (SF = 0 ), 112D is positive mov EAX,15add EAX,97Set SF (SF = 1), (15 – 97) is negativesub EAX,97
7 Sign and Unsigned Unsigned Number (CF = 0) Signed Number (OF = 1) Q: How does the system know how your program is interpreting a given bit pattern?A: The answer is that the processor does not have a clue.It is up to our program logic to interpret a given bit pattern correctly.The processor assumes both interpretations and sets the carry and overflow flags.Examplemov AL,72Hadd AL,0EHUnsigned Number (CF = 0)The result 80H (128) fits 8-bit unsignedSigned Number (OF = 1)The results 80H (128) is outside the 8-bit signedThus both flags are set and it is up to our program logic to consider the right flag.If unsigned numbers are represented, disregard the OF as use the CF.
11 Code Example ; displays a signed 8-bit integer that is in AL register. ;PutInt8:enter 3,0 ; reserves 3 bytes of buffer spacepush AXpush BXpush ESItest AL,80H ; negative number?jz positivenegative:PutCh ’-’ ; sign for negative numbersneg AL ; convert to magnitudepositive:mov BL,10 ; divisor = 10sub ESI,ESI ; ESI = 0 (ESI points to buffer)repeat1:sub AH,AH ; AH = 0 (AX is the dividend)div BL
12 Code Example ; AX/BL leaves AL = quotient & AH = remainder add AH,’0’ ; convert remainder to ASCIImov [EBP+ESI-3], AH ; copy into the bufferinc ESIcmp AL,0 ; quotient = zero?jne repeat1 ; if so, display the numberdisplay_digit:dec ESImov AL,[EBP+ESI-3]; display digit pointed by ESIPutCh ALjnz display_digit ; if ESI<0, done displayingdisplay_done:pop ESI ; restore registerspop BXpop AXleave ; clears local buffer spaceret