Copy Link
Add to Bookmark
Report

The Hex Files part 2

eZine's profile picture
Published in 
The Hex Files
 · 5 years ago

  

Ferris The Hex Files
Programmer at work - part 2 by Ferris

Hello all and welcome to the second part of our course for
budding programmers. Last issue we introduced a few of the
commands our C64 can understand and this issue I intend to
bring a few more in to play. LDA and LDX have both been
covered independently last issue, but they can work together.
Lets get things off to a start with the first example:

LDX #$00 Set the X register to zero.
LDA #$01 Set the A register as well.
STA $0400,X Store A to $0400+X.
RTS Exit.

If we were to run this a letter A would appear at the top
left corner of our screen. Nothing exiting so far eh? Well,
if we were to alter the first command to read LDX #$01 and
re-run it the results would be slightly different in that the
A would now be one character to the right. So how does this
actually work you ask. Well, the first command sets the value
in the X register and the second sets the accumulator (A
register) in the same way as before. But the STA command has
been altered slightly so that instead of just putting the
contents of A to a set place (in this case memory location
1,024) it adds the value in X to the location. So if X is 0
then the A character appears at 1,024 and if it were 40 then
the A will be one line down because there are forty
characters a line.

This is the basics of loops in machine code, but before we
can introduce them properly we need to first look at another
facet of machine code. When writing code it is necessary to
leap back and forth through the program in the same way that
BASIC can with GOTO's and GOSUB's. Because we have no line
numbers we need some way of identifying a piece of code to
jump to which is what labels do. A label is not actually a
command its a word, but it's like naming a piece of your
program so that you can then refer to it by that name.
Classic examples are calling a loop LOOP or your control
routine READSTICK (for reading the joystick) but just about
any word that doesn't have an instruction in can be used.
Labels do have other uses which we will cover later but for
now I'll leave this definition.

Time for another example I think, this time showing the use
of a label and our next two commands. This next example
would, if executed, put eight A's at the top left of the
screen:

LDX #$00 Reset X.
LDA #$01 Put 1 in A.
LOOP STA $0400,X Our first label!
INX INcrement X (as before).
CPX #$08 ComPare X to see if its 8.
BNE LOOP Branch if Not Equal to "loop".
RTS Stop.

Why have I confused you and introduced two new commands
together? Well, lets cover each command in turn. First, CPX
is short for ComPare X and its job is to compare things to
the X register (obvious, eh?) In the example above we are
checking to see if X has the value of #$08 but if we wanted
to put say nine A's we merely change the command to CPX #$09.
But when we have done this comparison we need to then do
something with the results. And this is where Branch if Not
Equal, or BNE comes in.

The basic flow of this loop goes like thus. We put a zero in
X and a one in A (for character 1, which is the A we would
see on screen). Then we hit the main loop which puts the
contents of A into location $0400 + X, which is $0400 since X
is zero. Then we add one to X and compare it to eight to see
if it's reached the end, if not we branch back to LOOP and
put another character down at $0400 + X (which is now $0401
since X is 1). This continues until X does reach eight. When
this happens the BNE is ignored (after all, it is equal now)
and we fall through to the end.

Some of you may be a bit confused by this logic, thinking
that since we stop when X reaches eight then surely the
eighth character wouldn't appear on screen because the loop
would stop, but it makes more sense when you remember that we
are counting from zero and not one.

So far we have been examining routines purely in theory but
I'm sure we are getting to the point where all of you want to
start seeing results from your code. To this end we have
included Turbo Assembler on the download page for you to play
with. Emulator users please note that,due to a problem with
the emulator, C64S cannot run this program. To start you all
off I'll just give you the basics of starting it up and we
will enter the previous example into Turbo and run it to show
it working. First off you need to load the assembler (the
filename is "TASM V5.6X /MIC") and when it's done type
SYS36864. Two lines of text appear at the bottom of the
screen giving the version number and statistics.

Because we are now moving into the "real" world of
programming the listing is slightly different. First tap
return once and the cursor will move to its start position.
Now type this:

*= $0900
LDX #$00
LDA #$01
LOOP STA $0400,X
INX
CPX #$08
BNE LOOP
RTS

The difference is the *= $0900 at the top. This is a command
specific to Turbo Assembler to tell it where we want our code
to go (in this case $0900 in memory which is 2,304 in
decimal). Now we want to see our code going so first press
the back arrow key (at the top left of the keyboard) and the
cursor will vanish. Press the "3" key to assemble and if all
has gone well you will see a new page appear giving
information on our program. The last three lines should read:

errors: 0
press `s` to start
or other key to edit

and you can press the "S" key. And before you can even see
the "started" message on screen our little listing has put
eight "A" characters at the top left of the screen! To get
back into the editor type SYS36864 again.

Well, that's the second part over with but before you carry
on to the next part I'll give you with a little challenge.
Can you figure out how to change our example to put eleven
characters on the screen in the listing above and change them
from "A" to "C"? Why not play with the code and see what you
can come up with! If you have any questions about this
article or machine code, drop me an email and I'll see what I
can do!</BODY

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT