Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- def ask_type
- t = ""
- loop do
- STDERR.print "Choose a format {R,I,J}[R]: "
- t = STDIN.gets.chomp
- if "RIJrij".include?(t)
- break
- else
- STDERR.puts "That's not recognized as a format"
- end
- end
- if( t.empty? )
- t = "r"
- end
- STDERR.puts "Format chosen: #{t}"
- t.downcase
- end
- def ask_op(prompt,min,max)
- #STDERR.puts "You can either supply one in decimal or hexadecimal format."
- op = 0
- loop do
- STDERR.print prompt
- op = STDIN.gets.chomp.to_i(16)
- if op > max
- STDERR.puts "That's larger than allowed. Maximum value 0x#{max.to_s(16)}."
- elsif op < min
- STDERR.puts "That's less than allowed. Minimum value 0x#{min.to_s(16)}."
- else
- break
- end
- end
- op
- end
- STDERR.puts <<eoh
- This is a small script for creating properly formated MIPS-instructions.
- You will be asked a number of questions and then rewarded with a 4-byte
- instruction in hexadecimal and decimal format. Have fun!
- eoh
- loop do
- t = ask_type
- op = ask_op(" Choose an op-code {0x00-0x3f}[0x00]: ",0x00,0x3f)
- fu = ask_op(" Choose a function-code {0x00-0x3f}[0x00]: ",0x00,0x3f) if t == 'r'
- rs = ask_op(" Choose rs-register {0x00-0x1f}[0x00]: ",0x00,0x1f) if t != 'j'
- rt = ask_op(" Choose rt-register {0x00-0x1f}[0x00]: ",0x00,0x1f) if t != 'j'
- ta = ask_op("Choose target-address {0x0-0x2000000}[0x00]: ",0x00,0x2000000) if t == 'j'
- im = ask_op(" Choose immediate value {0x0-0x8000}[0x00]: ",0x00,0x8000) if t == 'i'
- rd = ask_op(" Choose rd-register {0x00-0x1f}[0x00]: ",0x00,0x1f) if t == 'r'
- sh = ask_op(" Choose shift-amount {0x00-0x1f}[0x00]: ",0x00,0x1f) if t == 'r'
- case
- when 'j' == t
- instruction = (op<<26) + ta
- when 'i' == t
- instruction = (op<<26) + (rs<<21) + (rt<<16) + im
- when 'r' == t
- instruction = (op<<26) + (rs<<21) + (rt<<16) + (rd<<11) + (sh<<6) + fu
- end
- STDOUT.printf( "instruction:\t0x%08x\t%d\n", instruction,instruction)
- end
Add Comment
Please, Sign In to add comment