6
1
Let's assume we've got an imaginary assembler. The assembler supports numerical labels. An infinite loop example:
:1
jmp 1
Your task is, to write a preprocessor for this assembler supporting named labels (instead of numerical ones; with up to 8 characters in length), so the label name is replaced to it's numerical value.
The assembler uses a dot (.) to express a character constant, for example:
mov dl, .A
Will store ASCII(65) - 'A', in the register dl. The assembler supports string constants in double quotes too:
:20
db "Hello, world!"
So, to sum things up, you need to be aware of character constants, and the fact that you can't perform any substitution inside quote-enclosed strings.
The label declaration character and reference character is up to your choice, but for the sake of completness I will use a $ to refer to a label, and @ to denote a label.
The label' number might be any natural number not equal to zero.
You may assume that assembly on the input is perfectly valid.
Let's look at a few examples:
@loop
jmp $loop
:1
jmp 1
jmp $skip
mov dl, .@
@skip
cmp dl, .$
jmp 1
mov dl, .@
:1
cmp dl, .$
@x
db "$a @b"
@a
jmp $a
@b
jmp $b
:1
db "$a @b"
:2
jmp 2
:3
jmp 3
This is a code golf, so the smallest answer wins. Standard rules and loopholes apply.
You can safetly assume that the user won't use numerical labels anymore, and the labels contain just A-Za-z letters.
Label declaration and reference character has to be different.
2May the strings contain escaped double-quotes? (e.g.
db "this is \"bad\"") – Arnauld – 2019-10-09T16:20:13.103@Arnauld nope, they can't. The example you've given is incorrect input so you don't need to deal with that. – Krzysztof Szewczyk – 2019-10-09T16:29:38.183
Can the named labels contain numbers? Besides a newline can they contain any ASCII character? Or even any character? – FryAmTheEggman – 2019-10-09T16:33:57.360
@Arnauld sorry, my mistake. – Krzysztof Szewczyk – 2019-10-09T16:50:54.147
@FryAmTheEggman you can assume just A-Za-z letters. – Krzysztof Szewczyk – 2019-10-09T16:51:10.217
@Arnauld no, they have to be different. – Krzysztof Szewczyk – 2019-10-09T16:51:40.410
May a single line contain both a string and a genuine reference to a label? – Arnauld – 2019-10-09T17:01:15.827
1(Or maybe more generally: may a genuine reference to a label be followed by anything other than an end of line?) – Arnauld – 2019-10-09T17:11:37.970
@Arnauld yes, it can. – Krzysztof Szewczyk – 2019-10-09T17:27:54.867
You should specify what the behaviour of each of the examples is – Jo King – 2019-10-09T21:11:07.123
2Can anything be on the same line as a label? – Jonathan Allan – 2019-10-09T23:07:54.200
1Can newlines be inside quoted strings? – Jonathan Allan – 2019-10-09T23:09:25.903
1Can quote characters appear anywhere for any other purpose? – Jonathan Allan – 2019-10-09T23:11:23.633
1Answering questions in order, yes - a comment starting with a semicolon and ending with a newline, no - they can not, yes - you have to parse the syntax, not guess the semantics. – Krzysztof Szewczyk – 2019-10-10T05:14:13.410
Do we need to preserve comments unchanged? Also there is nothing about comments in the question itself. – Qwertiy – 2019-10-17T14:48:41.300