In one of the assignments in this course, we have a data file in which each line contains 0 or more integers, separated by spaces. We might have lines like these:
45 789
271
62 511 9999
Suppose we want to read all these integers and store them in a table as fullwords. We can do this using TRT, EX and CVB.
For the sake of simplicity, we will assume all the integers are non-negative. We will also assume there are no more than 100 numbers in the file.
We will need to declare some items:
BUFFER DC 81C' ' Input buffer
*
TABLE DS 100F Table in which to store integers
EOT DS F End-of-table address
*
DIGITS DC 256X'00' Search table to look for digits
ORG DIGITS+X'F0'
DC 10X'01'
ORG
*
SPACES DC 256X'00' Search table to look for spaces
ORG DIGITS+X'40'
DC X'01'
ORG
*
TEMP DS D Used with CVB
*
THEPACK PACK TEMP(8),0(0,5) Executed by EX
The code we need would look something like this:
LA 7,TABLE 7 is our table pointer
LOOP1 XREAD BUFFER,80 Read a line
BC B'0100',END1 End-of-file? End the outer loop
LA 1,BUFFER 1 = address of BUFFER
LOOP2 TRT 0(80,1),DIGITS Search for a digit
BC B'1000',END2 No numbers? End the inner loop
LR 4,1 4 = address of first digit
TRT 0(80,1),SPACES Search for a space
LR 5,1 5 = address of next space
SR 5,4 Now 5 = actual length
BCTR 5,0 Adjust the length
EX 5,THEPACK Pack the number into TEMP
CVB 6,TEMP Now 6 = the number (binary)
ST 6,0(0,7) Store it in the table
LA 7,4(0,7) Advance table pointer
BC B'1111',LOOP2 Repeat the inner loop
END2 DS 0H
BC B'1111',LOOP1 Repeat the outer loop
END1 DS 0H
ST 7,EOT Store end-of-table address
In the PACK instruction, the lengths are 8 and 0. When this is encoded, they will be 7 and 0. (A length of 0 is encoded as 0, not -1.) When the EX is executed, we will have a copy of the encoded form of the PACK instruction, and the second byte will be modified:
second byte = second byte OR 4th byte of register 5
All that is in register 5 is the adjusted length, so all of it is zeros except the last hex digit. The effect of the OR instruction is to leave the 7 alone and replace the 0 with the adjusted length.
Why was BUFFER declared as 81 characters long? The last character is there, initialized as a space, so we can be sure TRT will find a space after the last number. In theory, the number could be in the last few columns of BUFFER and we want to make sure register 1 will point to the space after it.
If we want to deal with negative integers as well, we would need to modify DIGITS to search for C'+' and C'-' as well. After using TRT, we would need to use CLI to see if we found C'+' or C'-'. If we did find a sign, we would need to advance register 1 by 1 to point to the first digit. If the sign was negative, we would later need to use LCR to fix the sign of the binary value in register 6 before storing it.
If the file might contain more than 100 integers, we could deal with that as well. We could maintain a counter and see if the counter exceeds 100, or we could check register 7 at the bottom of LOOP2 to see if it is equal to A(TABLE+400). Either way, if the table is full, stop both loops by branching to END1.
One other point is that if we have a number in the file which is too large into a 32-bit signed binary integer, CVB will ABEND the program with a fixed-point divide exception. Therefore, we might want to add code to check for this. Specifically, if the actual length of the number (in register 5 before BCTR) is more than 9 bytes, we should probably not proceed with EX and CVB.