This is an old revision of the document!
Table of Contents
SD-8516 Assembly Language
- This file contains lessons 1-7
- You can find lesson 8 and above at: SD-8516 Assembly Language Part II.
Introduction
Welcome to the SD-8516 Assembly Language Reference Manual! Inside here you will find detailed information and discussion about programming your new SD-8516.
Lesson 1 : Memory, Registers and Flags
- Lesson 1: “Memory, Registers and Flags”
- Time: 5 min
- Learn:
- Registers: A B X Y
- Flags: N Z C V
- Memory: Flat memory model
In general there are some things you should know and consider first when learning SD-8516 assembly language programming.
Fist, we use a flat memory model, which goes from $0 to $03FFFF. That's 4 banks of 64k.
Second, word registers are 16 bits. To access them as high byte or low byte use H and L. For example X is 16 bits, but XH is the high byte, and XL is the low byte. Similarly for every register, such as Y, YL is the low byte and YH is the high byte. For A, AH and AL, and so on.
Third, memory requires three bytes to address, so you can only address bank 0 if you use a 16 bit register. To address upper memory, include a bank byte. Like this: BLX, ELM, or GLD, etc. This means BL+X (BL is the bank byte and X is the address within that bank). Or if you use ELM it EL + M, and so on. Always remember that these are not independent registers, but if you modify EL or M it will modify ELM, and if you modify ELM it will modify EL and M as well.
Finally, the concept of flags. Flags are one bit status registers. Some operations modify flags. For example, if you load a zero, the zero flag will be set. if you ADD two numbers which don't fit in a word, the overflow or carry flags might be set. The flags will be explained more in detail in the lessons teaching the individual instructions and how they manage flags. For now know that there are four primary flags that are set via operations:
- N – Negative. If an operation produces a number that looks like a signed negative number, this flag will be set. Most of the time you can just ignore this.
- C – Carry flag. If you add two numbers and it doesn't fit in a word, the carry will be set and then added in on the next ADD (on an ADD-with carry operation). This makes it easy to chain additions of very large numbers by keeping track of the “carry the one” for you.
- V – Overflow flag. In general if there is more overflow than in carry, this is set
- Z – Zero flag. If an operation produces or sees a zero, this is set.
Why flags? During decision making, you can use flags to control the program flow. This will be discussed in lesson 5: Program flow.
For today, let's do a deep-dive on the registers, because there's a little more to them than merely being 16 bit words.
All About Registers
There are sixteen general purpose registers available for use> Here they are, with a short comment on name and purpose. Of course, since they're general purpose, there is nothing separating one register from another except convention. You can feel free to use this guide, or use them any way you like.
| REG | Name | Convention | Notes |
|---|---|---|---|
| A | Accumulator | Scratchpad for math operations, function calls, etc. | The accumulator – used in much the same way as A or AX on 6502/8086 style systems. |
| B | Assistant to the Accumulator | Secondary accumulator | This will often hold the results of functins called with A as a variable. Often used as the high byte, or bank pointer, combind with A (see: register AB) |
| X | Column Index Register | Intended to help map 2d memory and arrays, loops, etc. | Often used for example in cursor or pixel array helper functions |
| Y | Row Index Regisrer | Intended to act as a row or record indicator alongside X. | Can form the XY paired register with X. |
| C, I, J, K | Iterator Registers | C is often used for counting, but I, J, K are also used. Also see: CD, IJ and KT. Some people treat these (especially K, alongside T and TK) as temporary registers | |
| T | Temporary Register | There is a saying, if you are preserving T you're doing it wrong. Don't PUSH and POP T to protect it– use it locally and then ignore it. T is our favorite temporary register! | |
| M, D | Memory pointer and memory pointer Destination. | These are often used in pairings like ELM, ELD, etc, to point to memory locations. As such they are generally for immediate use only and could be used on their own as temporary registers. | ELM is EL as high-byte |
| E, F, G | Extra registers most often used as high bytes for 24-bit memory access ex. GLD, FLM, etc. but can also be used for general purpose (esp. G – use G for anything! And in that sense, F is the 'Free Register' – use it as you like!) | ||
| L | The Last True Register | If you really need another register, use this one. For emergency use only. | |
| Z | Z-index pointer | Often used as a third dimensional register for graphics or data processing. Ex. FLZ |
Byte Access
Each 16 bit register (such as A) may be accessed as the byte registers H and L. This means AH is the high byte of A, BH is the high byte of B, etc. while AL is the low byte of A, ZL is the low byte of Z, etc.
24-bit Register Pairing
The system uses register pairing, which simulates 24 and 32 bit registers, for certain limited operations. The allowed pairings are:
- B: BLA, BLY, BLZ and BLT.
- E: ELA, ELB, ELC, ELD, ELX, ELY, ELZ, ELI, ELJ, ELK, ELT, ELM
- F: FLA, FLB, FLC, FLD, FLX, FLY, FLZ, FLM,
- G: GLA, GLB, GLC, GLD, GLI, GLJ, GLK, GLT.
Suggested use is for pointer only since this requires the CPU to manually combine two registers and is therefore much slower than a usual 16 bit register access.
Examples:
- Source pointer ELM, Destination pointer ELD; if it's in an alternate bank, FLD.
- S/D pointers ELI, ELJ or ELX, ELY, ELZ, or across banks you could use ELX, FLY, BLZ if you insisted on XYZ.
- If you needed a second pair you could use FLA, GLB, FLM, GLD, etc.
Warning: B, X and BLX (for example) are not separate! You cannot store something in BLX, then modify B or X independently from destroying BLX. They are built from B and X dynamically by the CPU.
The convention of BLX is that BL is the high-byte. This opposite from 32 bit mode (see below):
32-bit register pairing
The system can simulate 32 bit operations by combining two registers together however be advised this is very slow as it requires the CPU to simulate operations across multiple registers. These otherwise operate like their 24 bit counterparts.
The only allowed pairs are: AB, CD, XY, IJ, TK, LZ, EF, GM.
WARNING: Modifying G or M will destroy GM, etc. as GM is directly made of G and M.
LDGM $12345678 is equivalent to LDG $5678 and LDM $1234. So AB for example uses B as the high-word. This is opposite the BLX convention which uses BL as the high-byte.
This is because during MUL operations overflow moves into the high byte, otherwise it stays in the original register. EX. MUL A, B moves into A but overflow goes into B.
Lesson 2 : Load-Store Architecture
- Lesson 2: “Load-Store Architecture”
- Time: 5 min
- Learn:
- Registers: A and B
- Opcodes: LDA, LDB, STA, STB.
- Addressing modes: Immediate mode (numbers). Memory mode (memory reference).
Let's dive in to the basic idea behind SD-8516 assembly language programming! If you've ever programmed before, it's similar but different to a high level language. It is similar because there are functions and commands that take operands, and it is different because the functions are very simple building blocks, and there are only a limited number of integer variables that you can use.
The first concept is “Load-Store Architecture”. The SD-8516 uses a load-store architecture. This means that data is read from and written to registers, operated on inside registers, and then written back out to memory. There are no memory to memory operations and registers can only be loaded and saved.
The commands to load and store are LD and ST (load and store) followed by the register and an operand. For example,
; * LDA means "load into A,"
; * LDB means "load into B".
LDA #56 ; Load the decimal number 56 into the variable A.
LDA [#56] ; Load the value in memory location #56 into the variable A.
That's it for load operations. You can load a variable with a number either from memory or from a number directly. You cannot load a variable from another variable. This is invalid:
LDA B ; this doesn't work.
Next let's look at store operations. Store operations only write to memory. You can't write to a number, that's impossible, and you can't write to another variable. That would violate the “load-store architecture”. Examples:
STA [$1000] ; Store the value in A at memory location $ (hex) 1000.
Hex 1000 in decimal is 4,096. You can use decimal numbers via the '#' prefix or hex numbers with the '$' prefix. If you want to use binary, use 0b00000001 (that's the number 1 in binary).
Now you know how to load and store information from memory to the variables!
Lesson 3: Operations
- Lesson 3: Operations
- Time: 5 min
- Learn:
- Registers: C and D
- Opcodes: ADD, SUB
Now, once you have access to information in the computer's memory, you need to be able to perform operations on that information. Some of the things you can do are: adding, subtracting, multiplying and dividing. Here are some examples of things you can do:
ADD A, B ; Adds A and B and stores the result in A.
ADD B, C ; Adds B and C and stores the result in B
As you can see, the ADD command has a source register and a destination register. The destination is first and the source is second. So ADD A, D means D will be added to A, and A will hold the result. All of the registers such as A, B, C, D can be used. However by convention we like to use A and B for simple math.
Anyways, you can also do these things:
SUB A, B ; Subtract A - B and store the result in A.
Lesson 4: Advanced Operations
- Lesson 4: Advanced Operations
- Time: 5 min
- Learn:
- 32-bit Register Pairing
- MUL and DIV
Some processors such as the venerable 6502 (6510, etc.) stop with ADD and SUB, but we have a more advanced 8516, so we can also MUL and DIV. However, MUL and DIV are special operations; observe:
MUL A, B ; multiply A and B and store the result in AB.
Storing the result in AB? What's that? The SD-8516 has a special 32 bit extended operation for multiplication. The result is stored in A, but if the result would not fit in a word, the extra information is in B and the overflow flag is set. For example. what is $FFFF times $FFFF? It obviously cannot fit in one word. However, the result ($FFFE0001) does fit into two words. So in this case, A would be $FFFE and B would be $0001. This kind of overflow allows muliplication of larger numbers. Before anyone says “Why not just check overflow”, it's because you can also multiply like this:
MUL AB, CD ; Multiply AB by CD and store in ABCD.
Now, there is no way to operate on a 64 bit number (ex. ABCD) however, the result will be stored there, for you to interpret. That's the power of the SD-8516, it can multiply quite nicely! If you wanted, you could extend 64 bit operations via software. It would be slow, but workable. “bigwords”?
DIV A, B ; Divide A by B and store the answer in A and the remainder in B
The special properties of DIV allow you to perform modulus for free, or, in a modulus operation you can get the DIV for free. You can also do things like:
DIV AB, CD ; Divide AB by CD and store in AB and modulus (remainder) in CD.
You can also divide 32 bit paired registers. The powerful MUL and DIV capabilities of the SD-8516 set it apart from other CPUs of the era.
Lesson 5: Flow Control (Branching)
- Lesson 5: Flow Control (Branching)
- Time: 10 min
- Learn: Assembler Labels, CMP, JZ, RET
Tying everything together, what do you think this program does?
LDA [$00]
LDB [$02]
CMP A, B
JZ @equal
not_equal:
LDC $01 ; error code #1
RET
equal:
LDC $00 ; no error
RET
The program loads the word (two bytes) at $00 ($00 and $01) into A, and the word at $02 ($02 and $03) into B. Then it compares them. If they are equal, the zero flag is set. Depending on this we set our return code, which here by convention is C. But it could be anything. We have thus demonstrated the ability to compare registers and make a decision on program contorl flow based on that comparison. This has applications everywhere, from making sure a cursor is within the limits of the screen, to testing if a character is uppercase or lowercase, and many, so many applications that we cannot list them here.
CMP is the fundamental flow control operation. Compare two registers and JZ if equal. Fall-through is the not-equal case. You could also use JNZ instead and fall-through the “is equal” case. Now you know how to control the flow of your programs!
How CMP affects flags
CMP works by doing a simple test:
CMP A, B ; We are doing A - B!
Yes that's right, it's doing A - B, but it isn't doing it to store the value in A. It's testing if the result is 0 or not. If the result is zero, it sets the zero flag; ZF = 1. If it's not equal, then it is either ABOVE or BELOW zero. Imagine CMP 5,5 versus CMP 5,10 versus CMP 10,5:
CMP 5, 5 ; 5 - 5 = 0. Aha, a zero! ZF = 1
CMP 5, 10 ; 5 - 10 = -5. No zero. ZF = 0
CMP 10, 5 ; 10 - 5 = 5. No zero. ZF = 0
So because it's equal, it produces a zero. Seeing the zero, the CPU sets the zero flag. Then you can control program flow by JZ (jump-if-zero) and JNZ (jump-if-not-zero).
But there is more! As you see above, there are actually three situations that can occurr. It can be equal, or it can be less than zero, or above zero. You will notice that if A is less than B, the number is negative – or, “less than”. And, if the number in A is greater than B, then A-B produces a positive number, which is “greater than” zero. So it means A is greater than zero! This is why it's called CMP or “compare”. It compares if A is greater than, equal to, or less than B. And, we can test that by looking at the carry flag. The rule is, if you need to “borrow”, you do not set carry.
CMP 5, 5 ; 5 - 5 = 0. No borrow --> carry is set: CF = 1
CMP 5, 10 ; 5 - 10 = -5. Yes borrow --> carry is NOT set: CF = 0
CMP 10, 5 ; 10 - 5 = 5. No borrow --> carry is set: CF = 1
Therefore, if carry is set, we know that A is less than B.
But wait! There's more!
CMP 5, 5 ; 5 - 5 = 0. Not negative. N flag not set.
CMP 5, 10 ; 5 - 10 = -5. Yes negative. N flag set!
CMP 10, 5 ; 10 - 5 = 5. Not negative. N flag NOT set!
So you can also use the N flag. So here is the situation:
- If ZF=1 then A and B are equal.
- If ZF = 0, then look at CF or NF
- If CF is set, A is greater than B.
- If NF is set, A is less than B.
There you go! You can do this now, to branch on each condition:
- JZ @A_equals_B
- JC @A_greater_than_B
- JN @A_less_than_B
This is the foundation of how an IF statement works, or the ternary operator in C.
Lesson 6: The Boring Lesson
- Lesson 6: The Boring Lesson
- Time: 5-10 min
- Learn: AND, OR, XOR, NOT
The problem with computer science is that sometimes you have to learn some very boring things and you might not understand why they are important until later. Please understand that this is lesson #6, a fundamental lesson, and even if you find it boring, it will all work out for the best – trust me bro.
AND
AND is a classic logic gate. When two signals are 1, it shows result 1. I.E. 1 and 1 is 1. If one of the signals is down (like, an actual electrical signal in a wire) then the result is zero. This is OFTEN but not always an analogy for a light switch. There is always power in your house (A is 1) but only when the switch is ON (=1) is the light on. So you need 1 power and 1 switch and when they are both ON, then the light is ON. If they are both off, then what happens? Nothing! Absolutely nothing! Watch:
LDA 1
LDB 1
AND A, B ; A now is 1 (1 and 1 is 1).
LDC 1
LDD 0
AND C, D ; C is now 0 (one of the switches is off).
LDE 0
LDF 1
AND E, F ; E is now 0 (one of the switches is off).
LDG 0
LDI 0
AND G, I ; G is now 0 (both switches are off).
And is often displayed as an easy to read table:
| AND | ||
| 0 | 1 | |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
The AND means “result 1, only when x AND y are 1”.
Binary
These types of operation are how we deal with binary numbers. Binary numbers will not be fully explained here, but they are known as “base-2” numbers – versus base-10 (one to ten) or base-16 (hexidecimal). If you are not familiar with binary numbers, please look them up somewhere (in an encyclopaedia, online or book form,) before continuing.
LDA 0b01000111 ; 7 in binary
LDD 0b00010110 ; 6 in binary
AND A, D ; A is now what? 0b00000110
The bits in A that were also set in D remain. The bits that weren't set, ain't. Why is this useful? If you're using bits to hold status, or you want to test the value of a bit, you can do this:
LDA 0b01000111 ; some status register
LDD 0b00000100 ; Test for bit 3
AND A, D ; A is now 0b00000100
JNZ @bit_3_is_set
JZ @bit_3_is_not_set
Since if bit 3 is not set, AND A, D produces a zero, you can branch flow control based on bits. So for example, if your CPU has a “someone pressed a key” flag, you can test for that and handle the keypress by testing if rhe bit is on. This is just like checking if A = 5. Except you're checking a bit instead of an integer.
Other commands that work in a similar way are OR, XOR, and NOT.
OR
OR works by saying “Set the bit if either A or B is set.” So it will be 1 unless both are zero. Thats useful for detecting thieves. If any one of the laser traps detect a thief, the alarm has to go off. Not all of them at once, but any one, anywhere, and the alarms go off! That's how OR works.
XOR
XOR is “Exclusive” or. This means if the bits are the same, it's 0, if they're DIFFERENT, it's 1. This can be used to perform some surprising tricks. But as long as you understand the basic principle…
- 0b00010001
- 0b00010010
- XOR
- 0b00000011
The bits that were the same are 0, the bits that are diffrent are 1. Please don't ask me why this is useful, i'm sure i'll remember why later. Ha.
NOT
Finally, NOT. Not inverts a number.
- 0b00000001 ; This is a 1.
- NOT
- 0b11111110; This is 254 in decimal or FE in hex. Commonly written as #254 or $FE in assembler convention. Or 0xFE. Or FEh.
Why is NOT useful? NOT gives you the negative version minus one. So to make a number negative. NOT it and add one. In the case of 1, this is FF. This means you had a zero, subtracted one, and it rolled over to FF. So FF is negative one! We will explain negative numbers later. For now, FF is 255. Not -1. But, well, that's what NOT is for.
The End of the Boring Lesson
If this lesson was confusing I'm sorry. The fact is you're not going to understand binary logic until later when you see it in action and see how it actually is used. For now, just try to remember the basic ideas. Or, failing that, just remember that there is an AND, and OR, an XOR, and a NOT. Everything else is based on those.
Lesson 7: The Exciting Lesson
- Lesson 7: The Exciting Lesson
- Time: 10 min
- Learn: shifts and rotates, PAB, PXY, UAB, UXY
Imagine you have some number such as 5, in binary: 0x00000101
- If you shift this number to the left, you will have 0x00001010.
- If you shift it to the right, you will have 0x00000010.
- If you rotate it to the left, in this case, it will be the same as a shift. but,
- If you rotate it to the right, in this case, you will get 0x10000010.
- Notice how the '1' on the right was 'rotated' over to the left.
The above commands are SHR, SHL, ROR and ROL.
There's another way to do this called SHRC, SHLC, RORC and ROLC. When you do this the bit that 'falls off' goes into the carry flag. Also, for RORC and ROLC, the carry flag's bit is rotated back in. Cycling things through carry (or not) allows you to do some interesting things. What things, well, a whole bunch of things! Far too many to list here, but I'll give you some ideas.
One, if you want to pack information into a small space, you can set the carry bit and RORC/ROLC to set the correct bit. For example, if you wanted to set only the fifth bit, you could do this:
LDA #0 SEC ; set the carry flag ROLC A ; 1st bit is set ROL A ; 2nd bit is set ROL A ; 3rd... ROL A ; 4th... ROL A ; bit is now moved into 5th position.
You can use the same pattern to “test” a bit, then use JC or JNC to branch code.
Another great way is a kind of cheap cypher; you can “ROL” a byte before writing it then “ROR” it back later. Unless someone knows what you've done, it could be difficult to figure out! This wouldn't stop a dedicated code-breaker, but it will confound almost everyone else.
You can also use this to pack or unpack nybbles. This is how 16 color graphics are stored in half the space – or how two decimal numbers can be encoded in a hexidecimal and read separately. For example?
LDAL $F5
SHR AL
SHR AL
SHR AL
SHR AL ; After four shifts, AL contains the "high nybble", i.e. $F.
LDAL $F5
SHL AL
SHL AL
SHL AL
SHL AL ; this clears the original top bits in AL,
SHR AL
SHR AL
SHR AL
SHR AL ; After four shifts back right, AL contains the "low nybble", i.e. $5.
I suppose the first and most common use of these instructions is to pack and unpack data into a byte.
Given that, there are two common instructions PAB and UAB (and PXY, UXY,) that pack and unpack nybbles for you.
LDAL $C7
UAB ; AL is now $7 and BL is now $C.
LDBL $0D
PAB ; AL is now $D7
Lesson 8 and above
Now you can go to SD-8516 Assembly Language Part II.
Appendix IV: Debugging Techniques
There are several ways you can debug programs in SDA assembly. If you look at the console, inserting SED will turn on trace debugging and you will in general be able to see what the CPU is executing (if, debugging has been turned on during compilation). Otherwise you may need to HALT at any particular position and read the registers yourself. However if you can do neither, or are looking for something a bit more helpful, recall the methods of PRINTing that allow you to print messages.
INT 05h IO_PUTNUM
One way is to use IO_PUTNUM from int05 (CAM/IL interrupt):
LDB #10 ; print a number in b (0-65535)
LDAH $63 ; IO_PUTNUM INT $05
INT 05h IO_PRINT_STR
LDBLX @hello_world
LDAH $66 ; IO_PRINT_STR
INT $05
LDAH $64 ; IO_NEWLINE
INT $05
RET
hello_world:
.bytes "Hello World!", 0
This will allow you to print a string.
INT 10h print string
The interface for the above is based on the KERNAL BIOS interface in Int 10h.
LDAH $26 ; AH=26h: Write string at cursor
LDBLX @hello_world
INT 0x10
The underlying function that this calls is write_string, but outside of kernal assembly you will not have access to this function. This is because the labels are not shared outside of the compilation; you will however have direct access to this function via INT 10h.
LDBLX @hello_world
CALL @write_string
CALL @carriage_return
CALL @linefeed
For user use, the assembler will place a #13 (CR, hex $0D) inside the string if you type \n. Therefore although there is no carriage return function, you can do that via the set cursor position call (INT 10h, AH=22h) and you can send a CRLF pair by calling INT 10h, AH=26h on a string containing a newline (#13 or 0x0D).
If you need to issue a newline on it's own you can store the cursor position, issue a CRLF at the bottom of the screen and then restore the cursor X position. Also, under the hood, IO_NEWLINE from int 05 calls @carriage_return and @linefeed.
INT 10h print char
LDAH 0x24 ; AH=24h: Write character at cursor (teletype)
LDAL 0x41 ; ascii 65 'A'
INT 0x10
The KERNAL BIOS also has functions to put characters on the screen in mode 1 (40×25 TTY). The first one is “print char”. It is accessible via INT 10h AH=24h as above.
INT 19h memdump
Let's say you want to examine memory; for example to print some data in memory. You can use INT 0x19:
LDAH #7 ; Memory dump function LDCL #2 ; Two rows (16 bytes) INT 0x19 ; System services library HALT
This looks something like:
000000: 00 00 00 00 00 00 00 00 | ........ 000008: 00 00 00 00 00 00 00 00 | ........
