macro/ex4b.mac

71 lines
1.9 KiB
Plaintext

TITLE REVERSE - Example 4-B
SEARCH MONSYM
COMMENT $
This is another program to demonstrate the reversal of the
characters of an input line.
The structure or organizatino of this program is similar to
that found in example 4-A. In contrast to example 4-A, this
program uses the byte instructions to make character processing
easier.
$
A=1 ;Assign symbolic names to
B=2 ;the accumulators
C=3
P=17 ;Symbolic for push down pointer
BUFLEN==40
PDLEN==240
IBUFR: BLOCK BUFLEN ;buffer for input line
OBUFR: BLOCK BUFLEN+1 ;buffer for output line
PDLIST: BLOCK PDLEN
START: RESET ;initialize IO
MOVE P,[IOWD PDLEN,PDLIST] ;initial stack pointer
GETLIN: HRROI A,[ASCIZ/Reverse a line: /]
MOVE C,A ;save this prompt for RDTTY
PSOUT ;prompt for a line
HRROI A,IBUFR ;Setup buffer space for RDTTY
MOVEI B,BUFLEN*5-1 ;size of buffer
RDTTY ;read a line
ERJMP ERROR
MOVE B,[POINT 7,IBUFR] ;Initial input pointer to line
INLOOP: ILDB A,B ;gobble a byte
CAIN A,15 ;is it carriage return
JRST INLOOP ;discard return
CAIN A,12 ;is it the line-feed?
JRST INDONE ;yes. finish with line
JUMPE A,INDONE ;jump in case of input buffer overrun.
PUSH P,A ;normal character, save it.
JRST INLOOP ;loop thru line
INDONE: CAMN P,[IOWD PDLEN,PDLIST] ;is the stack empty?
JRST STOP ;yes, line was all blank
HRROI A,[ASCIZ/Reversed line: /] ;tell them what's coming
PSOUT
MOVE B,[POINT 7,OBUFR] ;Initialize output pointer
OUTLOO: POP P,A ;get a byte from stack
IDPB A,B ;store the byte
CAME P,[IOWD PDLEN,PDLIST] ;Is the stack unwound now,
JRST OUTLOO ;no. loop until stack empties
MOVEI A,15 ;stack is now empty.
IDPB A,B ;add cr at end of line
MOVEI A,12
IDPB A,B ;add lf
MOVEI A,0
IDPB A,B ;end with null for PSOUT
HRROI A,OBUFR
PSOUT ;type buffer
JRST GETLIN
ERROR: HRROI A,[ASCIZ/ERROR in RDTTY.
/]
PSOUT
STOP: HALTF ;stop at a blank line
JRST STOP ;stay stopped.
END START