Having trouble in handling variable symbols

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Having trouble in handling variable symbols

thedumbone
I have written code such that when a new variable symbol appears, it gets values from 16 onwards.

For example, @i would translate to 0000 0000 0001 0000 and @sum would translate to 17 (in binary) in the .hack file. But the file that the provided assembler generates outputs from 10 onwards, while incrementing twice. Example, @i would translate to 10 and @sum would translate to 12, then 14 etc.

What am I missing here?
Reply | Threaded
Open this post in threaded view
|

Re: Having trouble in handling variable symbols

ivant
Can you give a small but full example program in HACK assembly and what your assembler generates for it?
Reply | Threaded
Open this post in threaded view
|

Re: Having trouble in handling variable symbols

thedumbone


this is the comparison from Max.asm

Similarly, all symbol-less files are generating correct code but when I assemble the .asm files with symbols, my assembler generates different code. I think my code is correct since there are few mentions about some bug in the provided assembler. Could you check that? if my code is wrong or there's a bug?

function that translates A instructions: (for your reference)

Reply | Threaded
Open this post in threaded view
|

Re: Having trouble in handling variable symbols

ivant
The problem is, that OUTPUT_FIRST is actually defined in the source code as a label so you should not treat it as a variable.

How can you handle this case?
Reply | Threaded
Open this post in threaded view
|

Re: Having trouble in handling variable symbols

pm100
In reply to this post by thedumbone
OUTPUT_FIRST is a label, it is at offset 10 thats why the assembler inserts 10 there, same for OUTPUT_D

You need to distingush between @<label> @<number> and @<variable>

When you hit @xxx you need to check to see if there is a (xx) if so then insert the offset of xx, otherwise generate a new variable address starting at 16
Reply | Threaded
Open this post in threaded view
|

Re: Having trouble in handling variable symbols

thedumbone
In reply to this post by ivant
Thank you. it's working after fixing the code
Reply | Threaded
Open this post in threaded view
|

Re: Having trouble in handling variable symbols

WBahn
Administrator
In reply to this post by pm100
pm100 wrote
OUTPUT_FIRST is a label, it is at offset 10 thats why the assembler inserts 10 there, same for OUTPUT_D

You need to distingush between @<label> @<number> and @<variable>

When you hit @xxx you need to check to see if there is a (xx) if so then insert the offset of xx, otherwise generate a new variable address starting at 16
Just to clarify things a bit:

You need to make two passes through the code. On the first pass, you identify all of the labels and associated a number with each that represents the ROM address at which the next instruction will be stored.

On the second pass, when you encounter a symbol, you first check to see if it is one of the predefined symbols and, if so, replace it with the associated value. If it is not, you check to see if it is already defined as a label from the first pass and, if so, replace it with the associated value. If it is not, you check to see if it is already a defined variable and, if so, replace it with the associated value. If you have gotten to this point, THEN you know you have encountered a new variable for the first time and you assign it a new address, starting at 16.

While this is the conceptual logic, it can be significantly streamlined (as recommended in the text) by recognizing that, fundamentally, labels and variables are the same things -- namely symbols that need to be replaced by the numerical equivalent that is stored in a symbol table.

So you can have a single symbol table and this table does not need to track whether each entry is pre-defined, a label, or a variable. The symbol is either in the table or it isn't, and if it is, it has a single number associated with it that is used to replace the symbol in the instruction.

So you can initialize the symbol table with all of the pre-defined symbols. Then make your first pass and do nothing except add an entry for each label. Then, on your second pass, you check each use of a symbol (which can only appear in @xxx instructions) and if it is already in the symbol table (doesn't matter how or when it got there), you replace it. Otherwise, you add it using the next available RAM address and then replace it.