Last active
December 8, 2016 17:18
-
-
Save julien-h/32f1df33252425ee3a6f72ce2f5fec1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MIPS Repl written in MIPS | |
# Run with MarsIDE (remember to turn on `Settings` —> `Self-modifying code`) | |
# The input instruction must be converted to decimal | |
.data | |
prompt_string: .asciiz "Enter the next instruction to execute: " | |
.text | |
main: li $v0 9 # code to request memory | |
li $a0 16 # enough for 2 instructions + 2 registers ($v0, $a0) | |
syscall # $v0 now contains adress of newly allocated memory | |
# memory layout: registers first, then the input instruction, then the jump instruction. | |
move $s0 $v0 # $s0 points at the begining of the allocated memory | |
addi $s1 $s0 8 # $s1 points where the input instruction will be | |
addi $s2 $s1 4 # $s2 points where the jump instruction will be | |
la $s3 save_state # $s3 points to save_state | |
# let's write the jump instruction first | |
li $t0 0x02600008 # $t0 = "jr $s3" | |
sw $t0 ($s2) # mem[jump] = "jr $s3" | |
prompt: # ask for an instruction | |
li $v0 4 # system call code to print a string | |
la $a0 prompt_string # "Enter the next instruction to execute: " | |
syscall | |
# read instruction | |
li $v0 5 # system call code to read an int | |
syscall | |
sw $v0 ($s1) # load to new instruction in memory | |
# load registers from previously saved state | |
lw $v0 ($s0) | |
lw $a0 4($s0) | |
jr $s1 # execute instruction | |
save_state: # save registers' state | |
sw $v0 ($s0) | |
sw $a0 4($s0) | |
j prompt | |
# End of program | |
# Example usage: | |
# > Enter the next instruction to execute: 537133098 | |
# > Enter the next instruction to execute: 537001985 | |
# > Enter the next instruction to execute: 12 | |
# > 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment