Beginning ML - Part One
(C) 1992 by Craig Taylor

  The best way to learn machine language is to actually code routines that you
don't think will work, hope that they work, and then figure out why they don't
work. (If they do work, you try to figure out why you didn't think they'd
work).  Ie: THE BEST WAY TO LEARN ANY PROGRAMMING LANGUAGE IS TO PROGRAM IN
THAT LANGUAGE.  And Machine Language is a programming language.
  Now, let's get a few terms and definitions out of the way:

  Machine Language - Instructions that the computer understands at a primitive
     level and executes accordingly.
  Assembly Language - Instructions more understandable to humans than pure
     Machine Language that makes life easier.

                      Assembly:			Machine:
	Example:	lda #$00                  $A9 $00

  Huh? you might be saying at the moment.  Turns out that LDA stands for, or is
a mnemonic (computer people always come up with these big long words -- you'll
see mnemonic's often when dealing with machine language) for the following:
    "LOAD register A with the following value"
     ^  ^          ^
  Cool 'eh? Yeah, but there's somebody grumbling now about why not make it
LOADA etc.. Hey, that's life. (GRIN).
  
  Oh, more definitions:

  Register - A location inside the CPU that can be manipulated directly without
     having to access memory.

  The "A" register is often called the accumalator which indicates its
function: all math and logical manipulations are done to the "A" register (from
hereon out it will be referred to as .A).
  There are two other registers inside the 6502 processor, specifically .X and
.Y.  These registers help act as counters and indexes into memory (sorta like
mem[x] in pascal but not quite...).

  Now, let's add 3 and 5 and leave the result in the accumalator (.A).

		lda	#3 		; Here .A = 3 (anything w/ a ; is a
   ; comment and will be ignored by the assembler... 
		clc		; hu? - This clears the carry. The 6502
  ; does addition *everytime* with the carry ... so if we clear it it won't
  ; affect the result.
		adc	#5		; Now, .A = .A + 5

  and we're done.  If the CLC confused you then consider that if you're adding
a column of #'s:
               
       12 <--\__The 2 + 9 = 11, but we put the 1 down and set the carry to 1.
    +  89 <---/ 
       --
      101
   Then we say 1 + 8 + carry , which in this case happens to = 1 and we get 10
and again we set the carry and write down 0. Then it's just the carry and we
write that down.  If we didn't clear the carry we may have ended up with the
value of 9 instead 8 if the carry had happened to be set.
  
  Aaagh, Math - Let's continue - The CLC mnemonic stands for "CLEAR CARRY" and
the ADC stands for "ADD with CARRY".  On many processors there is a ADD
(without a carry) but unfortunately the 6502 processor inside the C=64 doesn't
have it.
 So we've got:
    load reg A with the value 5     		lda #5
    clear the carry				clc
    add reg a and value 3			adc #3

 In Basic it's just: 
    A = 5+3 

  One statement... In Machine Language you've got to break everything down into
smaller and smaller steps and quite often the ML listing will be far longer
than the BASIC or PASCAL or C equivlent.

 Definitions:
   Assembler - Program takes source code in basic form or from a file and
writes to memory or a file the resulting executable. Allows higher flexibility
than a monitor (see below) due to use of labels etc and not having to keep
track of each address within the program.

  Monitor - A program, resident in memory, invoked by a sys call from basic or
by hitting the restore key that will let you disassemble, assemble and examine
areas of memory and execute programs directly from the monitor. Useful for
debugging programs and for writing short programs.
  
  Let's enter the following into a monitor (if you don't have one then contact
duck@pembvax1.pembroke.edu and I'll send ya one):

128:			c64:
>a 1300 lda #$93     	>a c000 lda #$93
>a 1302 jsr $ffd2	>a c003 jsr $ffd2
>a 1305 rts		>a c005	rts
(exit monitor)		(exit monitor)
bank15:sys4864		sys 49152

  Wow! It cleared the screen. Neat 'eh?  But see how much ya gotta break
problems down?  The first statement loads in $93 hex into the accumalator ($93
hex just happens to equal 147 which is also the Commodorscii code for clear
screen.  For a whole list just look in the back of the book that came with the
computer).  Then we jump to a system routine which Commodore so graciously
supplied us with that prints the value of the character in .A to the screen.
(jsr $ffd2) then we do a RTS (ReTurn from Subroutine) so that we will go back
to basic and the Ready prompt when we are finished with the sys call.
  You C= 128 people may be wondering why you had to do a bank 15 and assemble
the stuff at a different memory location.  Turns out that the C128 memory map
of where routines etc are at is much more complex than the C=64 and thus you
have to tell basic which bank you wish to have all sys, peek, and poke calls to
take place in. Also, $c000 as used on the C=64 is not an area that is free to
use on the C128 in this manner.

  Assignment: Take a look @ the different commands as listed in 6502 Opcodes
and try to understand what they do. Experiment with the jsr $ffd2 routine by
using different values etc.

