Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. # MIPS Repl written in MIPS
  2. # Run with MarsIDE (remember to turn on `Settings` —> `Self-modifying code`)
  3. # The input instruction must be converted to decimal
  4.  
  5. .data
  6. prompt_string: .asciiz "Enter the next instruction to execute: "
  7.  
  8. .text
  9. main: li $v0 9 # code to request memory
  10. li $a0 16 # enough for 2 instructions + 2 registers ($v0, $a0)
  11. syscall # $v0 now contains adress of newly allocated memory
  12.  
  13. # memory layout: registers first, then the input instruction, then the jump instruction.
  14. move $s0 $v0 # $s0 points at the begining of the allocated memory
  15. addi $s1 $s0 8 # $s1 points where the input instruction will be
  16. addi $s2 $s1 4 # $s2 points where the jump instruction will be
  17. la $s3 save_state # $s3 points to save_state
  18.  
  19. # let's write the jump instruction first
  20. li $t0 0x02600008 # $t0 = "jr $s3"
  21. sw $t0 ($s2) # mem[jump] = "jr $s3"
  22.  
  23. prompt: # ask for an instruction
  24. li $v0 4 # system call code to print a string
  25. la $a0 prompt_string # "Enter the next instruction to execute: "
  26. syscall
  27.  
  28. # read instruction
  29. li $v0 5 # system call code to read an int
  30. syscall
  31. sw $v0 ($s1) # load to new instruction in memory
  32.  
  33. # load registers from previously saved state
  34. lw $v0 ($s0)
  35. lw $a0 4($s0)
  36.  
  37. jr $s1 # execute instruction
  38.  
  39. save_state: # save registers' state
  40. sw $v0 ($s0)
  41. sw $a0 4($s0)
  42.  
  43. j prompt
  44. # End of program
  45.  
  46. # Example usage:
  47. # > Enter the next instruction to execute: 537133098
  48. # > Enter the next instruction to execute: 537001985
  49. # > Enter the next instruction to execute: 12
  50. # > 42
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement