[net.micro.apple] DOS, Applesoft, and the Moniter

kamath@reed.UUCP (Sean Kamath) (05/21/86)

In article <2620@sdcc6.UUCP> ix742@sdcc6.UUCP (James Hayes) writes:
>wtm@neoucom.UUCP (Bill Mayhew) writes in article <203@neoucom.UUCP>:
>
>>                                       .....  DOS links into
>>BASIC's "charget" routine.  To save time, dos ingores everything
>>until it detectes a carriage return followed by control-D in the
>>input stream, it then parses the line until the next return comes
>>up.  Charget is a short subrountine that is stored in page zero,
>>beginning at $39 (I think, but check the "Applesoft Programmer's
>>reference manual" to be sure.)
>
>Nope.  DOS never looks at CHARGET.  DOS looks at the KEYboard INput
>(KEYIN) vector.  This lies at $38/$39 (Low/High) and contains the
>address of the routine that reads from the keyboard.  KEYIN is used
>to monitor what you type. I.e. "CATALOG".
>
>DOS also looks out Character OUT (COUT $36/$37) for the routine to
>listen to the output of BASIC, etc..  It looks for commands like
>"OPEN", "CLOSE", "CATALOG" that are preceded by <CR>^D{command}.
>This is how BASIC works with DOS.
>
>I can't remember the charget address off hand, but I beleive it
>lives near $10-$20.     
>
>>	I've found the best way to take over control from DOS is
>>to attack DOS itself.  If I remember right, you can steal contol
>>back by planting you vector in dos somewhere around $AA65.
>>Disassemble DOS, and look for the vector that points to the input
>>routine in the monitor ROM.  I've used this approach to add
>>a keyclick subroutine.
>
>A slightly more portable way to do this is the following:
>
>Set up the addresses of the input/output hooks to point to your
>routines, and then ask DOS to connect them for you.  This is done
>by calling the DOS reconnect vector at $3EA.  This will automatically
>modify DOS's internal vectors for you.  This is more reliable, for
>different DOS versions might have different internal vectors.  $3EA
>will always set them properly.
>
>
>I hope I haven't created more confusion than I've gotten rid of...
>--
>Jim Hayes, UCSD  EE/CS Department.
>ARPA: hayes@UCSD.EDU
>UUCP: {ihnp4, gatech, ucbvax, decvax, etc.}!sdcsvax!hayes

OK.  DOS grabs both CWSL and KWSL when it boots.  It stores the pointers
internally, then puts pointers to it's own routine.  If you look at
the source for the ROM, you'll find the COUT simply JMP's to
CWSL($36-37).  Likewise, KEYIN JMP's to KWSL ($38-39).  Upon power up,
these are set to COUT1 and KEYIN1, respectively.  When you go into BASIC
(from the moniter) they point somewhere in the $D000-$F7FF range. (I
hope, I hope :-).  When DOS has them, it looks for that good ol' CR ^D
combo.  If it doesn't find it, it goes to BASIC.  If BASIC doesn't find
something of interest, it goes to the Moniter.  If IT doesn't find
something of use, error.  An error can occure anywhere, like in spelling
;-).  Anyway, that's what happens when you're staring at the screen
after booting a disk.  Now, about CHARGET and CHARGOT.  They start at
$B1 and I can't remember exactly what CHARGOT does, but who cares?
CHARGET is only used from a running AppleSloth program.  What it does is
get the next character in the PROGRAM!, not from the keyboard.  A good
place to get a sidewise view of it is in "Now That You Know Assembly
Language, What Can You Do With It?", which was recommender here a few
weeks ago.  It really is worth buying (though it did take a while for me
to buy it).  Anyway, this sets a pointer called something like EVAL or
somesuch to the next byte of the code.  CHARGET is called by Applesoft
when it wants the next token.  This is handy, as when you use the amper
routine, you can call CHARGET to point to what's after the &, and if
it's, say, in quotes, print it.  Which brings me to printing from
AppleSloth. When you say 10 PRINT "HELLO" AppleSoft simply calls COUT to
print what's in the quotes.  Hence, if you have ^M^D imbeded in the
quotes, DOS will see it.

Another fun thing to do is this:

10 PR#1
20 call 1002

This is the same thing as changing the output hooks, and reconnecting
DOS (1002 = $3EA).  Sometimes it's handy to have DOS turned off from a
program.  PR#1 PR#0 will do it.  THen before your routine exit, it can
call 1002.  Three invaluable things to have when you start mucking
around with this stuff.  Beneath Apple DOS (Worth and Lechner),
DOSSOURCE (unknown.  It's DOS Disassmbled for Lisa, completely
commented) and , well, MERLIN assembler has a routine to generate a
commented listing of BASIC.  A very handy thing to have.  Also of
mention, the Apple Reference Manual (with source code), the Apple
Gazetteer (I TOLD you I can't spell!) and Atlas, and every useful book
on assembly language, like "Now That You Know Assembley..."

Any specific questions, write me.  I now have the time to think about
these things...

Sean Kamath

ihnp4,decvax,ucbcad!tektronix!reed!kamath

p.s.  I hope I haven't confused things even more!