LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Assembler and Virtual Machine for a 16-bit URISC CPU

Root / Submissions / [.]

amihartCreated:
Download:Q33N4DHS
Version:Size:
The R313 is a (currently) imaginary URISC CPU created by me for educational purposes. URISC stands for "ultimate reduced instruction set", it is a computer with only one instruction. Since it only has one instruction, you do not even need to store the instruction in memory, only the operand of it. The R313 is based on the reverse-subtract and skip-on-borrow (RSSB) instruction (view the README for how this works). It has one operand and is Turing complete. This program contains two programs called "RASM" and "RVM". RASM is the assembler. Included is a "Hello, World!" assembly program to help you understand how it works. RVM is the virtual machine. It will run your code. Please note that I had designed this CPU architecture a long time ago for educational purposes. Coding for this CPU is very difficult and rather impractical. However the virtual machine and assembler that come with this are both fully documented to serve as a guide for assembler and emulation creation. This was originally written in C and then ported to SmileBASIC. You can check out the assembly "Hello, World!" code on PC here Update 1:
  • Bug fixes.
  • Modified the text input to act like it does on Windows/Unix.
  • Added another example file. It asks you for your name, lets you input a name (maximum 3 characters), and then says, "Hello, {name}!"

I believe that the FIX16 function can be replaced with N<<16>>16. Additionally, single characters of a string can be accessed using square brackets rather than MID$. Also I think your sorting function could be written as something like:
DIM LENS[LEN(STN$)]
FOR I=0 TO LEN(STN$)-1
 LENS[I]=LEN(STN$[I])
NEXT
SORT LENS,STN$,STV

Replying to:12Me21
I believe that the FIX16 function can be replaced with N<<16>>16. Additionally, single characters of a string can be accessed using square brackets rather than MID$. Also I think your sorting function could be written as something like:
DIM LENS[LEN(STN$)]
FOR I=0 TO LEN(STN$)-1
 LENS[I]=LEN(STN$[I])
NEXT
SORT LENS,STN$,STV
Oh, a few more things. SB has logical and/or operators (the standard && ||), and you can compare strings using < >= etc.

Replying to:12Me21
I believe that the FIX16 function can be replaced with N<<16>>16. Additionally, single characters of a string can be accessed using square brackets rather than MID$. Also I think your sorting function could be written as something like:
DIM LENS[LEN(STN$)]
FOR I=0 TO LEN(STN$)-1
 LENS[I]=LEN(STN$[I])
NEXT
SORT LENS,STN$,STV
Does using "&&" instead of "AND" have any effect on performance?

So basically rasm is a version of the MOVfuscator but in SB :P

Replying to:ninjagnu
So basically rasm is a version of the MOVfuscator but in SB :P
I've never heard of the MOVfuscator until now, but looking into it, they are definitely very different. The MOV instruction on an x86 machine just so happens to be Turing-complete in of itself. The MOVfuscator uses this to compile C code into native x86 code only using this single instruction. The differences are as follows:
  • The MOVfuscator compiles code specifically to run on an x86 chip natively. It does not contain a virtual machine as it is intended to run on your actual hardware.
  • The MOVfuscator uses "MOV" logic and not "RSSB" logic.
  • The MOVfuscator compiles from C code while this currently only compiles from assembly code. I don't believe the MOVfuscator contains an assembler because it's meant to be assembled using a native assembler.

Replying to:12Me21
I believe that the FIX16 function can be replaced with N<<16>>16. Additionally, single characters of a string can be accessed using square brackets rather than MID$. Also I think your sorting function could be written as something like:
DIM LENS[LEN(STN$)]
FOR I=0 TO LEN(STN$)-1
 LENS[I]=LEN(STN$[I])
NEXT
SORT LENS,STN$,STV
If the first value is false, it doesn't have to evaluate the second value. (And the opposite for ||)

Ah cool! I hope people learn stuff from this!

Replying to:12Me21
I believe that the FIX16 function can be replaced with N<<16>>16. Additionally, single characters of a string can be accessed using square brackets rather than MID$. Also I think your sorting function could be written as something like:
DIM LENS[LEN(STN$)]
FOR I=0 TO LEN(STN$)-1
 LENS[I]=LEN(STN$[I])
NEXT
SORT LENS,STN$,STV
It's just generally more sane to use the right operator for the job. Bitwise AND/OR have to do more work, but it's just more clear to use the logical/conditional operators to people reading your code. Two bonuses in one.

Why not use ' for comments like SB? It looks better in the editor. You could make it another option and leave in the "original" comment character for compatibility.

Replying to:snail_
Why not use ' for comments like SB? It looks better in the editor. You could make it another option and leave in the "original" comment character for compatibility.
That's just an annoying thing to deal with. The code wouldn't be compatible with other versions of the assembler, and plus, basically every assembly language uses ; for comments.

Replying to:snail_
Why not use ' for comments like SB? It looks better in the editor. You could make it another option and leave in the "original" comment character for compatibility.
I haven't looked too far into it, but I'm convinced with a few basic tweaks, you can implement 90% (or more) of an assembler in SB using just a COMMON DEF library. You'd be limited in features and syntax, I guess, but it's a possibility because many assembly languages are so simple. Using ' for comments is one step towards this.

Why not get rid of RSSB prefix before things? If all you need is the arguments?

Replying to:ninjagnu
Why not get rid of RSSB prefix before things? If all you need is the arguments?
Looks nicer that way imo.

Awesome work, thanks for this! Now lets just hope I can actually understand it and learn a thing or two.

Some things remain unclear, which make this very difficult to program. For example, does the CPU handle negative numbers, or only 0-65535? If it does what format does it use? Two’s complement?