Programming Your Homemade Computer

Homepage

One of the issues I certainly have, and I'm sure others do as well, is that once you've built your computer you then have to program it. This usually means writing in assembler or machine code, and since almost all homemade computers will be using some form of RISC architecture, a lot of clever little bit manipulations are required to do even basic functions. However, the search for many of these algorithms has so far been fairly fruitless, so I am compiling a list of useful functions I already know on one page to hopefully help anyone else struggling with assembly programs (These should also apply to any other assembler based languages for things like PIC chips). If you know any other functions that would be useful for fellow assembly programmers, or can improve the ones already shown, then drop me a line and I'll add it to the list.

Obviously, all instruction sets differ in format and complexity, so I will start with the very simplest functions, and then work up. The more complex functions may use ones that were shown previously, so they will need to be substituted in where your computer requires.

Arbitary registers will be defined as A,B,C,D, etc. All movement between them is assumed and will have to be worked out to suit your machine. The word length will also be assumed as 8 bits, but it shouldn't make much difference to the functions. I've tried to minimize register usage and instructions where possible, so watch out for where registers are overwritten.

Below are some very basic functions required for single ALU command machines. I'm going for NOR and NAND as they're the simplest universal operators. They're all fairly simple, but I'm writing them down anyway.

 

The NAND and NOR logic are derived from these pages: NAND, NOR

NAND and NOR logic

 

A=NOT A

C=A OR B

C=A AND B

C=A EXOR B

NOR

A=A NOR A
C=A NOR B
C=C NOR C
A=A NOR A
B=B NOR B
C=A NOR B
D=A NOR B
A=A NOR A
B=B NOR B
C=A NOR B
C=C NOR D

NAND

A=A NAND A
A=A NAND A
B=B NAND B
C=A NAND B
C=A NAND B
C=C NAND C
C=A NAND B
A=A NAND C
B=B NAND C
C=A NAND B
 

Basic Functions

Most architectures will contain these functions, but if not here's how they can be achieved.

C=A ADD B (D AS CARRY)

C=A SUBTRACT B

A=Shift Left A

A=Shift Right A
C=A EXOR B
E=D AND C
C=C EXOR D
D=A AND B
D=D OR E
B=NOT B
CARRY=1
C=A ADD B
A=A ADD A
A=Shift Left A 8 times
(If anyone knows of a better way to do this one let me know)

Masking Operations

Masking is using a constant and basic functions to select and manipulate individual bits in a word. I will show these as examples as its easier to see what's happening (These can be used to select more than one bit at a time, but for the sake of simplicity most of these are shown operating on a single bit).

 

Select Bits

Selective Bit Clearing

Selective Bit Setting

Selective Bit Inversion

01101011
AND
00001000
00001000
01101011
AND
11110111
01100011
01101011
OR
00010000
01111011
01101011
EXOR
00100100
01001111

Being able to select single bits is useful for testing for if it is zero, as the zero flag (assuming your computer has one) will be set by the single bit in the accumulator. Other useful tests are shown here:

IF A=B

IF A<B

C=A SUBTRACT B
If C=0 then A equals B
C=A SUBTRACT B
If C is negative then A<B

This list is not nearly exhaustive, and any input would be greatly appreciated! (email)

 

(Much of the information on this page was found here.)