Guest User

Untitled

a guest
Jul 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. def ask_type
  4. t = ""
  5. loop do
  6. STDERR.print "Choose a format {R,I,J}[R]: "
  7. t = STDIN.gets.chomp
  8. if "RIJrij".include?(t)
  9. break
  10. else
  11. STDERR.puts "That's not recognized as a format"
  12. end
  13. end
  14. if( t.empty? )
  15. t = "r"
  16. end
  17. STDERR.puts "Format chosen: #{t}"
  18. t.downcase
  19. end
  20.  
  21. def ask_op(prompt,min,max)
  22. #STDERR.puts "You can either supply one in decimal or hexadecimal format."
  23. op = 0
  24. loop do
  25. STDERR.print prompt
  26. op = STDIN.gets.chomp.to_i(16)
  27. if op > max
  28. STDERR.puts "That's larger than allowed. Maximum value 0x#{max.to_s(16)}."
  29. elsif op < min
  30. STDERR.puts "That's less than allowed. Minimum value 0x#{min.to_s(16)}."
  31. else
  32. break
  33. end
  34. end
  35. op
  36. end
  37.  
  38.  
  39. STDERR.puts <<eoh
  40. This is a small script for creating properly formated MIPS-instructions.
  41. You will be asked a number of questions and then rewarded with a 4-byte
  42. instruction in hexadecimal and decimal format. Have fun!
  43.  
  44. eoh
  45.  
  46. loop do
  47. t = ask_type
  48.  
  49. op = ask_op(" Choose an op-code {0x00-0x3f}[0x00]: ",0x00,0x3f)
  50. fu = ask_op(" Choose a function-code {0x00-0x3f}[0x00]: ",0x00,0x3f) if t == 'r'
  51. rs = ask_op(" Choose rs-register {0x00-0x1f}[0x00]: ",0x00,0x1f) if t != 'j'
  52. rt = ask_op(" Choose rt-register {0x00-0x1f}[0x00]: ",0x00,0x1f) if t != 'j'
  53. ta = ask_op("Choose target-address {0x0-0x2000000}[0x00]: ",0x00,0x2000000) if t == 'j'
  54. im = ask_op(" Choose immediate value {0x0-0x8000}[0x00]: ",0x00,0x8000) if t == 'i'
  55. rd = ask_op(" Choose rd-register {0x00-0x1f}[0x00]: ",0x00,0x1f) if t == 'r'
  56. sh = ask_op(" Choose shift-amount {0x00-0x1f}[0x00]: ",0x00,0x1f) if t == 'r'
  57. case
  58. when 'j' == t
  59. instruction = (op<<26) + ta
  60. when 'i' == t
  61. instruction = (op<<26) + (rs<<21) + (rt<<16) + im
  62. when 'r' == t
  63. instruction = (op<<26) + (rs<<21) + (rt<<16) + (rd<<11) + (sh<<6) + fu
  64. end
  65.  
  66. STDOUT.printf( "instruction:\t0x%08x\t%d\n", instruction,instruction)
  67. end
Add Comment
Please, Sign In to add comment