|
|
|
|
Unless you're planning on having more than 256 opcodes, a 2 byte opcode probably isn't necessary. You could condense this into 1 byte pretty easily. IE: right now you have 10,0,X being MOV A, 'X' and 10,1,X being MOV B, 'X' Instead you could do something like 10,X being MOV A,'X' and 11,X being MOV B,'X'. |
The bigger concern here is that you are assuming all instructions are arranged the same way. That is, the first token is the instruction, the 2nd is either A or B, and the 3rd is an integer. |
Maybe instead... you could have a table that dictates the grammar for all these instructions. Then you parse the line and examine what kind of token you have. Then run those tokens through the table to see which opcode it matches. |
I did want a MOV A,B but the only way I can think of doing is with another opcode, because the program is loaded into memory I'm unsure how to get the value of the registers without using the CPU..? I hope I make sense. |
Not always the case. In my original code, PRINT made the pass increment one more time before the end of the loop, so it skipped right to storing the value in memory. ADD on the other hand, completely skipped the rest of the loop, as the opcode doesn't take arguments, it just adds B into A. |
I guess that's what you did in your example with the const strings? |
Ah... I missed that. That's a little confusing. Manipulating loop counters usually trips me up because I don't expect it. It also might lead to confusion for the user. They might try to do "ADD A, 1" which would assemble without error... but then they'd be surprised when the program is actually doing "ADD A,B" |
That was actually DPut's example, not mine. |
But I was thinking of something even more involved. Have a table to associate strings with specific keywords... but then have another table to associate keywords and grammer with opcodes. |
|
|
|
|
Although your code looks great I don't feel comfortable implementing it |
I don't know what an enum class is, I know what an enum and a class is, not an enum class, the only thing that comes to mind is an enum that can have multiple instances. |
|
|
// values < 0 could be markers to replace with a provided literal <---------- What? |
{ {Token::store, Token::a, Token::integer}, { 4, 0, -1} },
{ {Token::move, Token::integer, Token::integer}, { 14, -1, -2} },
// then have a lookup to get the output based on the given grammar <-------- Again not sure how to do this |
I think the only thing I perfectly understand is the Grammer table, it just seems to be every possible series of tokens that are legal and what it would represent in memory. That's all I can decipher from your code, I'm sorry I'm a noob.. |
That's fine. Do it however you want. I'm just throwing out ideas. Feel free to take or leave whatever you want. |
An enum class is the same as an enum. Only more strongly typed. The only differences are: - You can't implicitly convert a Token to an int (like you could if it was an enum) - You must refer to the Token identifiers with the Token scope. Example:
|
|
|
I don't like copy pasting code that I don't understand. I'll keep reading it and try and implement it another test. |
One question though, Why would one use an enum class? If you cannot convert them to int like a normal enum. |
How do you use them in context? |
|
|
|
|
|
|
Also how do you tokenise the instructions without the sstream? |
|
|
|
|
That Map keyword looks interesting, you are able to assign strings to equal other things? I guess not just an Enum Class? I'm going to read up on it now. |