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.

4 comments:

  1. Here's the improved Hello World for RSSB. This code is 2 lines shorter and a few cycles faster thanks to the following improvements:

    * sum has been moved to the instruction which is always skipped

    * loopoff has been moved to an opcode which is conveniently the correct value

    * the accumulator is no longer set to 0 before loading characters

    Here's the code:

    ------------------------------
    loop rssb acc ; acc = character from ptr
    ptr rssb hello

    rssb out ; display character

    rssb zero ; acc = -acc

    sum rssb -1116 ; 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
    loopoff rssb one
    rssb ptr

    rssb acc ; jump to loop
    rssb loopoff
    rssb ip

    rssb 33+6 ; '!'
    rssb 100+6 ; 'd'
    rssb 108+6 ; 'l'
    rssb 114+6 ; 'r'
    rssb 111+6 ; 'o'
    rssb 87+6 ; 'W'
    rssb 32+6 ; ' '
    rssb 44+6 ; ','
    rssb 111+6 ; 'o'
    rssb 108+6 ; 'l'
    rssb 108+6 ; 'l'
    rssb 101+6 ; 'e'
    hello rssb 72 ; 'H'
    ------------------------------

    ReplyDelete
  2. Can anyone explain why there are words before the rssb instruction in some lines?

    ReplyDelete
  3. The words before the RSSB instructions are labels for the compiler. They "label" the memory address so the compiler will calculate where that memory address actually is when the program is built, and any time you reference it, it replaces that reference with the real memory address. Such as, the beginning of the program is labeled "loop" and this would sit in memory address 5. So any time "loop" is references later, it will be replaced with "5" by the compiler.

    I think this is kinda of an ugly and weird way to do labels, tbh, as it's not as straightforwards what they mean and make the line look cluttered.

    This is how they typically look (and the syntax my compiler expects):
    https://pastebin.com/raw/QwvTzpHh

    ReplyDelete
  4. Is this truly a URISC machine? I looked at the source code of a couple emulators as well as wrote my own to successfully compile and run this code. What I found is that this code only seems to work if "rssb out" is a special case of "rssb" that does not update the accumulator. Otherwise, the code doesn't work. If "rssb out" is a special case of "rssb", then it's not a true URISC computer, because the computer still has to decode the instruction and do two separate operations depending on what the instruction is.

    Someone can correct me if I'm wrong, but I'm pretty sure this is essentially a 2 instruction machine. You could still do "hello world" with a 1 instruction URISC machine, it just would look different from this. The machine wouldn't output the character in the accumulator, instead it would output the character inside of the memory address of OUT, since that could be done separate from the CPU itself and doesn't involve making a special case of "rssb out". It would also update the accumulator based on the memory address of OUT just like any call to RSSB would.

    ReplyDelete

Note: only a member of this blog may post a comment.