Skip to content

Instruction Set

Nathan Paul edited this page Nov 7, 2023 · 25 revisions

Arithmetic Instructions

There are instructions to do simple arithmetic.

Over Integers:

  • add output input1 input2: add input1 and input2 and store result in output
  • sub output input1 input2: subtract input2 from input1 and store result in output
  • mul output input1 input2: multiply input1 and input2 and store result in output
  • div output input1 input2: divide input1 by input2 and store result in output
  • mod output input1 input2: store the remainder of dividing input1 by input2 in output
  • inc input-output: increment input-output by 1
  • dec input-output: decrement input-output by 1

Over Floats:

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

Conversion Instructions

  • ftoi output input: store the conversion of input to an integer in output
  • itof output input: store the conversion of input to a float in output

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.

Bitwise Instructions

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.

Function Instructions

There are instructions to do with function calls.

  • j input : jumps to the given program counter
  • jump input : jumps to the given program counter
  • jal input : stores the current return address $ra and file identifier $fid onto the stack and then jumps to the given program counter
    • increments the stack pointer by 2 * word size
  • call input : stores the current return address$ra and file identifier $fid onto the stack and then jumps to the given program counter
    • increments the stack pointer by 2 * word size
  • return : returns to the currently stored return address and then pops the old return address $ra and file 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

Note: Jump instructions will throw an InvalidProgramCounterException when attempting to jump to an address outside the current program's code.

Branching Instructions

There are instructions to do with conditional branches.

  • blt input1 input2 input3 : jumps to input3 if input1 is less than input2
  • ble input1 input2 input3 : jumps to input3 if input1 is less than or equal to input2
  • bgt input1 input2 input3 : jumps to input3 if input1 is greater than input2
  • bge input1 input2 input3 : jumps to input3 if input1 is greater than or equal to input2
  • beq input1 input2 input3 : jumps to input3 if input1 is equal to input2
  • bne input1 input2 input3 : jumps to input3 if input1 is not equal to input2

Comparison Instructions

There are instructions to do with conditional branches.

  • slt input1 input2 input3 : stores a 1 in input1 if input2 is less than input3, 0 otherwise
  • sle input1 input2 input3 : stores a 1 in input1 if input2 is less than or equal to input3, 0 otherwise
  • sgt input1 input2 input3 : stores a 1 in input1 if input2 is greater than input3, 0 otherwise
  • sge input1 input2 input3 : stores a 1 in input1 if input2 is greater than or equal to input3, 0 otherwise
  • seq input1 input2 input3 : stores a 1 in input1 if input2 is equal to input3, 0 otherwise
  • sne input1 input2 input3 : stores a 1 in input1 if input2 is not equal to input3, 0 otherwise

Memory Instructions

Basic memory functionality.

  • push input : stores the given value onto the stack and then decrements (grows) the stack pointer
  • pop input : retrieves a value from the stack and then increments (shrinks) the stack 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 : allocates input bytes and returns a pointer to the allocation
  • move output input : move the contents of input to output

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 Instructions

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 to input2 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 to input2 length into the address of input1

  • reads input1 : reads from the terminal a string into the address of input1

  • readln input1 input2 : reads from the terminal a string of up to input2 length into the address of input1 until a newline character

  • readln input1 : reads from the terminal a string into the address of input1 until a newline character

Clone this wiki locally