CIS 020 Assembly Programming Chapter 06 Comparisons & Logical control Procedures 5/27/2012 © John Urrutia 2012, All Rights Reserved. 1 Logical Control Procedures Program logic All programs will execute in sequential order unless told to do otherwise. The set of branch instructions will alter the order of program execution and work in conjunction with comparison instructions 5/27/2012 © John Urrutia 2012, All Rights Reserved. 2 Logical Control Procedures Unconditional Branches Always alters the program execution path and are used to create repetition or iterative loops. Only one operand – must be a label. 5/27/2012 © John Urrutia 2012, All Rights Reserved. 3 Logical Control Procedures Conditional Branches May alter the program execution path and are used to create both selection structures and repetition loops 2 operands 5/27/2012 Test condition mask (0 – 15) Label to branch to if condition matches © John Urrutia 2012, All Rights Reserved. 4 Logical Control Procedures Selection structures One-way selection if true then branch to label Two-way selection if true then branch to label else continue in sequence if false then branch to label else continue in sequence 5/27/2012 © John Urrutia 2012, All Rights Reserved. 5 Logical Control Procedures One-Way Selection Flowchart Two-Way Selection Flowchart True No 5/27/2012 True Yes -branch Yes -branch No Instructions to execute Instructions to execute © John Urrutia 2012, All Rights Reserved. Instructions to execute 6 Logical Control Procedures Looping structures pseudocode Pre-Test Loop Do until true continue in sequence end Do Post-Test Loop Do continue in sequence Until true 5/27/2012 © John Urrutia 2012, All Rights Reserved. 7 Logical Control Procedures No Instructions to execute Post-test iteration Flowchart Instructions to execute True Yes branch True Yes -branch branch Pre-test iteration Flowchart No 5/27/2012 © John Urrutia 2012, All Rights Reserved. 8 Logical Control Procedures True The condition or test is the logical result of a comparison between two values. 2 instructions work with each other The comparison instruction sets the condition code The branch instruction determines the next instruction to execute based on the condition code 5/27/2012 © John Urrutia 2012, All Rights Reserved. 9 Comparison Statements True Used to determine the relationship between two data fields. Only three possible results Field 1 is greater than Field 2 Field 2 is greater than Field 1 Field 1 is equal to Field 2 5/27/2012 © John Urrutia 2012, All Rights Reserved. 10 Comparison Statements The result of a comparison is stored in a special area called the Condition Code which is part of the arithmetic logic unit of the CPU. For now we will concentrate only on how character data effects the Condition Code . The Condition Code is stored in the PSW. 5/27/2012 © John Urrutia 2012, All Rights Reserved. 11 Program Execution & the PSW You previously learned about the parts of the CPU The ALU The CU The Instruction Pointer – which always contains the address of the next instruction to execute. All information about the execution of your program is stored in the Program Status Word or PSW 5/27/2012 © John Urrutia 2012, All Rights Reserved. 12 Program Execution & the PSW The PSW is 8 bytes long and consists of: Bits 00 thru 31 (4 bytes) of system control information CC (bits 18,19) – Condition Code Bits 32 thru 63 (4 bytes) of program information Next instruction address (bits 40 thru 63) Many instructions will cause the CC to be updated which we will cover later All branching instructions interrogate the Condition Code and take action depending on the value. 5/27/2012 © John Urrutia 2012, All Rights Reserved. 13 Compare Logical Characters The CLC instruction Like the MVC instruction has two operands The 1st is compared byte by byte, left to right, against the 2nd operand You can compare up to 256 bytes per instruction As soon as one of the bytes are unequal or the length of the 1st operand is reached the condition code is set and the instruction terminates. Just like MVC the length of the compare is governed by the length of Operand 1 5/27/2012 © John Urrutia 2012, All Rights Reserved. 14 Compare Logical Characters The CLC instruction When dealing with printable characters the collating sequence determines which character is larger. 5/27/2012 ASCII – from low to high (abbreviated) Special characters, numerals, upper then lower case letters EBCDIC – from low to high (abbreviated) Special characters, lower then upper case letters, numerals © John Urrutia 2012, All Rights Reserved. 15 Compare Logical Characters CLC instruction termination When one of the bytes are unequal If value of Operand 1 is less than Operand 2 Set condition code to 1st Op Low (1) If value of Operand 2 is less than Operand 1 Set condition code to 1st Op High (2) End of 1st operand is reached Set condition code Op equal (0) The Condition Code does not change until another instruction is executed that updates the condition code 5/27/2012 © John Urrutia 2012, All Rights Reserved. 16 Compare Logical Characters Syntax of Instruction Op Code – CLC Operand 1 – label or address Operand 2 – label or address 5/27/2012 © John Urrutia 2012, All Rights Reserved. 17 Compare Logical Characters Which field is greater? 5/27/2012 © John Urrutia 2012, All Rights Reserved. 18 Compare Logical Characters Just like MVC the CLC operand(s) displacement and length attributes can be modified. 5/27/2012 © John Urrutia 2012, All Rights Reserved. 19 Compare Logical Immediate Just like MVI the CLI instruction compares only one byte which is imbedded as part of the instruction. CLI FIELD1,C’$’ Operand2 can be Any of the data type designators Cannot be a reference 5/27/2012 © John Urrutia 2012, All Rights Reserved. 20 Branch on Condition Statement Syntax of Instruction Op Code – BC Operand 1 – Mask value used to interrogate condition Operand 2 – branch to label or address 5/27/2012 © John Urrutia 2012, All Rights Reserved. 21 Branch on Condition Statement Syntax of Instruction Operand 1 – Mask values 01 – condition code = 3 Not set for Characters 02 - condition code = 2 Op1 > Op2 04 - condition code = 1 Op1 < Op2 08 - condition code = 0 Op1 = Op2 5/27/2012 © John Urrutia 2012, All Rights Reserved. 22 Branch on Condition Statement Syntax of Instruction Operand 1 – Mask values 01 – condition code = 3 Not set for Characters 13 – not condition code = 2 Op1 <= Op2 11 – not condition code = 1 Op1 >= Op2 07 – not condition code = 0 Op1 <> Op2 5/27/2012 © John Urrutia 2012, All Rights Reserved. 23 Branch on Condition Statement Operand 1 – Mask values difficult to remember and have mnemonic equivalents 02 - CC = 2 Op1 > Op2 BH Branch High 04 - CC = 1 Op1 < Op2 BL Branch Low 08 – CC = 0 Op1 = Op2 BE Branch Equal 13 – not CC = 2 Op1 <= Op2 BNH Branch not High 11 – not CC = 1 Op1 >= Op2 BNL Branch not Low 07 – not CC = 0 Op1 <> Op2 BNE Branch not Equal 5/27/2012 © John Urrutia 2012, All Rights Reserved. 24 Branch vs. Branch on Condition B and BC are equivalent when the Mask value is set to 15 BC becomes a null operator or NOP which will execute the next instruction in all cases when the Mask value is set to 0 In assembly we can modify the Mask value during execution of the program thereby changing a NOP instruction into a conditional branch. 5/27/2012 © John Urrutia 2012, All Rights Reserved. 25