assembly 05 arithmetic flags and instructions. status flags zero flag (zf) indicate that the...

12
Assembly 05 Arithmetic Flags and Instructions

Upload: oswin-bradley

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

Assembly 05

Arithmetic Flags and Instructions

Page 2: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

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

Page 3: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

Zero FlagSet the ZF

mov AL,0FHadd AL,0F1H

Set the ZFmov EAX,1dec EAX

Example sub EAX, EAXmov EDX,M

L1:mov ECX, N

L2:inc EAXloop L2dec EDXjnz L1

Exit:mov sum, EAX

Page 4: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

Carry Flag• 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.

Page 5: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

Overflow Flag• Range of values

• Sets the OFmov AL, 72H ; 72H = 114D

add AL, 0EH ; 0EH = 14D

• and thismov 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.

Page 6: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

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

mov EAX,15

add EAX,97

• Set SF (SF = 1), (15 – 97) is negative

mov EAX,15

sub EAX,97

Page 7: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

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.

Examplemov AL,72Hadd 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.

Page 8: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

Example

Page 9: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

mul & imul instructions

mov AL,10mov DL,26mul DL

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

Page 10: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

div & idiv instructionsmov AX,251mov CL,12div CL

xor DX, DX ; clear DXmov AX, 141BH ; AX = 5147Dmov CX, 012CH ; CX = 300Ddiv CX

Page 11: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

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 positive

negative:PutCh ’-’ ; sign for negative numbersneg AL ; convert to magnitude

positive: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

Page 12: Assembly 05 Arithmetic Flags and Instructions. Status Flags Zero flag (ZF) Indicate that the execution of the last instruction that affects the zero flag

Code Example; AX/BL leaves AL = quotient & AH = remainderadd 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 number

display_digit:dec ESImov AL,[EBP+ESI-3]; display digit pointed by ESIPutCh ALjnz display_digit ; if ESI<0, done displaying

display_done:pop ESI ; restore registerspop BXpop AXleave ; clears local buffer spaceret