neriya wrote
what is a better style and way about setting the return type of the functions in the code module.
I'm programming in java, which has an easy way of dealing with strings, so initially I thought of going case by case and setting the return type as strings. this seems like it is taking too much memory (should I care about this?). Is returning a boolean array better, while trying to capture the logic better?
thanks,
Neriya
Code clarity is generally more important than optimizing code size/speed and memory use.
My assembler, written in Python, initializes
dicts (Java
HashMaps) that map the mnemonic strings to the required command bits as strings. The C-command can then be constructed by simple string concatenation. This is reasonably efficient, too, since the output of the assembler is strings.
Most efficient from a code speed/memory size perspective would be to return the required bits in an integer, shifted into the correct position so that the various parts of the C-command could be combined with bitwise or. The code to do this is more obscure and error prone, and the savings is negligible compared to the size/execution time of the rest of the assembler. This would not be a good tradeoff.
In Code's constructor:
_compDict = {'0': '0101010',
'1': '0111111',
...
'M|D': '1010101'}
Code.Comp() Note: 'self' is Java's 'this'. 'None' is, in effect, a null string.
def Comp(self, mnemonic):
if mnemonic in self._compDict:
return self._compDict[mnemonic]
else:
return None
--Mark