8-bit ATARI Assembly Language

Go to
The Atari 8-bit Web Ring

Convert machine language to BASIC strings


Relative JMPs and JSRs
If you ever have occasion to write relocatable code for use in machine
language subroutines for BASIC, or want to, below are some routines and
tools that might help.

Writing relocatable code in 6502 assembly language has always been
difficult.  As routines get larger the difficulties become greater.  The
JMP and JSR opcodes use absolute addresses which are not relocatable.
The branch is the only relocatable jump available on the 6502.  A branch
can only jump forward 127 bytes or backward 128 bytes.

The two files below address this problem.  The first one is RELJMP and
the second is RELJSR.  They both will jump up to 32,768 ($8000) bytes in
both directions.  I haven't tried them that far.  I have only tested
them with jumps of only a few bytes, so far .  They have both been tested
for relocatability.

RELJMP uses the RTS opcode as a software programable jump.  At runtime
(the time when the machine code is actually running), your code is
fixed in memory.  The only thing RELJMP needs to know is the starting
address of the code at runtime.

The RELJMP macro is placed in code like this: 


ORGADR is the origin of the program as it is assembled.  Place a label
at the start of your program, so that it has the same address as the
*= or ORG opcodes.

LABEL is the label you want to jump to.

RELSTART is location of the starting address at runtime.

At assembly RELJMP subtacts ORGADR from LABEL.  This is the displacement
to LABEL.  It then subtracts 1.  This is because RTS adds 1 to the
address.  To the resultant number it adds the runtime address.  The total
is the current address of of LABEL-1.  This number is now pushed into the 
stack.  RTS pulls the number from the stack, adds one to it then jumps to

ORGADR and LABEL are pretty straight forward, but where do you find
RELSTART.  You can get it from memory at $D4 or pass it directly from
BASIC.  When BASIC passes control to your code, it stores the runtime
starting address at $D4 and $D5 in the usual low-byte, high-byte format.
You can use $D4 as RELSTART.  If your program calls the floating point
package, the contents of $D4 and $D5 will be destroyed.

Another way to get RELSTART is to get it directly from BASIC.  The address
can be passed to your code through the stack by means of an ADR(YOURCODE$).
The basic code to accomplish this is:
Your program can pull the 2 byte address from the stack and store it in
memory.  A good place to store it is the floating point registers on page
zero.  The floating point registers run from $D4 to $FF.  The only problem
with $D4-$FF is that you can't use it if you call the Floating Point
Package.  Assembly language subroutines used with BASIC rarely have any
need to use the Floating Point Package, since it is easier to manipulate
floating Point numbers in BASIC.

Other places are the usual $CB through $CF, $600 through $6FF.  And I
think $100 and $101 are very safe.

RELJSR works the same way as RELJMP.  It calculates the return address
and pushes it into the stack before the jump address.
A Digression

A listing of RELJMP

A listing of RELJSR

The two files below contain identical contents.  The first file,
RELATIVE.ZIP, is in zip format.  It requires PKUNZIP or WINZIP to extract

The second file, RELATIVE.EXE, is a self-extracting file.  Running it
from the DOS prompt extracts RELJMP and RELJSR.
Relative JMP and JSR for BASIC Assembly Language subroutines - RELATIVE.ZIP
6,572 bytes

Relative JMP and JSR for BASIC Assembly Language subroutines - RELATIVE.EXE
22,342 bytes


List of the sites on the Atari 8-bit Computer WebRing
Best Electronics - Atari
Atari computers hardware, software, service, and video games at B & C ComputerVisions
Bob Puff's Computer Software Services
Video 61 & Atari Sales
Antic - The Atari Resource
A.N.A.L.O.G. Magazines
Classic Computer Magazine Ring
De Re Atari
--Home Page for the Link above
Atari System Reference manual
Mapping The Atari - Revised Edition by Ian Chadwick
6502 Op Code Chart
For everything 6502-ish, www.6502.org
The 6502 webring
The Vintage Computer Festival

The Official Darwin Awards My Favorite Website

This site is maintained (or not) by Greg Menard - mailto:turbots@sbcglobal.net

The Atari 8-bit Web Ring
This Atari 8-Bit Computer WebRing site is owned by Greg Menard.
[ Previous 5 Sites | Previous | Join | RandomSite | Next | Next 5 Sites ]