I refactored tools/wktcat.s to lose the repeated code, and I also
improved the comments in the code. I'm happy with it, now.
This commit is contained in:
+18
-32
@@ -9,9 +9,6 @@
|
|||||||
"
|
"
|
||||||
" ./a7out a.out z1 z1
|
" ./a7out a.out z1 z1
|
||||||
" <the text you typed in will be displayed twice>
|
" <the text you typed in will be displayed twice>
|
||||||
"
|
|
||||||
" Also, the current coding is hideous and needs refactoring.
|
|
||||||
" I'm still coming to grips with PDP-7 assembly code.
|
|
||||||
|
|
||||||
main:
|
main:
|
||||||
" Load the pointer pointer in 017777 to see if we have any arguments
|
" Load the pointer pointer in 017777 to see if we have any arguments
|
||||||
@@ -19,23 +16,24 @@ main:
|
|||||||
sad d4 " Skip if we have more than four argument words
|
sad d4 " Skip if we have more than four argument words
|
||||||
jmp stdinout " Only four argument words, so no arguments
|
jmp stdinout " Only four argument words, so no arguments
|
||||||
|
|
||||||
lac 017777
|
lac 017777 " Move five words past the argument word count
|
||||||
tad d1 " Bump AC up by 5 so that it points at the first argument
|
tad d1 " so that AC points at the first argument
|
||||||
tad d4
|
tad d4
|
||||||
|
|
||||||
" This section opens files, and copies their contents to standard output
|
" This section opens files and copies their contents to standard output
|
||||||
catfiles:
|
catfiles:
|
||||||
" We start with AC pointing to an argument. Save it at label 1f
|
" We start with AC pointing to an argument. Save it at the "name" label
|
||||||
dac name
|
dac name
|
||||||
|
|
||||||
" Open the file and get the fd into AC
|
" Open the file and get the fd into AC
|
||||||
sys open; name:0; 0; 0
|
sys open; name:0; 0; 0
|
||||||
spa
|
spa
|
||||||
jmp badfile " Bad fd, exit with an error message
|
jmp badfile " Negative fd, exit with an error message
|
||||||
dac fd " Save the file descriptor
|
dac fd " Save the file descriptor
|
||||||
|
|
||||||
fileloop:
|
fileloop:
|
||||||
" Read five words into the buffer from the input file
|
" Read five words into the buffer from the input file
|
||||||
|
" Five was chosen arbitrarily
|
||||||
lac fd
|
lac fd
|
||||||
sys read; buf; 5
|
sys read; buf; 5
|
||||||
spa " Skip if result was >= 0
|
spa " Skip if result was >= 0
|
||||||
@@ -76,26 +74,15 @@ end:
|
|||||||
sys exit
|
sys exit
|
||||||
|
|
||||||
" This section copies from standard input to standard output
|
" This section copies from standard input to standard output
|
||||||
|
" We cheat by setting the fd value to zero and storing 8
|
||||||
|
" into the argc word count, so that when the code hits
|
||||||
|
" fileend, the word count drops to 4 and we exit.
|
||||||
stdinout:
|
stdinout:
|
||||||
" Read five words into the buffer from stdin
|
lac d8
|
||||||
" Five was chosen arbitrarily
|
dac 017777 i " Save 8 into the word count
|
||||||
lac d0
|
lac d0
|
||||||
sys read; buf; 5
|
dac fd " Save file descriptor 0
|
||||||
spa " Skip if result was >= 0
|
jmp fileloop
|
||||||
jmp error " Result was -ve, so error result
|
|
||||||
sna " Skip if result was >0
|
|
||||||
jmp end " Result was zero, so nothing left to read
|
|
||||||
|
|
||||||
" Save the count of words read in
|
|
||||||
dac 1f
|
|
||||||
|
|
||||||
" Write five words from the buffer to stdout
|
|
||||||
lac d1
|
|
||||||
sys write; buf; 1:0
|
|
||||||
|
|
||||||
" and loop back for more words to read
|
|
||||||
jmp stdinout
|
|
||||||
|
|
||||||
|
|
||||||
" This code comes from the real cat.s
|
" This code comes from the real cat.s
|
||||||
badfile:
|
badfile:
|
||||||
@@ -107,12 +94,11 @@ badfile:
|
|||||||
sys write; 1f; 2
|
sys write; 1f; 2
|
||||||
sys exit " and exit
|
sys exit " and exit
|
||||||
|
|
||||||
1: 040; 077012
|
1: 040; 077012 " String literal: " ?\n"
|
||||||
|
|
||||||
|
|
||||||
error:
|
error:
|
||||||
" Print an "err read" string and exit
|
" Print an "err read" string on stderr and exit
|
||||||
lac d1
|
lac d8
|
||||||
sys write; noreadstr; 5
|
sys write; noreadstr; 5
|
||||||
sys exit
|
sys exit
|
||||||
|
|
||||||
@@ -120,11 +106,11 @@ noreadstr:
|
|||||||
<er>;<r 040;<re>;<ad>;012000
|
<er>;<r 040;<re>;<ad>;012000
|
||||||
|
|
||||||
fd: 0 " fd of the open file
|
fd: 0 " fd of the open file
|
||||||
d0: 0 " Constants 0 and 1
|
d0: 0 " Constants 0, 1, 4 and 8
|
||||||
d1: 1
|
d1: 1
|
||||||
d4: 4
|
d4: 4
|
||||||
d8: 8 " stderr seems to have fd 8
|
d8: 8 " stderr seems to have fd 8
|
||||||
minus4: 0777774 " -4 constant
|
minus4: 0777774 " Constant -4
|
||||||
|
|
||||||
" Input buffer for read
|
" Input buffer for read
|
||||||
buf: 0; 0; 0; 0; 0
|
buf: 0; 0; 0; 0; 0
|
||||||
|
|||||||
Reference in New Issue
Block a user