Skip to content

Instantly share code, notes, and snippets.

@yuvve
Created May 23, 2022 17:55
Show Gist options
  • Save yuvve/a87b1c39dbef0d6ec8690a80c725bae5 to your computer and use it in GitHub Desktop.
Save yuvve/a87b1c39dbef0d6ec8690a80c725bae5 to your computer and use it in GitHub Desktop.
Makes it easier to read machine code by splitting the bits depending on the MIPS instruction
def i_type(s):
op = s[0:6]
rs = s[6:11]
rt = s[11:16]
imm = s[16:]
return f"{op} {rs} {rt} {imm}"
def r_type(s):
op = s[0:6]
rs = s[6:11]
rt = s[11:16]
rd = s[16:21]
shamt = s[21:26]
funct = s[26:]
return f"{op} {rs} {rt} {rd} {shamt} {funct}"
def j_type(s):
op = s[0:6]
addr = s[6:]
return f"{op} {addr}"
def main():
bits = input("bitar: ")
mode = input("i for I-type, r for R-type, j for J-type: ")
match mode.lower():
case "i":
print(i_type(bits))
case "r":
print(r_type(bits))
case "j":
print(j_type(bits))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment