Definition of allowed symbols? Clarification about how to deal with it?

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

Definition of allowed symbols? Clarification about how to deal with it?

mmiller7
I'm a bit confused about what symbols are allowed in the hack computer asm.  I know the A-instruction can only load a 15-bit value, but what happens if the person puts something else that can't be loaded?

Is there an official definition for a user-defined symbol?
I can't find one...

For example, this is obviously valid for a variable:
@x
M=D

And this is obviously valid for a loop:
(y)
//do stuff
@y
0;JMP

These all do something strange and gives no errors...how does the built-in assembler deal with it?
@65535    //too big a number, I haven't found the pattern of what it does yet
@-1       //doesn't load a negative number, I haven't figured out what it does
@1stVar   //can symbol names start with a number?

Either I'm not understanding the definition of the symbols, or I feel like the assembler should abort and throw some sort of error if those aren't allowed.
Reply | Threaded
Open this post in threaded view
|

Re: Definition of allowed symbols? Clarification about how to deal with it?

cadet1620
Administrator
Garbage in, garbage out.

The specification is in 6.2.1, Constants and Symbols. Is specifically precludes symbols that start with numbers.

Properly handling erroneous user input is a big chore, often harder than solving the specified problem. In an ideal world the assembler would only accept its specified input and would produce sensible error messages for all out-of-spec input.

I played around a bit to see what was being generated for the bad input, here's what I found.

CodeSourceNotes
0: 0000000000010000@65535 A-instruction @32  It looks like 65535 was treated as a name and it was allocated to the first data variable address, probably because it is > 32767 (15-bit max).
1: 1111111111111111@-1 Illegal C-instruction
binary -1
  Input parser did not exculde negative numbers, A-instruction generator assumed unsigned value would have top bit = 0.
2: 1111111111111110@-2 Illegal C-instruction
binary -2
3: 0000000000010001@1stVar A-instruction @33  Treated as a name, allocated to the second data variable address
4:(1stLabel)   Defined label, address = 4
4: 0000000000000100@1stLabel A-instruction@4  Loads label address
5:(123)   Defined label??, address = 5
5: 0000000001111011@123 A-instruction@123  Loads value 123
6:(33000)   Defined label, address = 6
6: 0000000000000110@33000 A-instruction@6  Loads label address

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: Definition of allowed symbols? Clarification about how to deal with it?

mmiller7
Ok, so it looks like if I understand this the built-in assembler follows these basic rules(?):
#1 -Anything in parenthasis is defined as a label, even if it makes no sense
#2 -The "@" looks to see if the number can fit in 15-bits and if so ignores the symbol table
#3 -The "@" followed by any negative number is...???? (seems like nonsense to me)
#4 -Any number over 15 bits is treated as a symbol

I want to put in some helpful errors/warnings (which I wish the included assembler did) kind of like how Java does.

I'm thinking I should probably abort and reject if:
-negative numbers put after the "@"

And I should put warnings but continue if:
-15-bit numbers are put in parenthasis
-over 15-bit number after the "@" is treated as a symbol

Does this sound right?
Reply | Threaded
Open this post in threaded view
|

Re: Definition of allowed symbols? Clarification about how to deal with it?

mmiller7
I suppose a smarter assembler might deal with storing negative numbers in the A register by inserting multiple instructions (like A-instruction for A=posativeValue; followed by C-instruction A=-A) to simulate the desired command but I'm not sure if that would be a good idea or if it's better left to a compiler.  Don't know if you have any thoughts or suggestions about this idea...