使用python来模拟运行mips

使用python来模拟运行mips

    • 用法
      • Shell运行
      • 汇编(mips指令->机器码)
      • 反汇编(机器码->mips指令)
      • 运行
      • 运行 - Debug
    • 介绍
  • Contributing
    • Dev info
      • `Assembler`
      • `Simulator`
      • Dependency
      • Unit test

用法

pip install fengyong来安装一个mips模拟器,它可以支持Shell运行,汇编,反汇编,按文件运行,运行时debug

Shell运行

在cmd里输入 mips-shell 命令就能启动一个mips-shell,在shell里可以很方便的运行mips的指令
使用python来模拟运行mips_第1张图片

汇编(mips指令->机器码)

from fengyong import Assembler

instructions = """
j 10000
add $s0,$a1,$t7
sw $s1,10($s2)
"""

machine_code = Assembler.encode(instructions)
print(machine_code.bin)

反汇编(机器码->mips指令)

from fengyong import DisAssembler
from fengyong import  RegData

machine_code = RegData("0x8002710af820ae51000a")
instructions = DisAssembler.decode(machine_code)
print(instructions)

运行

  • 按行运行mips指令:
    from fengyong import Simulator
    
    Simulator.run_line("addi $s0, $s1, 10")
    
  • 运行asm文件:
    from fengyong import Simulator
    
    Simulator.run_file("../test/drings.asm")
    

运行 - Debug

  • 设置寄存器数据
    from fengyong import Registers
    from fengyong import RegData
    
    Registers.reg_set("$s0",RegData(100))
    
  • 获取寄存器数据
    from fengyong import Registers
    
    res = Registers.reg_get("$s0")
    
    # print all the "s" registers
    Registers.print("s")
    
    # print all registers
    Registers.print()
    
  • Github链接: https://github.com/CQU-AI/pymips
  • Pypi 链接:https://pypi.org/project/fengyong/

介绍

这下面是Github仓库上的介绍

MIPS-Simulator runs MIPS32 programs. Unlike real simulator, which assembles the instructions into machine code and executes them, MIPS-Simulator just parse the instructions and runs them using python code. In other words, MIPS-simulator actually interprets mips instructions.

开发者文档也比较详细:

Contributing

Everyone is welcomed to contribute!

To contribute, you may want to read the README to find out what we are doing here.

Dev info

Here are some other informations for developers:

This project mainly contains two seperate part : Assembler and Simulator

Assembler

  • class Assembler : Encodes mips instructions to machine code.
  • class DisAssembler : Decodes machine code to mips instructions.
  • Depends on class RegData, misc.static.

Simulator

  • class Interpreter : Parse and run mips instructions.
  • class Simulator : Run the mips instructions.
  • Depends on class RegData,class Memory,class Registers,misc.static.
  • Supported mips instructions:add,addi,and,andi,beq,bgez,bgtz,blez,bltz,div,j,lui,lw,mfhi,mflo,mult,noop,or,ori,sll,sllv,slt,slti,srl,srlv,sub,sw,xor,xori

Dependency

  • class RegData : [Key of this project] Deals with all kinds of numbers and number_length stuffs.
  • class Memory : Simulates sparse memory (singleton mode).
  • class Registers : Simulates registers (singleton mode).
  • misc.static : Stores the static dictionaries.

Unit test

See ./test/unit_test

你可能感兴趣的:(使用python来模拟运行mips)