diff --git a/src/cmd/cat.s b/src/cmd/cat.s index d93ac58..39b6a87 100644 --- a/src/cmd/cat.s +++ b/src/cmd/cat.s @@ -1,54 +1,57 @@ -" cat +" cat: cat [arg1 arg2 ...] - lac 017777 i " exit if four argument words only? - sad d4 - jmp nofiles - lac 017777 - tad d1 " why not tad d5 (add 5)? - tad d4 - dac name " name = *(017777) + 5 + " 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 nofiles " 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 " and save the pointer in name + dac name loop: - sys open; name; 0; 0 " open file, get fd back - spa " exit if fd == -1 - jmp badfile + sys open; name; 0; 0 " Open file, get fd back + spa + jmp badfile " Negative fd, exit with an error message dac fi " save file descriptor in fi 1: jms getc " get a character in AC sad o4 - jmp 1f + jmp 1f jms putc " write the character on stdout jmp 1b 1: - lac fi + lac fi " Close the file descriptor in fi sys close loop1: -4 - tad 017777 i + tad 017777 i " Subtract 4 from the count of argument words dac 017777 i - sad d4 - jmp done - lac name - tad d4 + sad d4 " Is the value 4, i.e. no args left? + jmp done " Yes, so exit + + lac name " Still an argument, so move up + tad d4 " to the next filename argument dac name - jmp loop + jmp loop " and loop back to cat this file badfile: - lac name - dac 1f + 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 four words of the filename lac d8 - sys write; 1:0; 4 - lac d8 - sys write; 1f; 2 - jmp loop1 + sys write; 1f; 2 " and then write " ?\n" + jmp loop1 " Now try doing the next argument + +1: 040;077012 " String literal: " ?\n" -1: 040;077012 nofiles: lac d8 - sys write; 1f; 5 " write 5 words to fd 8? + sys write; 1f; 5 " Write "No files\n" to stderr sys exit " and exit 1: ; 040; ;;