Re: LZ: On how to...


[Prev][Next][Index][Thread]

Re: LZ: On how to...



<SNIP>
> > As for what is assembly based on, its pretty much the same
> > as basic.  Basic, and all programming languages, borrow ideas
> > like loops and gotos and labels (variables, etc) from assembly.
> > What basic does is handle a lot of the details for you.
Although you have to make the loops yourself.
<SNIP>


  The most-used instruction is LD, which is kind of like let x = y or 
something.  Then you've got JP &amp; JR (goto) and CALL (gosub), and a few 
more like PUSH and POP.  But I think what you really want to know is:


  WHAT IS ALL THIS STUFF???


  So, here's a few low-level programming concepts:


Memory:
  Your program lives in it, you use it for everything.  Four digits of 
hexadecimal (two bytes) make up an address, like "$8C09".  The low 
addresses are in the ROM, the high ones are in the RAM.
  And since the machine can only address up to $FFFF, it uses RAM paging.
This is easier than it sounds - it just makes the address such-and-such
actually refer to the same address in some other bunch of memory 
somewhere, and you can swap different blocks in and out. You probably won't 
even need to worry about it.


Pointers (a.k.a. indirect adressing):
  A concept you're probably familiar with if you know C or Pascal, but if 
you've henceforth programmed only in BASIC, it could need some explanation.
  A pointer is simply an address in memory.  So if you had something 
stored at location $AAAA (call it x), and you had something somewhere 
(say, at $BBBB) that had a value of $AAAA, $BBBB would be a pointer to $AAAA.
  In assembly, if you wanted to refer to the value of $AAAA, you might 
say "($AAAA)" to indicate that you mean $AAAA is an address, not a value.




The program counter:
  This is just a register in the CPU that has as a value the address of 
the instruction it's handling right now.  Don't worry about it.




The accumulator:
  Better known as the A register.  You do all your math in this.  Where 
in BASIC or C you would type "x = y + 5", in assembler you have to 
actually carry out all that by hand.  For example, you would do:


	LD A, 5   ; to initialize the accumulator to 5
	ADD A, Y  ; to add Y to the accumulator
	LD X, A   ; to put the contents of the accumulator into x


  Actually, you couldn't do that, because X and Y are at locations in 
memory.  If X was the register B and Y was C, the code would be:


	LD A, 5
	ADD A, C
	LD B, A


  Or if X was in memory, at the location in your program that was the 
label, say, "myX" or somesuch, and y was "myY", you would say:


	LD A, 5
	ADD A, (myY)
	LD (myX), A


  This would be a better idea, because there's only a few registers and 
you should probably only use them for things you use a lot (because 
they're MUCH faster than memory).  You shouldn't ever leave anything in 
the registers if you're not using it, because then you'd have fewer left 
over to use.




The Stack:
  You probably won't use this much, so feel free to skip it,, but I'll 
explain it anyway.
  The analogy comuter science professors love is that the stack is like a 
big hat, and you can put something in it, and push it down, and the hat 
gets bigger and you can do it again.  Then when you want something back 
from it, you can pop it out.  Except you can only pop out the one that's 
on the top, and when you do, the hat shrinks again.  So you can only get 
out the last thing you put in.  You do all this with the PUSH and POP 
directives.
  Why is this useful?  You can use it for whatever you want, but the main 
use is for CALL directives.  Whenever you call a subroutine with the CALL 
directive, the program counter gets pushed onto the stack.  If you call a 
subroutine from within that one, the program counter there gets put onto 
the stack, too.  Then whenever you come back from a subroutine, the last 
address gets popped off the stack, and that's where you go back to.


  And how does it work?  The stack starts at a certain location in memory 
(I forget what), and there's a register in the CPU called SP, or Stack 
Pointer, that points to the "top of the stack", the end of the last thing 
you pushed onto it.  When you push something on, the stack pointer moves 
to the end of it, and when you pop it off, it goes back.






That's all for now.  Now go out there and write some games!
Dan Knapp


                     /***********************************
                     *           Daniel Knapp           *
                     *       FeatherWate Software       *
                     *     email: dankna@bergen.org     *
                     *                                  *
                     *      Whose account this is,      *
                     *         I think I know.          *
                     * The sysop is in Berkely, though. *
                     * She will not see me typing here, *
                     *        To watch this Mac         *
                     *          Refuse to go.           *
                     ***********************************/


References: