Kaya Çelebi
Home About GitHub PyPI Contribute Contact

AssemblyConverter

Follow @kcelebi Star Watch

Let's check out how to use AssemblyConverter. First, let's import it into our Python file:

from riscv_assembler.convert import *

We can now instantiate an object. The constructor's defaults are initialized as so:

AssemblyConverter(output_type = "a", nibble = False, hexMode = False)

output_type refers to whether a converted file should be outputted to an array("a"), a file ("f"), or printed to console ("p"). Here are acceptable usages:

cnv = AssemblyConverter(output_type = "b") # output to array
cnv = AssemblyConverter(output_type = "t") # output to file
cnv = AssemblyConverter(output_type = "p") # printing to console
cnv = AssemblyConverter() #array by default

nibble refers to whether (for file and console outputs) the 32-bit binary numbers should be split in nibbles or kept as an unbroken string. An example output for nibble = True would be:

1101 0110 0000 0000 ...

An example for the default nibble = False would be:

11010110000000...

hexMode gives the option (for text file, array outputs, and console outputs) to output in hexadecimal form instead of binary. By default, the outputs are in binary. Leading zeros are included to properly represent a 32-bit instruction.

Convert

With this object we can apply the key function : convert(). This function takes in either a string with assembly in it or a file name (with .s extension) from the local directory and converts it to the output format of your choice, specified by the object construction. Let's convert the file simple.s:

cnv = AssemblyConverter()
result = cnv.convert('simple.s')

Any empty files, fully commented files, or files without .s extension will not be accepted.

Helper Functions

Here are a few functions that might be useful.

Properties

AssemblyConverter has three properties: output_mode, nibble_mode , and hex_mode. This makes getting/setting these values very accessible. Observe:

cnv = AssemblyConverter(output_mode = 'a')
print(cnv.output_mode) # prints 'a'
cnv.output_mode = 'f' # set output_mode to files

Instructions

It is possible to check whether a certain instruction is supported by this package. The function instructionExists(instr) returns a boolean for whether instr exists. Observe:
cnv = AssemblyConverter()
cnv.instructionExists("add") #returns true
cnv.instructionExists("hello world") #returns false