diff --git a/src/other/wktcat.s b/src/other/wktcat.s new file mode 100644 index 0000000..0084ddf --- /dev/null +++ b/src/other/wktcat.s @@ -0,0 +1,105 @@ +" Warren's cat program: cat [arg1 arg2 ...] + +main: + " Load the pointer pointer in 017777 to see if we have any arguments + lac 017777 i + sad d4 " Skip if we have more than four argument words + jmp stdinout " Only four argument words, so no arguments + + lac 017777 " Move five words past the argument word count + tad d1 " so that AC points at the first argument + tad d4 + +" This section opens files and copies their contents to standard output +catfiles: + " We start with AC pointing to an argument. Save it at the "name" label + dac name + + " Open the file and get the fd into AC + sys open; name:0; 0; + spa + jmp badfile " Negative fd, exit with an error message + dac fd " Save the file descriptor + +fileloop: + " Read 64 words into the buffer from the input file + lac fd + sys read; buf; 64 + spa " Skip if result was >= 0 + jmp error " Result was -ve, so error result + sna " Skip if result was >0 + jmp fileend " 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 fileloop + +fileend: + " Close the open file descriptor + lac fd + sys close + + " Subtract 4 from the count of argument words + -4 + tad 017777 i + dac 017777 i + sad d4 " Is the value 4, i.e. no args left? + jmp end " Yes, so exit + + " Still an argument, so move up to the next filename argument + lac name + tad d4 + dac name + jmp catfiles " and loop back to cat this file + +end: + " exit + sys exit + +" 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: + lac d8 + dac 017777 i " Save 8 into the word count + lac d0 + dac fd " Save file descriptor 0 + jmp fileloop + +" This code comes from the real cat.s +badfile: + lac name " Get the pointer to the filename + dac 1f " Store it in 1f below + lac d8 " Load fd 8 which is stderr + sys write; 1:0; 4 " Write the name, max 4 words + lac d8 " Then write " ?\n" + sys write; 1f; 2 + sys exit " and exit + +1: 040; 077012 " String literal: " ?\n" + +error: + " Print an "err read" string on stderr and exit + lac d8 + sys write; noreadstr; 5 + sys exit + +noreadstr: + ;;;012000 + +fd: 0 " fd of the open file +d0: 0 " Constants 0, 1, 4 and 8 +d1: 1 +d4: 4 +d8: 8 " stderr seems to have fd 8 +minus4: 0777774 " Constant -4 + +" Input buffer for read +buf: .=.+64 diff --git a/src/other/wktcp.s b/src/other/wktcp.s new file mode 100644 index 0000000..c8485b8 --- /dev/null +++ b/src/other/wktcp.s @@ -0,0 +1,110 @@ +" Warren's cp program: cp arg1 arg2 + +main: + " Load the pointer pointer in 017777 to see if we have any arguments + lac 017777 i + sad d12 + jmp 1f " We have 12 words, so we have 2 arguments + jmp argserror " Otherwise, print an error and exit + +1: lac 017777 " Move five words past the argument word count + tad d5 " so that AC points at the first argument + + " Save the pointer to the file name + dac name + + " Open the input file and get the fd into AC + sys open; name:0; 0; + spa + jmp badfile " Negative fd, exit with an error message + dac infd " Save the file descriptor + + lac 017777 " Move nine words past the argument word count + tad d9 " so that AC points at the second argument + dac name + dac name2 + + " Open the ouput file and get the fd into AC + sys open; name2:0; 1; + spa + jmp badfile " Negative fd, exit with an error message + dac outfd " Save the file descriptor + +fileloop: + " Read 64 words into the buffer from the input file + lac infd + sys read; buf; 64 + spa " Skip if result was >= 0 + jmp readerror " Result was -ve, so error result + sna " Skip if result was >0 + jmp fileend " Result was zero, so nothing left to read + + " Save the count of words read in + dac 1f + + " Write five words from the buffer to the output file + lac outfd + sys write; buf; 1:0 + spa " Skip if result was >= 0 + jmp writeerror " Result was -ve, so error result + + " and loop back for more words to read + jmp fileloop + +fileend: + " Close the open file descriptors + lac infd + sys close + lac outfd + sys close + sys exit + +" This code comes from the real cat.s +badfile: + lac name " Get the pointer to the filename + dac 1f " Store it in 1f below + lac d8 " Load fd 8 which is stderr + sys write; 1:0; 4 " Write the name, max 4 words + lac d8 " Then write " ?\n" + sys write; 1f; 2 + sys exit " and exit + +1: 040; 077012 " String literal: " ?\n" + +readerror: + " Print an "err read" string on stderr and exit + lac d8 + sys write; noreadstr; 5 + sys exit + +noreadstr: + ;;;012000 + +writeerror: + " Print an "err write" string on stderr and exit + lac d8 + sys write; nowritestr; 6 + sys exit + +nowritestr: + ;;;;;;012000 + + +infd: 0 " fd of the input file +outfd: 0 " fd of the output file +d5: 5 +d9: 9 +d8: 8 " stderr seems to have fd 8 +d12: 12 + +" Input buffer for read +buf: .=.+64 diff --git a/src/other/write_test.s b/src/other/write_test.s new file mode 100644 index 0000000..fae298d --- /dev/null +++ b/src/other/write_test.s @@ -0,0 +1,61 @@ +" Test program for several system calls: open, read, write, close, exit +" Do: +" ./as7 write_test.s > a.out +" ./a7out -d a.out +main: + " Test the lac, dac instructions + lac in + dac out + + " Write hello to fd1 i.e stdout + lac d1 + sys write; hello; 7 + + " Test if the assembler can dac into a mid-line label + lac helloptr + dac 1f + lac d1 + sys write; 1:0; 7 + + " Try to open file fred + sys open; fred; 0; + + " read 5 words into the buffer from stdin: type in 10 or more characters! + lac d0 + sys read; buf; 5 + + " Stop and dump memory, so you can see five words at location 0400 + " Comment out the hlt instruction to test close and exit + hlt + + " close stdin + lac d0 + sys close + + " exit + sys exit + + " We should not get to the halt instruction + hlt + + +" Some memory locations for lac and dac +. = 0100 +in: 023 + +. = 0200 +out: 0 + +" Hello, world\n, two ASCII chars per word +hello: ; ; 040; ; ; ; ; 040040; 040040 + +" Input buffer for read +. = 0400 +buf: 0 + +d0: 0 +d1: 1