SoftCPU - Assembly Programming
mwilki7 (653)

Ever wanted to write an assembly program on repl?
Now you can do it with this program!

An 8 MB file is used as memory and a place for the instructions to be stored.
The stack grows from the larger address and moves towards the lower ones.
Unless you plan on using recursion or writing hundreds of thousands of instructions, you shouldn't have to worry about stack overflows.

Using this limited instruction set you can call/ret, push/pop, add and use the several other instructions listed:

Instruction Set:

    instr   arg1    arg2 - changes are stored in arg1

    halt                        stop the processor from executing
    add     a    ,   b          add a and b
    sub     a    ,   b          subtract a and b
    mult    a    ,   b          multiply a and b
    inc     a                   increment the value of a by 1
    dec     a                   decrement the value of a by 1
    and     a    ,   b          apply and operator to a and b
    or      a    ,   b          apply or operator to a and b
    xor     a    ,   b          apply xor operator to a and b
    cmp     a    ,   b          compare a and b, result stored in CP_FL
    call    i                   puts instruction pointer + 1 on stack, then jumps to address i
    ret                         resets instruction pointer to value on top of the stack
    push    a                   puts the value in a on the stack
    pop     a                   removes the value on the stack and puts it into a
    jmp     a                   sets instruction pointer to a
    je      a                   if CP_FL is zero, the instruction pointer is set to a
    jne     a                   if CP_FL is not zero, the instruction pointer is set to a
    jle     a                   if CP_FL is less than zero, the instruction pointer is set to a
    jge     a                   if CP_FL is greater than zero, the instruction pointer is set to a
    mov     a    ,   b          undefined, will do later
    irmov   a    ,   i          move the value of i to a (immediate to register move)
    rrmov   a    ,   b          move value of b to a (register to register move)
    mrmov   a    ,   i          move the value in the address of i to a (memory to register move)
    rmmov   a    ,   i          move the value of a to the address of i (register to memory move)
    immov   i1   ,   i2         move the value of i2 to the address of i1 (immediate to register move)
    mmmov   i1   ,   i2         move the value in the address i2 into the address of i1 (memory to memory move)

In order to run your own assembly programs fork/copy the repl and begin writing your assembly in the 'program' file. Console will output the 'call-stack' for the instruction pointer as well as the register file:

Registers:

    TEMP0
    TEMP1
    TEMP2
    TEMP3
    TEMP4

    USER0
    USER1
    USER2
    USER3
    USER4
    USER5
    USER6
    USER7
    USER8
    USER9
    USER10
    USER11
    USER12
    USER13 
    USER14
    USER15
    USER16
    USER17
    USER18
    USER19

    INSPTR  -   instruction pointer
    STKPTR  -   stack pointer
    BSEPTR  -   base pointer

Sample Assembly:

Here is an example of a while loop in assembly:

irmov USER0 0
irmov USER1 5
irmov USER3 1

add USER0 USER3
cmp USER0 USER1
jl 9

halt

Here is the C equivalent of the above assembly:

int user0 = 0;
int user1 = 5;
int user3 = 1;
user0 = user0 + user3;
while (user0 < user1)
{
    user0 = user0 + user3;
}

Using jmp,jl,jle,je,jne,jg,jge instructions:

You have to run the program at least once to see the correct line numbers so you know where you're jumping:

See the 'instruction manual' file in the repl for more details.

  • does not currently have a floating point unit
  • does not currently support signed integers
  • does not currently support comments in the assembly
You are viewing a single comment. View All