Bit Manipulation & Editing Ch. 10 Page 240 Boolean Instructions Bits in the sending field are used to modify bits in the receiving field – one byte at a time Instruction SI SS RX RR OR OI OC O OR AND NI NC N NR EXCLUSIVE OR XI XC X XR OR Logic If one bit or both bits are “on”, then the resulting bit is “on” AND Logic Both bits must be “on” in order for the resulting bit to be “on” X-OR Logic If one bit or the other bit is “on”, but not both, then the resulting bit is “on”. OR Family O R1,D2(X2,B2) OI D1(B1),I2 OC D1(L,B1),D2(B2) OR R1,R2 The OR of the 1st and 2nd operands are placed at the first operand location Example: OI AREA+10,X’5C' Before: AREA+10 Mask After: AREA+10 0011 0101 0101 1100 0111 1101 Notice the result in the example to the left: Begin with the left-hand bit in the result field – the result bit if “0” because neither of the target fields (AREA+10 and MASK) is a “1”. In the case of the second bit from the left, the result is “1” because the bit in the same position in MASK is “1” even though it is a “0” in AREA+10. The third bit from the left is the same situation. Notice the fourth bit from the left. The result is “1” because both target bits are “1”. Simply apply the same rules to the second half of the byte – the result is “0” only if both of the target bits are “0”. This is the instruction you have been using to change the flag bit in the DCB to accept the data as EBCDIC rather than ASCII. You have also, probably, used this same instruction to make your sign-byte printable following a CVD and UNPK instruction for an output field. AND Family N R1,D2(X2,B2) NI D1(B1),I2 NC D1(L,B1),D2(B2) NR R1,R2 The AND of the 1st and 2nd operands are placed at the first operand location Example: NI AREA+10,X’5C' Before: AREA+10 Mask After: AREA+10 0011 0101 0101 1100 0001 0100 This time the target fields are being AND’ed – both operand fields must contain “1” bits in order for the result to be a “1” in the same bit position. Notice the only bits that are “1” in the result field are because that is the only part of the target fields that both bits are “1” – all others are “0”. This instruction is typically used to turn selected bits to “0”. In your mask field, set a bit to “1” for those bits you do not want to change, but set the bits to “0” if you wish to turn the target bit to “0”. X-OR Family X R1,D2(X2,B2) XI D1(B1),I2 XC D1(L,B1),D2(B2) XR R1,R2 The X-OR of the 1st and 2nd operands are placed at the first operand location Example: XI AREA+10,X’5C' Before: AREA+10 Mask After: AREA+10 0011 0101 0101 1100 0110 1001 This is probably the trickiest of the Boolean sets of instructions. Set the result to a “1” if either the target or the mask is a “1” bit – but not both are “1” bits (that would then be the same as AND). Map the result field back to the two input fields to see that in each case, only one of the two bits being compared cause the result to be “1”. Use this instruction when OR nor AND will do the right thing for you. Test Under Mask - TM • • • • Check the status of bits in a byte Storage-Immediate format only Sets Condition Code based on results of the test Indicate in the mask the bits in the byte to be tested by setting those bits to 1’s Practically every program uses a switch or some indicator to indicate some event has occurred. TM instruction provides a very efficient means to test that switch to see if your event has occurred yet. Do you remember the Parcheesi exercise back near the beginning of class? In it, you set a switch (using OI perhaps) to indicate that you landed on a Blue Square and are due to lose your next roll of the die. On entry to your Parcheesi Player, you could test that switch using TM. The smallest amount of memory that you can allocate is a byte, so let’s assume that you used the high-order byte for your switch. The example using Parcheesi for testing the LOSE TURN follows. Of course, the purpose of TM is to set the condition code so you can actually check on the status of the comparison. FLAGBYTE,X’C3’ TM CC set: If tested bits are: CC = 0 CC = 1 CC = 3 0's mixed 1's FLAGBYTE 1111 1011 Mask 1100 0011 Result: CC=3 All tested bits are 1’s Remember Parcheesi ? The flowchart that you created lies to the far left – at least the part of the flowchart that relates to our current discussion. The Assembler code that is relevant is under the flowchart symbol labeled RESET SWITCH. Early in the turn of PLAYER1, the test needs to be made to see if a Blue Square was landed on the last turn and if so, this turn needs to be bypassed. The TM instruction satisfies the test of the Blue Square (the switch is the high-order bit of the BLUESW byte. The define is done in the last displayed instruction at the bottom. The switch is set once it has been determined that the square landed on is blue. This can be easily accomplished with the OI instruction, as shown near the bottom. Note also that the switch is tested prior to rolling the die. Finally, where the switch is tested, then the branch to SWITCHON is taken, you want to remember to shut the switch off. This could be a good use of the Exclusive Or instruction: XI BLUESW,X’80’ Translation Instructions • Translate – TR • Translate and Test – TRT • Execute - EX Translate one bit pattern into a different bit pattern – up to 256 unique characters per byte TR and TRT essentially work the same way, but they end up a little differently. Read on! The EX instruction is not really a Translation instruction, but it does most often get used with the TR/TRT instructions. p.328 Translate - TR 2 Operands: 1. 2. TR pattern to be translated table of replacement characters EBCDIC,ASCIITAB We will create an example to change EBCDIC digits (F0-F9) into ASCII digits (30-39). EBCDIC operand is the digits to be translated & ASCIITAB is the replacement digits. 0 1 7 8 9 B C D E F 0_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 1_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 2_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 3_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 4_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 5_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 6_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 7_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 8_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 9_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 A_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 B_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 C_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 D_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 E_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 F_ 30 31 32 33 34 35 36 37 38 39 40 40 40 40 40 40 40 2 3 4 5 6 A Take notice of the table shown on the previous slide. Nearly all the blocks in the table contain the EBCDIC character for a blank – X’40’. These are the EBCDIC character patters to be ignored for translation. While the characters around the table – across the top and down the left side are not part of the table in memory – these characters are just showing us the 256 positions in memory which is occupied by the table. The only characters other than X’40’ are used in the bytes representing F0-F9 – the EBCDIC characters for the digits 0-9. That’s where the characters to be translated are placed in the table. The values that are replacing the X’40’ are those of the ASCII characters that represent the digits 0-9. The idea should now be quite obvious to you – when a byte that contains the EBCDIC character X’F0’ it will be translated into ASCII character X’30’, X’F1’ will be translated to ASCII X’31’ and so on. Any other characters will remain untouched (un-translated) which is what the X’40’ means to accomplish. The hardest part (not really hard, just tedious or cumbersome) is building and defining the table in memory. Recall that each character, as it appears in memory, is made up of two hex digits, up to 256 unique characters. In the table on the previous slide, the first hex character is represented down the left column and the second hex character is represented across the table (in a row). Translate - TR TR EBCDIC,ASCIITAB where EBCDIC contains: which in hex is: 1984 F1 F9 F8 F4 From left-to-right, each EBCDIC character is used as an index into ASCIITAB. The resulting character is the character at the indexedto location in memory. Translate - TR TR EBCDIC,ASCIITAB Any other bit configuration would give back a blank character – X’40’ Defining ASCIITAB ? ASCIITAB DS DC DC DC 0CL256 16X’40’ 16X’40’ 16X’40’ DC DC DC DC 16X’40’ *E0-EF X’303132333435’ X’36373839’ 12X’40’ *00-0F *10-1F *20-2F *F0-F5 *F6-F9 *FA-FF Translate-and-Test - TRT • Similar to TR in the way it works – 1st operand acts as an index into 2nd operand – 2nd operand is a table • No replacement of characters – just checks that table byte is X’00’ – If X’00’, process the next byte – If not X’00’, stop processing TRT and place address of the byte in GPR 1, place the byte from the table into low-order byte of GPR 2 – You had better not be using GPRs 1 & 2 for anything So What Are Uses of TRT? • When looking at a variable-length field such as a NAME, Find the blank following a name shorter than the fixed-character length in an input area • Find the first significant digit in a numeric field • Locate a comma separator • Find any character that has special meaning TRT Table • X’00’ – character with no special meaning • Non-X’00’ – special meaning – X’40’ represents characters without interest – X’00’ represents alpha or numeric characters – Other fields identify some special characters such as a blank (40) is identified with X’04’ and from there, the special characters are identified in increments of 4 bytes (04, 08, 0C, etc.) As you may have guessed by now, this is another of those instructions that takes significantly more time to think through, design, and build all the support areas, and the instruction itself is very easy to code. 0 1 2 3 0_ 40 40 40 40 1_ 40 40 2_ 40 3_ 4 5 6 7 8 9 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 4_ 04 40 40 40 40 5_ 14 40 6_ 24 7_ A B C D E F 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 08 40 0C 10 40 40 40 40 40 40 40 40 40 40 18 1C 20 40 40 28 40 40 40 40 40 40 40 40 40 2C 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 30 34 38 3C 40 8_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 9_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 A_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 B_ 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 C_ 40 00 00 00 00 00 00 00 00 00 40 40 40 40 40 40 D_ 40 00 00 00 00 00 00 00 00 00 40 40 40 40 40 40 E_ 40 40 00 00 00 00 00 00 00 00 40 40 40 40 40 40 F_ 00 00 00 00 00 00 00 00 00 00 40 40 40 40 40 40 Review closely the table on the previous slide: X’00’ in the table represent the alphanumeric characters in EBCDIC – letters and numbers X’40’ is used to indicate invalid characters – those that have no interest Some special characters are of interest and are identified with other than X’00’ or X’40’ – for example a blank (40) is identified with a X’04’ and the period (4B) is identified with X’08’. Notice that the special characters are identified with symbols that are in increments of 4 bytes – 04, 08, 0C, 10, etc. The table to the right outlines the characters that are “special” for the TRT instruction. • Assume the table is located beginning at location X‘2000’ – so the letter upper-case A is the translate byte at X’20C1’ • A branch table is located at X’3004’ with 16 branch instructions – one for each of the special characters- the first of which is for a blank • The characters to be translated are at location X’CA80’ which contains: (CA80-CA94) UNPK PROUT(9),WORD(5) E4D5D7D2 40D7D9D6 E4E34DF9 5D6BE6D6 D9C44DF5 5D • Translate-and-Test instruction is: LA TRT R2,BRTABLE UNPKINST(21),TRTTABLE • 1st character to translate is U – X’E4’ • Translation characters are X’00’ at Table+E4 – take no special action • Translate the rest of the op-code until translation reaches the blank (b) • A blank character is X’40’ which takes us to table+40 which is non-X’00’ – Addr of the source byte is placed in GPR 1 GPR 1 00 00 00 00 00 00 CA 84 – Table entry (X’04’) is placed in low order byte of GPR 2 (which contained X’3000’) GPR 2 00 00 00 00 00 00 30 04 – CC is set to 1 (non-zero byte found) BC 4,0(2) *blank found This is complicated! At table+40 (address in GPR1) is found X’04” so the X”04” is placed in the low-order byte of GPR2. Prior to this, GPR2 contained X”3000” – the address of the Branch Table which you constructed and can be seen on the next slide. With the value found by TRT (X”04”), the condition code is set to 1, so the BR instruction above branches on condition (cond. Code 4 = Cond. Code set to 1) to location 0000 in memory, indexed by GPR2 which contains the address of the Branch Table entry for “Found a Blank” which was X”04” in the Translate Table. Phew! Condition Code Set Mask Bit Value 0 8 1 4 2 2 3 1 TRT instruction set the Condition Code to 1 to indicate non-zero found when the “blank” was translated. With Cond.Code 1, the code to test in the Branch-on-Condition is X”4”. Branch Table beginning at Loc. X’3000’ Memory Location 3000 3004 3008 300C 3010 3014 3018 301C 3020 3024 3028 302C 3030 3034 3038 303C Contents of Branch Table BC 4,0(2) BLANKSP BC 4,0(2) PERIOD BC 4,0(2) LEFT PAREN BC 4,0(2) PLUS SIGN BC 4,0(2) AMPERSAND BC 4,0(2) DOLLAR SIGN BC 4,0(2) ASTERISK BC 4,0(2) RIGHT PAREN BC 4,0(2) DASH BC 4,0(2) FORW’D SLASH BC 4,0(2) COMMA BC 4,0(2) POUND SIGN BC 4,0(2) AT SIGN BC 4,0(2) APOSTROPHE BC 4,0(2) EQUAL SIGN I think you get the idea… Execute - EX • Causes just one target instruction to be executed out of sequence without actually branching to that instruction. • After OR’ing bits 8-15 (2nd byte) of the 2nd operand, which could be the length field of SS-type instructions without changing the byte in the instruction (OR’s from low-order byte of 1st operand) • Most often used with TRT • RX format: EX R1,D2(X2,B2) As we all learned possibly on the very first day of class, we don’t mix instructions and definitions in the same part of the program. Normally instructions come first, then the definitions. But there are circumstances where that rule may be disregarded and the use of the EX instruction is one of those times. As we also know, with the EX instruction, we can target some other instruction that is out of sequence, execute that target instruction, then return to the instruction following the EX instruction. In this case, it makes sense to place that target instruction out of the way of normal sequential instruction execution. Place the target instruction along with the definitions of memory. The ability of the EX instruction to modify the second byte of a target instruction gives you the power, depending on the instruction format, to change a register reference, or an immediate operand, or an operand length. Review each instruction format for what is located at the second byte location, therefore subject to change: FORM AT OPERANDS RR R1,R2 RS R1,R3,D2(B2) RX R1,D2(X2,B2) SI D1(B 1),I2 SS(1) D1(L,B1),D2(B2) SS(2) D1(L1,B1),D2(L2,B2) In the chart to the left, the underlined operand in each instruction format shows the possibilities for the values that can be altered by using the EX instruction. Operand 1 designates a register containing a value used to modify the second byte of the target instruction. However, if register 0 is specified, no modification is done. Operand 2 refers to the target instruction, defined elsewhere in memory, to be executed by EX. Any instruction except another EX may be target to EX. EX then Ors bits 8-15 (byte 2) of the target instruction with bits 24-31 (the rightmost byte) of the operand 1 register. The target instructions, however, is not actually changed in memory. After executing the target instruction, the program resumes either with the instruction following EX or, if the target instruction is a branch, where the branch is directed. Examples of Using EX The following RR-format example inserts X”14” into GPR5 and Ors byte 2 of ADDREG with the X”14”. This process causes the second byte of ADDREG to reference GPR 1 and 4 temporarily. ADDREG executes out of line as “add” the contents of GPR4 to the contents of GPR1. EXRR ADDREG IC EX --DS AR 5,=X’14’ 5,ADDREG insert X’14’ into R5 Add R4 to R1 0H 0,0 target instruction The RS-format example Ors the second byte of STOREGS with X’DF’. This process causes byte 2 of STOREGS to reference R13 and R15. STOREGS executes out of line as “Store the contents of registers 13,14, and 15 in SAVEREGS. SAVEREGS STOREGS IC EX --DS DS STM 6,X’DF’ 6,STOREGS insert X’DF’ into R6 store regs 13-15 3F 0H 0,0,SAVEREGS 3 fullwords boundary alignment to even RS target instruction Examples of Using EX This RX-format example Ors the second byte of STORCH with X’20’. This process causes byte 2 of STORCH to reference base reg 2 and index reg 0. STORCH executes as “store the rightmost 8 bits of R2 in SAVEBYTE. SAVEBYTE STORCH IC EX --DS DS STC 7,=X’20’ 7,STORCH put X’20’ into R7 store right byte of R7 C 0H 0,SAVEBYTE one byte – character force even address RX Target instruction This SI-format example Ors the second byte of MOVIMM with X’5B’ (a dollar sign), then moves the dollar sign to PRINT+20, and the instruction can thus move any immediate value. PRINT MOVIMM IC EX --DC DS MVI 8,=X’5B’ 8,MOVIMM place X’5B’ into R8 move $ to print area CL133’ ‘ 0H PRINT+20,X’00’ print area blanked out force boundary alignment target instrution Examples of Using EX Using EX on SS-format instructions causes it to modify operand lengths. Remember that when executing an SS-format instruction, the computer adds 1 to the length. The first example uses MOVECHAR to move a specified number of asterisks (up to 3) to the print area and could be used to print one, two, or three asterisks to denote the total level: PRINT MOVECHAR IC EX --DC DS MVC 9,=X’02’ 9, MOVECHAR insert X’02’ into R9 move *** to print area CL133’ ‘ print area definition 0H PRINT+30(0),=C’CCC’ target instruction for EX This example executes an AP operation is which byte 2 contains a length code for both operands: FLD1 FLD2 ADDPK IC EX --DS DS DS AP 10,=X’32’ 10,ADDPK insert X’32’ into R10 Add FLD2 to FLD1 PL4 PL3 0H FLD1(0),FLD2(0) target of EX instruction Last EX Example 003802 003804 003808 00380C 00380E 003810 003814 003818 00381A 00381E 003820 003824 1711 4190 4310 1881 0610 4190 4410 1A98 4310 0610 4190 4410 003828 003852 0038D7 00 0038D8 0038DE 4026 9000 9001 40D6 9000 9001 40DC 6 7 8 9 11 12 13 14 16 17 18 19 20 21 22 23 24 * * * NAMADDR PRLINE 26 M30MOVE 27 M40MOVE XR LA IC LR BCTR LA EX AR IC BCTR LA EX ------DS DS 1,1 9,NAMADDR 1,0(0,9) 8,1 1,0 9,1(0,9) 1,M30MOVE 9,8 1,0(0,9) 1,0 9,1(0,9) 1,M40MOVE Clear Reg 1 Addr 1st length indicator length of NAME in Reg 1 save length in Reg 8 decrement Reg 1 by 1 increment for NAME field execute M30MOVE addr of NAME + length for 2nd indicator length of addr in Reg 1 decrement reg 1 by 1 increment for address field execute M40MOVE CL42 CL133 variable name & address print area MVC MVC --- PRLINE+20(0),0(9) PRLINE+50(0),0(9) move NAME to Print move ADDRESS to Print If you follow this example carefully, there is a length byte in front of each variable-length field in the input that is read from a device. The length is one byte and in binary format. There are two fields alternating: NAME and ADDRESS. 0A ADAM SMITH 0C 423 BASIN ST nn NEXT NAME nn NEXT ADDR Last EX Example Continued Since the name is 10 characters long, the preceding length indicator contains X’0A’. The address is 12 characters long, and its preceding length indicator contains X’0C’. The entire record consists of 1 + 10 + 1 + 12 = 24 bytes. Other records would vary in length accordingly. The maximum record length is assumed to be 42 bytes. The purpose is to move the name and the address separately to the print area. IC extracts the length indicator, and the STC inserts the length indicator (minus 1 byte) into the second byte, the length, of the MVC instruction. At execution time, the computer adds 1 to the instruction length. At the end of execution, the print area looks like this: Print area: ADAM SMITH 423 BASIN ST PRINT + 20 PRINT + 40 Editing • Used to Edit (Beautify) numeric data to packed decimal (and it UNPK’s it too) • Insert $-sign • Include commas in larger numbers • Insert the decimal point in financial numbers • Suppress leading zeros p.82 in Ch.4 Some Edit Patterns Char Meaning 20 Digit selector 21 40 4B 60 6B Significance starter Blank character Decimal point Minus sign comma Detailed Edit Example 1200 1203 02 57 42 6C Source: 1000 Pattern: 100C 40 20 20 6B 20 21 20 4B 20 20 40 C3 D9 b d d , ED d ( d . d d b C R PATTERN(13),SOURCE(4) The SOURCE field is the value that you want to have beautified by inserting commas, if appropriate, dollar sign, and decimal point. The PATTERN field tells EDIT how to go about accomplishing the beautification. The PATTERN is shown in two places – the hex format in the light green box and the character interpretation in the white box below the green box. The instruction executes like most others (right to left) and the PATTERN must be the receiving field. SOURCE is unchanged after the instruction executes. Before decimal data in the packed format can be used in a printed report, digits and signs must be converted to printable characters. Moreover, punctuation marks, such as commas and decimal points, may have to be inserted in appropriate places. The highly flexible EDIT instruction performs these functions in a single instruction execution. The example on the previous slide shows step-by-step one way that the EDIT instruction can be used. The field to be edited (the source) is four bytes long; it is edited against a pattern 13 bytes long. The following symbols are used: SYMBOL b (hex 40) ( (hex 21) d (hex 20) MEANING blank character significance started digit selector As the instruction executes, it looks at each byte from left to right. The table below shows how each byte is treated by instruction execution. PATTERN b d d , d ( d . d d b C R DIGIT 0 2 5 7 4 2 6+ SIGNIFICANCE INDICATOR (before/after) off/off off/off off/on(2) on/on on/on on/on on/on on/on on/on on/off(3) off/off off/off off/off RULE LOCATION 1000-100C leave (1) bdd,d(d.ddbCR fill bbd,d(d.ddbCR digit bb2,d(d.ddbCR leave same digit bb2,5(d.ddbCR digit bb2,57d.ddbCR digit bb2,574.ddbCR leave same digit bb2,574.2dbCR digit bb2,574.26bCR fill same fill bb2,574.26bbR fill bb2,574.26bbb In the table above there are 3 notes: 1. This character is the fill byte. 2. First nonzero decimal source digit turns on the significance indicator. 3. Plus sign in the four rightmost bits of the byte turns off the significance indicator. And the Answer Is … Pattern: 40 40 F2 6B F5 F7 F4 4B F2 F6 40 40 40 which prints as: 2,574.26 After ED executes, the condition code is set. 0 result is 0 1 result is negative 2 result is positive 3 not used More Examples of ED on p. 83 in Your Textbook • • • • Suppress Leading Zeroes Significance Starting Insert commas and decimal points Negative values handling EDMK • Used for printing money values • Same as Edit instruction except that the address of the 1st significant digit is saved in GRP 1 when EDMK executes • This makes it easier to insert a dollar-sign or other currency symbol into the field to the left of the amount p.238 EDMK Example Source: 02 57 42 6C Pattern: 40 20 20 6B 20 21 20 4B 20 20 40 C3 D9 EDMK BCTR MVI PATTERN(13),SOURCE(4) 1,0 0(1),C’$’ Pattern: 40 5B F2 6B F5 F7 F4 4B F2 F6 40 40 40 Printed Output: $2,574.26 EDMK Examples on p.239 • • • • • • • Suppress leading zeroes Significance Starting Insert commas and decimal point Non-blank fill character Negative value handling Editing dates Suppressing fields Exercise • Due next week • Optional / Extra Credit • Bit Bash Assignment