Assembly 05 Arithmetic Flags and Instructions Status Flags

Assembly 05 Arithmetic Flags and Instructions

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 bits Parity 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 an odd number of 1 bits

Zero Flag Set the ZF mov AL, 0 FH add AL, 0 F 1 H Set the ZF mov EAX, 1 dec EAX Example sub EAX, EAX mov EDX, M L 1: mov ECX, N L 2: inc EAX loop L 2 dec EDX jnz L 1 Exit: mov sum, EAX

Carry Flag • 100 H require 9 bit to store • Rang of values • The CF is set as the result is too small for unsigned number • Inc and Dec instructions does not affect the CF • Other Usage – To 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.

Overflow Flag • Range of values • Sets the OF mov AL, 72 H ; 72 H = 114 D add AL, 0 EH ; 0 EH = 14 D • and this mov AX, 0 FFFBH ; AX = -5 sub AX, 7 FFDH ; subtract 32, 765 from AX • The overflow flag is also affected by – shift, multiply, and divide operations. Usage • The main purpose of the overflow flag is to indicate whether an arithmetic operation on signed numbers has produced an out-of-range result.

The Sign Flag • Clear SF (SF = 0 ), 112 D is positive mov EAX, 15 add EAX, 97 • Set SF (SF = 1), (15 – 97) is negative mov EAX, 15 sub EAX, 97

Sign and Unsigned 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. Example mov AL, 72 H add AL, 0 EH • Unsigned Number (CF = 0) The result 80 H (128) fits 8 -bit unsigned • Signed Number (OF = 1) The results 80 H (128) is outside the 8 -bit signed • Thus 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.

Example

mul & imul instructions mov AL, 10 mov DL, 26 mul DL mov DL, 25 ; DL = 25 mov AL, 0 F 6 H ; AL = -10 imul DL

div & idiv instructions mov AX, 251 mov CL, 12 div CL xor DX, DX ; clear DX mov AX, 141 BH ; AX = 5147 D mov CX, 012 CH ; CX = 300 D div CX

Code Example ; displays a signed 8 -bit integer that is in AL register. ; -----------------------------Put. Int 8: enter 3, 0 ; reserves 3 bytes of buffer space push AX push BX push ESI test AL, 80 H ; negative number? jz positive negative: Put. Ch ’-’ ; sign for negative numbers neg AL ; convert to magnitude positive: mov BL, 10 ; divisor = 10 sub ESI, ESI ; ESI = 0 (ESI points to buffer) repeat 1: sub AH, AH ; AH = 0 (AX is the dividend) div BL

Code Example ; AX/BL leaves AL = quotient & AH = remainder add AH, ’ 0’ ; convert remainder to ASCII mov [EBP+ESI-3], AH ; copy into the buffer inc ESI cmp AL, 0 ; quotient = zero? jne repeat 1 ; if so, display the number display_digit: dec ESI mov AL, [EBP+ESI-3]; display digit pointed by ESI Put. Ch AL jnz display_digit ; if ESI<0, done displaying display_done: pop ESI ; restore registers pop BX pop AX leave ; clears local buffer space ret
- Slides: 12