Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
If your task is to remove them outright, then your can do it in several ways:
A) TRT - Translate and Test (a single sweep). A succession of these, left justifying the data in the target-area as you move from left to right, with each new TRT beginning at the comma-address (in R1) just found + 1. The TRT requires a 256-byte table, consisting of 256 X'00's and set the comma-offset to X'FF', as the character you want to stop at.
B) TR - Translate - A two phase sweep, with the first sweep, converting commas to spaces in the target-area. The second sweep, left-justify the target-area into a separate 70-byte workarea, until you've addressed the target-area beyond byte-70. Always initialise the separate workarea to spaces beforehand. You'll need three registers for this. Your workarea will be left justified with low-order spaces after the second sweep.
C) BCT - Branch on Count. A single sweep, pointing a register at the target-area start, loading another register with value of 70 (use an LA with an L') and a third register, pointing to a separate 70-byte workarea. In the sweep, move each valid byte (non-comma and non-space) to the workarea. Always initialise the separate workarea to spaces beforehand. Your workarea will be left justified with low-order spaces after this one and only sweep.
To save yourself some time, before doing anything, issue a TRT against the 70-byte target-area, using a 256-byte table, consisting of 256 X'00's and set the comma-offset to X'FF'.
If after the TRT (which inherently uses R1 and R2), a BZ condition-code indicates there aren't any comma's, whereas a BNZ indicates there is at least one.
Example translate-table -
Code:
XLATETBL DC XL256'00' 256-BYTES OF X'00'S
ORG XLATETBL+C',' REDEFINITION
DC XL1'FF' SET COMMA-SLOT TO X'FF'
One of these three should get you on your way, with choice "C" being the most rudimentary.
Joined: 30 Nov 2013 Posts: 917 Location: The Universe
This too late for ravi243's homework turn in. At entry the only requirement is a valid base register.
Code:
...
SCAN TRT DATA,TRTTAB Find a comma
BZ DONE Br if no comma found
LA 14,1(,1) Compute address of the byte after ->
the comma
LA 15,DATA+L'DATA Load address of the end of the ->
data area
SR 15,14 Compute bytes to move
BNP DONE Br if nothing left to move
BCTR 15,0 Reduce length by 1
EX 15,MVC Remove the comma
MVI DATA+L'DATA-1,C' ' Make sure vacated byte is blank
B SCAN Try again
MVC MVC 0(*-*,1),1(1) ** Execute only **
DONE ...
TRTTAB DC 0XL256'0',(C',')X'00',X'04',(256-(*-TRTTAB))X'00'
DATA DC CL70'Moore School, 3300 Walnut Street, Philadelphia PA 1>
9104'
...
Many programmers preset register 1 before the TRT, but it's not necessary here, for two reasons
A default value must be set for use after a condition code 0, but this code exits after condition code 0.
TRT may not set the high order bit or high order 8 bits of register 1, depending on the AMODE, to 0, which will bollix the length calculation for the MVC. It's not necessary here, though this is a good exercise for a lazy reader.
Another issue is an argument can be made the code should resume its scan after it has removed a comma at the point where it detected the comma, to reduce execution time of the TRT. True, but then you have two other issues.
The arithmetic uses CPU time. How much will you save by scanning fewer bytes? Hard to answer, especially since the answer depends on how deep you are into the data area.
More instructions means more potential debugging time.
For what it's worth, the address for the Moore School of the University of Pennsylvania, the former home of ENIAC, is technically incorrect. As indicated here, it's at the intersection of 33rd Street and Walnut Street, but there is no entry to the building on Walnut Street. The main entry is on 33rd Street, so the correct address is something or other South 33rd Street.
Joined: 30 Nov 2013 Posts: 917 Location: The Universe
steve-myers wrote:
... Another issue is an argument can be made the code should resume its scan after it has removed a comma at the point where it detected the comma, to reduce execution time of the TRT. True, but then you have two other issues.
The arithmetic uses CPU time. How much will you save by scanning fewer bytes? Hard to answer, especially since the answer depends on how deep you are into the data area.
...
I played around with code some more. In the process I realized
If there was 1 comma in the last byte it would not be cleared.
The scan length to resume the scan was the same length used to shift the text.
The revised code becomes
Code:
...
LA 15,L'DATA-1 Load initial length
LA 1,DATA Load addr to start scan
SCAN EX 15,TRT Find a comma
BZ DONE Br if no comma found
MVI 0(1),C' ' Replace comma with a blank
LA 14,1(,1) Compute address of the byte after ->
the comma
LA 15,DATA+L'DATA Load address of the end of the ->
data area
SR 15,14 Compute bytes to move
BNP DONE Br if nothing left to move
BCTR 15,0 Reduce length by 1
EX 15,MVC Remove the comma
MVI DATA+L'DATA-1,C' ' Make sure vacated byte is blank
B SCAN Try again
TRT TRT 0(*-*,1),TRTTAB ** Execute only **
MVC MVC 0(*-*,1),1(1) ** Execute only **
DONE ...
TRTTAB DC 0XL256'0',(C',')X'00',X'04',(256-(*-TRTTAB))X'00'
DATA DC CL70'Moore School, 3300 Walnut Street, Philadelphia PA 1>
9104'
The MVI 0(1),C' ' after the BZ corrects the bug.
The two instructions at the start set an initial length and scan address.
Mr. Holland: I looked through the slides in your reference. I had never realized the relative performance of TRT was as good as apparently shown in the slides; it makes a case of sorts for complex instruction set hardware as opposed to RISC hardware! Personally, I never quite believed the case for RISCy hardware, though I'm reluctant to step into that holy war. In any event, the slides in Mr. Holland's reference suggest that the optimization implied by the revised code is hardly necessary.
Joined: 30 Nov 2013 Posts: 917 Location: The Universe
Mt. Holland's reference discussed the use of the SRST (Search String) instruction for the case where just one character is being searched for. I remember researching the instruction in the 1990s and concluded it would be useful for C language programs and library functions with their null character terminated strings, and promptly forgot it and never tried to use it. Since it does not need to load bytes from a table for each character the instruction is testing, it should be faster than the TRT instruction for this application.
In practice, however, since the strings are fairly short – 70 bytes in our application – there is little chance of measuring the performance improvement. In any event, here is the resulting program.
Code:
...
LA 3,DATA Load address of start of data
LHI 0,C',' Load stop character
NEXT LA 4,DATA+L'DATA Load address of end of data
SRST 4,3 Scan for a comma
BC B'1011',DONE Br if we did not find a comma
MVI 0(4),C' ' Set comma to blank
LA 15,DATA+L'DATA-1 Load address of end of data
SR 15,4 Compute length to move
BM DONE Br if nothing to move
EX 15,MVC Shift data
MVI DATA+L'DATA-1,C' ' Set vacated character to blank
LR 3,4 Set new start of scan
B NEXT And try again
MVC MVC 0(*-*,4),1(4) ** Execute only **
DONE ...
DATA DC CL70'3401 MARKET STREET, PHILADELPHIA PA 19104'
Joined: 30 Nov 2013 Posts: 917 Location: The Universe
Mr. O'Boyle proposed using the BCT instruction for loop control, and even provided an admittedly untested example.
I admit I tend to avoid BCT. When possible I prefer BXLE. BCT usually requires two instructions to close a loop; when you use BXLE you can usually close the loop with just the BXLE. The following tested fragment uses BCT; the readers will see the similarities with the examples that use the TRT and SRST instructions.
Code:
...
LA 3,DATA Load address of start of data
LA 0,L'DATA Load bytes in string
NEXT CLI 0(3),C',' Test if comma
BNE BUMP Br if we did not find a comma
MVI 0(3),C' ' Set comma to blank
LA 15,DATA+L'DATA-1 Load address of end of data
SR 15,3 Compute length to move
BM DONE Br if nothing to move
EX 15,MVC Shift data
MVI DATA+L'DATA-1,C' ' Set vacated character to blank
BUMP AHI 3,1 Update the scan pointer
BCT 0,NEXT And try again
B DONE
MVC MVC 0(*-*,3),1(3) ** Execute only **
DONE ...
DATA DC CL70'3401 MARKET STREET, PHILADELPHIA PA 19104'
Readers may also understand why instructions like TRT or SRST will often outperform code like this example. This example uses 4 instructions for each byte that is not a comma; worse, it fetches the test byte one byte at a time. Instructions like SRST or TRT can fetch multiple bytes in the string when starting and don't have to repeat the fetch as often.