DOSPLUS 3.5 for the Model III - SR/CMD Disassembled
Page Customization
Page Index
SR/CMD
Other Navigation
Introduction/Summary
DOSPLUS 3.5 SR/CMD Disassembly - Global Search and Replace of BASIC Text (Model III)
SR/CMD is the BASIC global search and replace utility of DOSPLUS 3.5, version 1.7. It is a load module of 3 blocks, 551 bytes, from 5200H to 5426H, with the transfer address 5200H, after the two comment records (the name SR and the copyright notice). The Model I and Model III files are identical byte for byte, and so are the two pages apart from their links.
SR works only from Extended Disk BASIC, as CMD"SR",search[,replace][,first-last]: BASIC/CMD's CMD (5C48H) passes the two characters SR to CMNDI@ as a DOS command, the DOS loads SR/CMD at 5200H, in the overlay area that BASIC/CMD leaves free, and calls it. SR finds BASIC's text pointer after "SR" on the stack, under the return address of CMNDI@ (5212H). Run from the DOS command level (return address 402DH) it only shows its heading (5209H). TBASIC cannot run it, since its CMD runs no DOS commands. SR never returns to CMNDI@: it ends at the ROM's READY, which also resets the stack.
The Parts of SR/CMD
| Address | Contents |
|---|---|
| 5200H-5240H | The start: the caller test, the search and replace strings, the line range (5200H). |
| 5241H-5285H | The loop over the lines in the range, each expanded into the input buffer (5241H). |
| 5286H-52FCH | The search and the replacement in the buffer (5286H). |
| 52FDH-5356H | A changed line is put back into the program (52FDH). |
| 5357H-53B7H | Routines: take a string (5357H), list a line (5377H), pass a 00H (538EH), the room check (5394H), the line too long (53AFH). |
| 53B8H-5628H | The texts and the variables (53B8H). |
How SR Works
The search string must not be empty (Illegal function call). A second string after a comma turns on the replace mode; a number after the comma instead is the line range, given as for LIST (the ROM's 1B10H): n, n-, -n, n-m, or none for the whole program. The current line number is set to FFFFH, so that an error shows as in the direct mode (523AH). Each line in the range is expanded into the input buffer by the ROM's LIST code (2B7EH), so the search compares the text as LIST shows it, keywords spelled out, and not the tokenized bytes. In the search mode a line with a match is listed once (its number, a space and its text) and BREAK and SHIFT @ are tested between the lines (524CH). In the replace mode every match in the line is replaced, the search going on after the replace string, so that a replace string that holds the search string is not searched again (52F2H). A changed line is listed, taken out of the program, tokenized again by the ROM (1BC0H) and put back with its line number; at the end the line links are rebuilt (1AF8H) and the variables cleared (1B5DH), and SR ends at READY.
Differences from the Manual
The manual (Disk BASIC, page 5-51) does not say that a search string or a range is compared with the listed text of the lines, that the search mode lists a line only once however many matches it has, or that the variables are cleared after a replacement. Two limits are not in the manual: a line that would grow past 255 characters shows Line too long to edit and keeps its old text, though the line listed shows the replacements made before the one that was too long (53AFH); and when the memory is too small for a changed line, the ROM's Out of memory is shown after the old line was already taken out of the program, so that line is lost (5394H).
Variables
SR/CMD Variables
| Address Size | Contents |
|---|---|
| 5426H 1 byte | Replace mode 00H: search only; otherwise the first character of the replace string's expression (5226H). |
| 5427H 1 byte | Line length The length of the text in the buffer with its 00H (5282H). |
| 5428H 1 byte | Change flag Not 00H once a match in the line was replaced (52D5H). |
| 5429H-5528H 256 bytes | Search string Its length, then the characters (5216H). |
| 5529H-5628H 256 bytes | Replace string Its length, then the characters (5229H). |
Memory Outside SR/CMD
| Address Size | Contents |
|---|---|
| 40A2H-40A3H 2 bytes | Current line number Set to FFFFH, the direct mode (523DH). |
| 40A7H-40A8H 2 bytes | Input buffer The ROM's input buffer, which holds the listed text of the line (5277H). |
| 40ECH-40EDH 2 bytes | Current line The number of the line being searched (526CH). |
| 40F9H-40FAH 2 bytes | VARTAB The end of the program, moved with each changed line (532EH). |
Outside Routines
| Address | Contents |
|---|---|
| 4467H | SYS0/SYS: DSPLY@, the heading (520CH). |
| 402DH | SYS0/SYS: EXIT@ (520FH). |
| 1B10H | ROM: the line range of LIST (5236H). |
| 2B7EH | ROM: expand a line into the input buffer (5274H). |
| 1BC0H | ROM: tokenize the input buffer (5324H). |
| 1AF8H | ROM: rebuild the line links (5261H, 53A3H). |
| 1B5DH | ROM: the set-up of RUN, which clears the variables (5264H). |
| 2337H | ROM: evaluate an expression (5358H). |
| 1D9BH | ROM: test BREAK and SHIFT @ (524CH). |
| 1A19H | ROM: READY (525EH, 5267H). |
Disassembly
5200H - SR
SR/CMD's transfer address. SR is run from Extended Disk BASIC as CMD"SR",search[,replace][,range]: BASIC/CMD's CMD passes "SR" to CMNDI@ as a DOS command, and the DOS loads SR/CMD at 5200H and calls it. A call from the DOS command level, whose return address is 402DH, only shows the heading and goes back to the DOS. From BASIC the text pointer after "SR" is on the stack: SR takes the search string (5429H), the replace string (5529H, which turns on the replace mode, 5426H) and the line range (the ROM's 1B10H), and walks the lines in the range. It never returns to CMNDI@: it ends at the ROM's READY.
5241H - The Next Line
The loop over the lines. Register Pair HL is the address of a line, the last line number of the range is on the stack. The end of the program, or a line past the range, ends the work: in the search mode at READY; in the replace mode after the ROM has rebuilt the line links (1AF8H) and cleared the variables (1B5DH), since the program text moved. A line in the range is expanded into the input buffer by the ROM's LIST code (2B7EH) and searched (5286H).
5286H - The Search
Compares the search string with the text in the buffer at each place, from Register Pair DE on, with Register C the count of the bytes left (the 00H included). In the search mode the first match lists the line (5377H) and the next line follows. In the replace mode every match is replaced: the search string is taken out of the buffer, the replace string put in its place, and the search goes on after the replace string, so that the new text is not searched again. A line that would become longer than 255 characters shows Line too long to edit (53AFH) and is listed, but the program keeps the old line. At the end of a changed line the program is changed (52FDH).
52FDH - Change the Line
The end of a line's text. A line with no change goes on to the next line. A changed line is listed (5377H), the old line is taken out of the program by moving the rest of the program down, the new text in the buffer is tokenized by the ROM (1BC0H), the room is checked (5394H), the rest of the program is moved up again and the new line (a link, the line number of 40ECH and the tokenized text) is put in its place. The line links are rebuilt only at the end (5261H); the new line's link gets a high byte that is not 00H, so that the line is not taken as the end of the program in the meantime.
5357H - Take a String
Evaluates the string expression at Register Pair HL (the ROM's 2337H; not a string: Type mismatch) and copies it to Register Pair DE as a length byte and the characters. Returns Register Pair HL at the text after the expression, less 1 (so that the RST 10H after the call reads the next character), and the Z FLAG for an empty string.
5377H - List the Line
Shows the line number of 40ECH, a space, the text in the input buffer and a carriage return.
538EH - Past the 00H
Moves Register Pair HL past the next 00H byte.
5394H - The Room Check
Register Pair HL is the new end of the program. Returns when the end is at least 58 bytes (3AH) below the Stack Pointer. Otherwise the line links are rebuilt, VARTAB is set after the program and the ROM shows Out of memory; the changed line is then lost, since its old text was already taken out of the program (531EH).
53AFH - Too Long
The replacement would make the line longer than 255 characters: the message Line too long to edit is shown, and the line is listed as in the search mode (52AAH) with the replacements made before this one; the program keeps the old line.
53B8H - The Texts and the Variables
Data.