170 likes | 336 Views
Flags are set by data manipulation instructions Example : “CMP dest , src” computes dest – src Sets ZF, SF, CF and OF to reflect the results: ZF = 1 if result = 0 SF = 1 if result is negative CF = 1 if operation required borrow into MSB
E N D
Flags are set by data manipulation instructions Example: “CMP dest , src” computes dest – src Sets ZF, SF, CF and OF to reflect the results: ZF = 1 if result = 0 SF = 1 if result is negative CF = 1 if operation required borrow into MSB OF = 1 if operation resulted in signed overflow How the Flags Reflect Conditions
Case (1): dest equals src e.g: BH = 01H and CL = 01H 01H – 01H = 0H, and no borrow is required Flag values: ZF = 1 since the answer is 0 CF = 0 since operation did not require a borrow CMP BH, CL
Case (2): dest is “below” src , e.g: BH = 00H and CL = 01H 00H – 01H = 0FFH, but 1 must be “borrowed” Flag values: ZF = 0 since answer is not 0 CF = 1 since operation required a borrow CMP BH, CL
Case (3): dest is “above” src e.g.: BH = 01H and CL = 00H 01H – 00H = 01H, and no borrow is required Flag values: ZF = 0 since answer is not 0 CF = 0 since borrow not required CMP BH, CL
Condition: dest ? Src ZF CF Equal (case 1) 1 0 Below (case 2) 0 1 Above (case 3) 0 0 JE (jump if equal) needs to check for ZF = 1 JB (jump if below) needs to check for CF = 1 JA (jump if above) must check for both ZF = 0 and CF = 0 UNSIGNED JMP’S
Now consider OF (overflow flag) and SF (sign flag) SF simply indicates msb of result (8 or 16 bit) OF set if result is “incorrect” or overflows 2's complement notation More complex analysis SIGNED NUMBERS
Case (1): dest equals src e.g: BH = 01H and CL = 01H 01H – 01H = 0H, and no borrow is required Flag values: SF = 0 since the sign bit is 0 OF = 0 since answer is “correct” CMP BH, CL
Case (2a): dest is “less than” src both non-negative , e.g: BH = 00H and CL = 01H 00H – 01H = 0FFH, and is “correct” Flag values: SF = 1 since answer is negative OF = 0 since answer is correct CMP BH, CL
Case (2bi): dest is “less than” src dest negative, src positive , e.g: BH = 0F1H and CL = 01H 0F1H – 01H = 0F0H, and is “correct” Flag values: SF = 1 since answer is negative OF = 0 since answer is correct BUT! results depend on values used! CMP BH, CL
Case (2bii): dest is “less than” src dest negative, src positive , e.g: BH = 80H and CL = 01H 80H – 01H = 7FH, but is not “correct” Flag values: SF = 0 since answer is not negative OF = 1 since answer has overflown 2's comp CMP BH, CL
CMP BH, CL • Case (2c): dest is “less than” src • both are negative , e.g: • BH = 80H and CL = 0FFH • 80H – 0FFH = 81H, and is “correct” • Flag values: • SF = 1 since answer is negative • OF = 0 since answer is correct
CMP BH, CL • Case (3a): dest is “greater than” src • both are non-negative , e.g: • BH = 02H and CL = 01H • 02H – 01H = 01H, and is “correct” • Flag values: • SF = 0 since answer is positive • OF = 0 since answer is correct
CMP BH, CL • Case (3bi): dest is “greater than” src • dest positive, src negative , e.g: • BH = 01H and CL = 0F0H • 01H – 0F0H = 11H, and is “correct” • Flag values: • SF = 0 since answer is positive • OF = 0 since answer is correct
CMP BH, CL • Case (3bii): dest is “greater than” src • dest positive, src negative , e.g: • BH = 7FH and CL = 0FFH • 7FH – 0FFH = 80H, but is not “correct” • Flag values: • SF = 1 since answer is negative • OF = 1 since answer has overflown 2's comp
CMP BH, CL • Case (3c): dest is “greater than” src • both are negative , e.g: • BH = 0FFH and CL = 0F0H • 0FFH – 0F0H = 0FH, and is “correct” • Flag values: • SF = 0 since answer is positive • OF = 0 since answer is correct
CMP BH, CL • Condition: dest ? Src ZF SF OF • Equal (case 1) 1 0 0 • Less Than (case 2a) 0 1 0 • Less Than (case 2bi) 0 1 0 • Less Than (case 2bii) 0 0 1 • Less Than (case 2c) 0 1 0 • Greater Than (case 3a) 0 0 0 • Greater Than (case 3bi) 0 0 0 • Greater Than (case 3bii) 0 1 1 • Greater Than (case 3c) 0 0 0
SIGNED JMP’S • Note that: • JE (jump equal) needs to check ZF = 1 • JL (jump less than) needs to check SF != OF • JG (jump greater than) must check • SF = OF and • ZF = 0 to distinguish the “equal” case.