Translate

Overview

Sometimes, as we process data, we need to change particular characters to other characters. For instance, we may have someone's name expressed as "henry" and we would like to change the first letter to upper case: "Henry".

A fancier example might be this: someone is filling in information on a form, and one line says "Age: 22 Telephone: 8157531111". We read this line as 35 characters, but all we are actually interested in is the numbers. It would be convenient to change all the non-digit characters to spaces: " 22 8157531111".

The Translate instruction will help with this. Its format looks like this:

     TR   D1(L,B1),D2(B2)

where D1(B1) is the address of a character variable L bytes long and D2(B2) is the address of a "translate table" as described below.


What is a translate table?

Each character is stored as one byte, and the value of a byte can be viewed as a small integer. The translate table is a table of 256 bytes. What the TR instruction does is replace each byte in the first argument with a byte from the table.

For instance, suppose we find X'D2' in the first argument. (That is C'K'.) We can view X'D2' as a number in base 16 (or 210 in base 10). We replace X'D2' by whatever is in the corresponding location in the table, that is, the byte at table + X'D2'.

This is done for each byte in the first argument. It is often the case that a byte is replaced by itself.

If we wanted a translate table that would change absolutely nothing, it would look like this:

TABLE    DC    XL16'000102030405060708090A0B0C0D0E0F'
         DC    XL16'101112131415161718191A1B1C1D1E1F'
         DC    XL16'202122232425262728292A2B2C2D2E2F'
         DC    XL16'303132333435363738393A3B3C3D3E3F'
         DC    XL16'404142434445464748494A4B4C4D4E4F'
         DC    XL16'505152535455565758595A5B5C5D5E5F'
         DC    XL16'606162636465666768696A6B6C6D6E6F'
         DC    XL16'707172737475767778797A7B7C7D7E7F'
         DC    XL16'808182838485868788898A8B8C8D8E8F'
         DC    XL16'909192939495969798999A9B9C9D9E9F'
         DC    XL16'A0A1A2A3A4A5A6A7A8A9AAABACADAEAF'
         DC    XL16'B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF'
         DC    XL16'C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF'
         DC    XL16'D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF'
         DC    XL16'E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF'
         DC    XL16'F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF'

For each possible byte value N (0 to 255), the value at TABLE + N is N, so each byte will be replaced by itself.

Of course, if we want to do anything useful, we will need to modify TABLE.

Example 1: Suppose we want to change lower-case letters to upper case and change. nothing else. The table would look somewhat the same but not exactly. Lower-case letters are stored as X'81' through X'89', X'91' through X'99' and X'A2' through X'A9'. The corresponding upper-case letters are stored as X'C1' through X'C9', X'D1' through X'D9' and X'E2' through X'E9'. The table would be:

UPTABLE  DC    XL16'000102030405060708090A0B0C0D0E0F'
         DC    XL16'101112131415161718191A1B1C1D1E1F'
         DC    XL16'202122232425262728292A2B2C2D2E2F'
         DC    XL16'303132333435363738393A3B3C3D3E3F'
         DC    XL16'404142434445464748494A4B4C4D4E4F'
         DC    XL16'505152535455565758595A5B5C5D5E5F'
         DC    XL16'606162636465666768696A6B6C6D6E6F'
         DC    XL16'707172737475767778797A7B7C7D7E7F'
         DC    XL16'80C1C2C3C4C5C6C7C8C98A8B8C8D8E8F'
         DC    XL16'90D1D2D3D4D5D6D7D8D99A9B9C9D9E9F'
         DC    XL16'A0A1E2E3E4E5E6E7E8E9AAABACADAEAF'
         DC    XL16'B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF'
         DC    XL16'C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF'
         DC    XL16'D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF'
         DC    XL16'E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF'
         DC    XL16'F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF'

End of Example

Example 2: Suppose we want to change anything except a digit to a space. The table can then we written much more easily:

DIGITS   DC    256X'40'
              ORG   DIGITS+X'F0'
              DC    XL10'F0F1F2F3F4F5F6F7F8F9F'
              ORG

End of Example

To make a translate table, we need to know what is our data, what we want to change, and how we want to change it.


Example of using TR

Suppose our data is someone's answers to a multiple-choice test. The only characters in it other than blanks should be C'A' through C'E'. It is possible the test-taker used lower-case letters. We want to fix this.

We have UPTABLE (as above) and we have

BUFFER   DC    80C

We can do this in one line:

     TR   BUFFER(80),UPTABLE