简体   繁体   中英

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. 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. An asm2hex Python program is provided which uses spim to convert the asm to hexadecimal, but it doesn't seem to work. There's an unknown argument to spim '-vasm', and the program doesn't actually output any files when run. The asm2hex.py is down here:

#!/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. 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 should read all instructions and pseudo-instructions without complaint. You can get it here

For reference, I just ran into this issue, and utilized WebMIPSASM to convert the assembly code to hex. Remove the directives at the top of the MIPS assembly code and it should produce the hex output.

Save this output to a file with the ".x" name and the simulator will run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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