Sunday, 18 January 2009

Hello World for the RSSB Virtual Computer

The RSSB virtual computer has a single instruction, reverse subtract and skip if borrow. Each instruction has one operand, a pointer into memory. RSSB subtracts the accumulator from the contents of memory and the result is stored in both. If the accumulator was greater than the number in memory, the next instruction is skipped.

Jumps can be implemented by manipulating the instruction pointer at memory location 0, which normally requires 4 instructions. A conditional jump requires 6 instructions. Other special locations are as follows:
  1. accumulator
  2. always contains 0
  3. character input
  4. character output
The four lines of code below demonstrate the shortest jump:
        rssb   acc       ; set acc to 0
rssb $+2 ; set acc to loop offset
rssb ip ; subtract acc from ip
rssb $-loop ; the loop offset
The code below implements hello world for the RSSB virtual computer. The sum deserves an explanation. Each character is subtracted from sum until sum passes zero, indicating all character have been printed. The final value of sum is the offset required by the conditional jump!
loop    rssb   acc       ; acc = character from ptr
ptr rssb hello

rssb out ; display character

rssb zero ; acc = -acc

rssb zero ; always skipped

rssb sum ; subtract acc from sum

rssb ip ; skipped if sum is <0
; otherwise jump to 0

one rssb acc ; subtract 1 from ptr
rssb one
rssb ptr

rssb acc ; jump to loop
rssb loopoff
rssb ip
loopoff rssb $-loop

sum rssb -1116

rssb 33 ; '!'
rssb 100 ; 'd'
rssb 108 ; 'l'
rssb 114 ; 'r'
rssb 111 ; 'o'
rssb 87 ; 'W'
rssb 32 ; ' '
rssb 44 ; ','
rssb 111 ; 'o'
rssb 108 ; 'l'
rssb 108 ; 'l'
rssb 101 ; 'e'
hello rssb 72 ; 'H'
If you can improve the code above, or you've seen any other programs inplemented with RSSB, please leave a message below.

Thursday, 15 January 2009

The Ultimate RISC, One Instruction Set Computers

A One Instruction Set Computer is a virtual computer designed to use only one instruction. There are two common methods to implement a OISC:

Reverse Subtract and Skip if Borrow

The instruction has one operand which points to a memory location. The program counter is stored at location 0 and the accumulator at location 1. Location 2 always contains zero, location 3 is used for input and location 4 for output.

RSSB subtracts the accumulator from the contents of a memory location, storing the result in both.  If the accumulator was greater than the memory location, the next instruction will be skipped. Jumps can be implemented by manipulating location 0.

Subtract and Branch if Negative

SBN requires three operands, a, b and c.  The contents of memory location a is subtracted from the contents of b and the result is stored in b. If the value originally stored in a was greater than the value in b, SBN will jump to c. A variation on the theme is Subtract and Branch unless Positive.

I'll publish my implementation in Redcode shortly. In the meantime, why not take a look at projects listed below. If you're aware of any other implementations, please leave a comment with the details.