简体   繁体   English

使用 Spim 将 MIPS asm 转换为十六进制

[英]Converting MIPS asm to hexadecimal using Spim

So I'm doing this assignment from an online CMU class where I am writing an instruction-level MIPS simulator.因此,我正在从在线 CMU 课程中完成此作业,我正在编写指令级 MIPS 模拟器。 There are several input files given in asm format, which have to be converted into hexadecimal form in order to be read by the simulator.有几个以 asm 格式给出的输入文件,它们必须转换为十六进制格式才能被模拟器读取。 An asm2hex Python program is provided which uses spim to convert the asm to hexadecimal, but it doesn't seem to work.提供了一个 asm2hex Python 程序,它使用 spim 将 asm 转换为十六进制,但它似乎不起作用。 There's an unknown argument to spim '-vasm', and the program doesn't actually output any files when run. spim '-vasm' 有一个未知参数,该程序在运行时实际上不输出任何文件。 The asm2hex.py is down here: asm2hex.py 在这里:

#!/usr/bin/python

import os, argparse, subprocess

# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("fasm", metavar="input.s", help="the MIPS assembly     file (ASCII)")
args = parser.parse_args()

fasm = args.fasm
fhex = os.path.splitext(args.fasm)[0] + ".x"

# run SPIM (the actual MIPS assembler)
SPIM = "/afs/ece/class/ece447/bin/spim447"
cmd = [SPIM, "-notrap", "-vasm", fasm, fhex]
subprocess.call(cmd)

# SPIM outputs many files; but we are interested in only one
cmd = ["mv", fhex + ".text.dat", fhex]
subprocess.call(cmd)

# remove unnecessary two lines from the file
lines = open(fhex).readlines()
lines = map(lambda x: x.lstrip(), lines)
data = str.join('', lines[2:])
data = str.join('\n', data.split())
open(fhex, 'w').write(data)

# remove all other files
cmd = ["rm", fhex + ".*.dat"]
cmd = str.join(' ', cmd)
subprocess.call(cmd, shell=True)  # we need a shell to expand the   wildcard

The program basically stores the hexa code found in the text segment shown in xspim to a .x file.该程序基本上将在 xspim 中显示的文本段中找到的 hexa 代码存储到 .x 文件中。 Can this code be fixed, or can someone suggest an alternative way to extract that part of the text segment?这段代码可以修复吗,或者有人可以建议另一种方法来提取文本段的那部分吗?

If you use the qtSpim simulator (which I believe just uses Python so it should be relatively simple to install), then you don't need any extra conversion step.如果您使用 qtSpim 模拟器(我相信它只使用 Python,因此安装起来应该相对简单),那么您不需要任何额外的转换步骤。 qtSpim should read all instructions and pseudo-instructions without complaint. qtSpim 应该毫无怨言地阅读所有说明和伪说明。 You can get it here你可以在这里得到

For reference, I just ran into this issue, and utilized WebMIPSASM to convert the assembly code to hex.作为参考,我刚遇到这个问题,并利用WebMIPSASM将汇编代码转换为十六进制。 Remove the directives at the top of the MIPS assembly code and it should produce the hex output.删除 MIPS 汇编代码顶部的指令,它应该产生十六进制输出。

Save this output to a file with the ".x" name and the simulator will run.将此输出保存到名称为“.x”的文件中,模拟器将运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM