Download presentation
Presentation is loading. Please wait.
Published byMoris Todd Modified over 9 years ago
1
Bit Manipulation & Editing Ch. 10 Page 240
2
Boolean Instructions Bits in the sending field are used to modify bits in the receiving field - – one byte at a time InstructionSISSRXRR OROIOCOOR ANDNINCNNR EXCLUSIVE ORXIXCXXR
3
If one bit or both bits are “on”, then the resulting bit is “on” OR Logic
4
Both bits must be “on” in order for the resulting bit to be “on” AND Logic
5
X-OR Logic If one bit or the other bit is “on”, but not both, then the resulting bit is “on”.
6
OR Family 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.
7
AND Family 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”.
8
X-OR Family 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.
9
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.
10
TMFLAGBYTE,X’C3’ CC=3 All tested bits are 1’s
11
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: XIBLUESW,X’80’
12
Translation Instructions Translate – TR Translate and Test – TRT Execute - EX p.328 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.
13
Translate - TR 2 Operands: 1.pattern to be translated 2.table of replacement characters TREBCDIC,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.
14
0123456789ABCDEF 0_40 1_40 2_40 3_40 4_40 5_40 6_40 7_40 8_40 9_40 A_40 B_40 C_40 D_40 E_40 F_ 30313233343536373839 40
15
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).
16
Translate - TR TREBCDIC,ASCIITAB where EBCDIC contains: 1984 which in hex is: 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 indexed- to location in memory.
17
Translate - TR TREBCDIC,ASCIITAB Any other bit configuration would give back a blank character – X’40’
18
Defining ASCIITAB ? ASCIITABDS0CL256 DC16X’40’ *00-0F DC16X’40’ *10-1F DC16X’40’ *20-2F DC16X’40’ *E0-EF DCX’303132333435’ *F0-F5 DCX’36373839’*F6-F9 DC12X’40’*FA-FF
19
Translate-and-Test - TRT Similar to TR in the way it works –1 st operand acts as an index into 2 nd operand –2 nd 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
20
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
21
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.
22
0123456789ABCDEF 0_40 1_40 2_40 3_40 4_0440 08400C1040 5_1440 181C2040 6_242840 2C40 7_40 3034383C40 8_40 9_40 A_40 B_40 C_4000 40 D_4000 40 E_40 00 40 F_00 40
23
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.
24
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
25
Translate-and-Test instruction is: 1 st 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) LAR2,BRTABLE TRTUNPKINST(21),TRTTABLE
26
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 –Table entry (X’04’) is placed in low order byte of GPR 2 (which contained X’3000’) –CC is set to 1 (non-zero byte found) 00 00 00 00 00 00 CA 84 GPR 1 00 00 00 00 00 00 30 04 GPR 2 BC4,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!
27
Condition Code Set0123 Mask Bit Value8421 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 Contents of Branch Table 3000 BC 4,0(2) BLANKSP 3004 BC 4,0(2) PERIOD 3008 BC 4,0(2) LEFT PAREN 300C BC 4,0(2) PLUS SIGN 3010 BC 4,0(2) AMPERSAND 3014 BC 4,0(2) DOLLAR SIGN 3018 BC 4,0(2) ASTERISK 301C BC 4,0(2) RIGHT PAREN 3020 BC 4,0(2) DASH 3024 BC 4,0(2) FORW’D SLASH 3028 BC 4,0(2) COMMA 302C BC 4,0(2) POUND SIGN 3030 BC 4,0(2) AT SIGN 3034 BC 4,0(2) APOSTROPHE 3038 BC 4,0(2) EQUAL SIGN 303C I think you get the idea…
28
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 (2 nd byte) of the 2 nd 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 1 st operand) Most often used with TRT RX format: EXR1,D2(X2,B2)
29
FORMA T OPERANDS RRR1,R2 RSR1,R3,D2(B2) RXR1,D2(X2,B2) SID1(B 1),I2 SS(1)D1(L,B1),D2(B2) SS(2)D1(L1,B1),D2(L2,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: 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.
30
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. EXRRIC5,=X’14’insert X’14’ into R5 EX5,ADDREGAdd R4 to R1 --- DS0H ADDREGAR0,0target 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. IC6,X’DF’insert X’DF’ into R6 EX6,STOREGSstore regs 13-15 --- SAVEREGSDS3F3 fullwords DS0Hboundary alignment to even STOREGSSTM0,0,SAVEREGSRS target instruction
31
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. IC7,=X’20’put X’20’ into R7 EX7,STORCHstore right byte of R7 --- SAVEBYTEDSCone byte – character DS0Hforce even address STORCHSTC0,SAVEBYTERX 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. IC8,=X’5B’place X’5B’ into R8 EX8,MOVIMMmove $ to print area --- PRINTDCCL133’ ‘print area blanked out DS0Hforce boundary alignment MOVIMMMVIPRINT+20,X’00’target instrution
32
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: IC9,=X’02’insert X’02’ into R9 EX9, MOVECHARmove *** to print area --- PRINTDCCL133’ ‘print area definition DS0H MOVECHARMVCPRINT+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: IC10,=X’32’insert X’32’ into R10 EX10,ADDPKAdd FLD2 to FLD1 --- FLD1DSPL4 FLD2DSPL3 DS0H ADDPKAPFLD1(0),FLD2(0)target of EX instruction
33
Last EX Example 003802 1711 6XR1,1Clear Reg 1 003804 4190 4026 7LA9,NAMADDRAddr 1 st length indicator 003808 4310 9000 8IC1,0(0,9)length of NAME in Reg 1 00380C 1881 9LR8,1save length in Reg 8 00380E 061011BCTR1,0decrement Reg 1 by 1 003810 4190 900112LA9,1(0,9)increment for NAME field 003814 4410 40D613EX1,M30MOVEexecute M30MOVE 003818 1A9814AR9,8addr of NAME + length for 2 nd indicator 00381A 4310 900016IC1,0(0,9)length of addr in Reg 1 00381E 061017BCTR1,0decrement reg 1 by 1 003820 4190 900118LA9,1(0,9)increment for address field 003824 4410 40DC19EX1,M40MOVEexecute M40MOVE 20 *--- 21 *--- 22 *--- 00382823 NAMADDRDSCL42variable name & address 00385224 PRLINEDSCL133print area 0038D7 00 0038D826 M30MOVEMVCPRLINE+20(0),0(9)move NAME to Print 0038DE27 M40MOVEMVCPRLINE+50(0),0(9)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
34
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 SMITH423 BASIN ST PRINT + 20PRINT + 40
35
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
36
Some Edit Patterns CharMeaning 20 Digit selector 21 Significance starter 40 Blank character 4B Decimal point 60 Minus sign 6B comma
37
Detailed Edit Example 02 57 42 6C Source: 40 20 20 6B 20 21 20 4B 20 20 40 C3 D9 Pattern: EDPATTERN(13),SOURCE(4) b d d, d ( d. d d b C R 12001203 1000100C 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.
38
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: SYMBOLMEANING b (hex 40)blank character ( (hex 21)significance started d (hex 20)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. PATTERNDIGIT SIGNIFICANCE INDICATOR (before/after) RULE LOCATION 1000-100C boff/offleave (1)bdd,d(d.ddbCR d0off/offfillbbd,d(d.ddbCR d2 off/on(2)digitbb2,d(d.ddbCR,on/onleavesame d5on/ondigitbb2,5(d.ddbCR (7on/ondigitbb2,57d.ddbCR d4on/ondigitbb2,574.ddbCR.on/onleavesame d2on/ondigitbb2,574.2dbCR d 6 + on/off(3)digitbb2,574.26bCR boff/offfillsame Coff/offfillbb2,574.26bbR Roff/offfillbb2,574.26bbb
39
And the Answer Is … Pattern: 40 40 F2 6B F5 F7 F4 4B F2 F6 40 40 40 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. which prints as: 2,574.26 After ED executes, the condition code is set. 0result is 0 1result is negative 2result is positive 3not used
40
More Examples of ED on p. 83 in Your Textbook Suppress Leading Zeroes Significance Starting Insert commas and decimal points Negative values handling
41
EDMK Used for printing money values Same as Edit instruction except that the address of the 1 st 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
42
02 57 42 6C 40 20 20 6B 20 21 20 4B 20 20 40 C3 D9 Source: Pattern: EDMK Example EDMKPATTERN(13),SOURCE(4) BCTR1,0 MVI0(1),C’$’ 40 5B F2 6B F5 F7 F4 4B F2 F6 40 40 40 Pattern: $2,574.26 Printed Output:
43
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
44
Exercise Due next week Optional / Extra Credit Bit Bash Assignment
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.