1 / 17

How the Flags Reflect Conditions

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

barto
Download Presentation

How the Flags Reflect Conditions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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.

More Related