Download presentation

1
**Arithmetic Flags and Instructions**

Assembly 05 Arithmetic 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 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 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, EAX mov EDX,M L1: mov ECX, N L2: inc EAX loop L2 dec EDX jnz L1 Exit: 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 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.

5
**Overflow Flag The overflow flag is also affected by**

Range of values Sets the OF mov AL, 72H ; 72H = 114D add AL, 0EH ; 0EH = 14D and this mov AX, 0FFFBH ; AX = -5 sub AX, 7FFDH ; 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.

6
**The Sign Flag Clear SF (SF = 0 ), 112D is positive**

mov EAX,15 add EAX,97 Set SF (SF = 1), (15 – 97) is negative sub 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. Example mov AL,72H add AL,0EH Unsigned Number (CF = 0) The result 80H (128) fits 8-bit unsigned Signed Number (OF = 1) The results 80H (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.

8
Example

9
**mul & imul instructions**

mov AL,10 mov DL,26 mul DL mov DL, ; DL = 25 mov AL, 0F6H ; AL = -10 imul DL

10
**div & idiv instructions**

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

11
**Code Example ; displays a signed 8-bit integer that is in AL register.**

; PutInt8: enter 3,0 ; reserves 3 bytes of buffer space push AX push BX push ESI test AL,80H ; negative number? jz positive negative: PutCh ’-’ ; sign for negative numbers neg AL ; convert to magnitude positive: mov BL,10 ; divisor = 10 sub 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 ASCII mov [EBP+ESI-3], AH ; copy into the buffer inc ESI cmp AL,0 ; quotient = zero? jne repeat1 ; if so, display the number display_digit: dec ESI mov AL,[EBP+ESI-3]; display digit pointed by ESI PutCh 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

Similar presentations

OK

Logic Conditional Processing. Status flags - review The Zero flag is set when the result of an operation equals zero. The Carry flag is set when an instruction.

Logic Conditional Processing. Status flags - review The Zero flag is set when the result of an operation equals zero. The Carry flag is set when an instruction.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google