-
Notifications
You must be signed in to change notification settings - Fork 5
Instruction Set
There are instructions to do simple arithmetic.
-
add output input1 input2
: addinput1
andinput2
and store result inoutput
-
sub output input1 input2
: subtractinput2
frominput1
and store result inoutput
-
mul output input1 input2
: multiplyinput1
andinput2
and store result inoutput
-
div output input1 input2
: divideinput1
byinput2
and store result inoutput
-
mod output input1 input2
: store the remainder of dividinginput1
byinput2
inoutput
-
inc input-output
: incrementinput-output
by 1 -
dec input-output
: decrementinput-output
by 1
The operations over floats are much the same in terms of functionality:
addf output input1 input2
subf output input1 input2
mulf output input1 input2
divf output input1 input2
modf output input1 input2
incf input-output
decf input-output
-
ftoi output input
: store the conversion ofinput
to an integer inoutput
-
itof output input
: store the conversion ofinput
to a float inoutput
Note: Division by zero in float instructions will yield NaN
if and only if the numerator is also zero. Otherwise, division by zero will result in an error.
Using any of the integer instructions on floats, or any float instructions on integers will cause undefined behavior. When programming, pay special attention to keep track of what registers contain which types.
Additionally, all non-conversion instructions listed in this section are guaranteed to produce a result in the same domain as their inputs when used as intended. This means that all operations over integers will produce an integer, and all operations over floats will produce a float.
ftoi
will result in an error if the input is NaN.
The LO
and HI
registers are updated only during the usage of the mul instruction. LO
will always store the same result as what is to be stored in output
. On overflow, HI
will store the most-significant half of the bits of the product, depending on the word size.
There are instructions to do simple bitwise arithmetic.
and output input1 input2
or output input1 input2
xor output input1 input2
not output input
-
sll output input1 shift
: "shift left logical" instruction -
srl output input1 shift
: "shift right logical" instruction -
sra output input1 shift
: "shift right arithmetic" instruction
Note: for all of the shift instructions, there are safeguards in place to prevent improper usage. Any negative shift or shift greater than the word size will default to a result of 0. In the case of sra
, these results will be sign-extended properly.
There are instructions to do with function calls.
-
j input
: jumps to the givenprogram counter
-
jump input
: jumps to the givenprogram counter
-
jal input
: stores the currentreturn address $ra
andfile identifier $fid
onto the stack and then jumps to the givenprogram counter
- increments the stack pointer by
2 * word size
- increments the stack pointer by
-
call input
: stores the currentreturn address$ra
andfile identifier $fid
onto the stack and then jumps to the givenprogram counter
- increments the stack pointer by
2 * word size
- increments the stack pointer by
-
return
: returns to the currently storedreturn address
and then pops the oldreturn address $ra
andfile identifier $fid
off of the stack -
exit input
: stores the input to$r0
and terminates the program -
import "relative/path/to/file"
: attempts to import code from the file at the given relative path- labels within imported files can only be accessed using
call
/jal
- looks for a file using absolute path if running anonymously
- produces an error if the file is unable to be read, contains invalid code, or contains a label which has already been defined
- labels within imported files can only be accessed using
Note: Jump instructions will throw an InvalidProgramCounterException when attempting to jump to an address outside the current program's code.
There are instructions to do with conditional branches.
-
blt input1 input2 input3
: jumps toinput3
ifinput1
is less thaninput2
-
ble input1 input2 input3
: jumps toinput3
ifinput1
is less than or equal toinput2
-
bgt input1 input2 input3
: jumps toinput3
ifinput1
is greater thaninput2
-
bge input1 input2 input3
: jumps toinput3
ifinput1
is greater than or equal toinput2
-
beq input1 input2 input3
: jumps toinput3
ifinput1
is equal toinput2
-
bne input1 input2 input3
: jumps toinput3
ifinput1
is not equal toinput2
There are instructions to do with conditional branches.
-
slt input1 input2 input3
: stores a1
ininput1
ifinput2
is less thaninput3
,0
otherwise -
sle input1 input2 input3
: stores a1
ininput1
ifinput2
is less than or equal toinput3
,0
otherwise -
sgt input1 input2 input3
: stores a1
ininput1
ifinput2
is greater thaninput3
,0
otherwise -
sge input1 input2 input3
: stores a1
ininput1
ifinput2
is greater than or equal toinput3
,0
otherwise -
seq input1 input2 input3
: stores a1
ininput1
ifinput2
is equal toinput3
,0
otherwise -
sne input1 input2 input3
: stores a1
ininput1
ifinput2
is not equal toinput3
,0
otherwise
Basic memory functionality.
-
push input
: stores the given value onto the stack and then decrements (grows) thestack pointer
-
pop input
: retrieves a value from the stack and then increments (shrinks) thestack pointer
-
load output input
: loads the value at the given address into the given output -
store output input
: stores the value given at the output address -
alloc output input
: allocatesinput
bytes and returns a pointer to the allocation -
move output input
: move the contents ofinput
tooutput
Note: Memory instructions will throw a SimulationAddressOutOfBoundsException when attempting to read or write to an address outside of the program's memory space. See: Structure
Terminal input and output.
-
printi input
: prints the given input to the terminal as an integer -
printf input
: prints the given input to the terminal as a float -
printc input
: prints the given input to the terminal as an ASCII character -
prints input1 input2
: prints up toinput2
characters of the given string address to the terminal until a null terminator is reached -
prints input1
: prints the given string address to the terminal until a null terminator is reached -
readi output
: reads the given input to the terminal as an integer -
readf output
: reads the given input to the terminal as a float -
readc output
: reads the given input to the terminal as an ASCII character -
reads input1 input2
: reads from the terminal a string of up toinput2
length into the address ofinput1
-
reads input1
: reads from the terminal a string into the address ofinput1
-
readln input1 input2
: reads from the terminal a string of up toinput2
length into the address ofinput1
until a newline character -
readln input1
: reads from the terminal a string into the address ofinput1
until a newline character