0

The LC-3 assembler must be able to map an instruction's mnemonic into its binary opcode. For instance, given an ADD, it must generate the binary pattern 0001.

The user must type in an LC-3 assembly language mnemonic and the program then displays its binary opcode.

If the assembly language mnemonic is invalid, it displays an error message.

So I'm writing the beginning of an LC-3 assembler in LC-3 assembly language, just handling mnemonic -> opcode mapping, not operands.Is there a way to do this without bruce forcing it? That is to say without having to write every mnemonic into memory.

(related: Difference between: Opcode, byte code, mnemonics, machine code and assembly)

.ORIG x3000
AND R0, R0, #0 
    IN      
    LD  R1, A   
    ADD R1, R1, R0
    BRz ND

LD  R1, B   
    ADD R1, R1, R0
    BRz ZERO    

ND  AND R0, R0, #0
IN
LD  R1, N
    ADD R1, R1, R0
    BRz ZZZO
    BRnp
ZZZO    AND R0, R0, #0
    LD  R0, N1
ZERO    AND R0, R0, #0
    OUT 
    HALT
A   .FILL   x8041
B   .FILL   x8042
D   .FILL   x8044
E   .FILL   x8045
I   .FILL   x8049
J   .FILL   x804A
M   .FILL   x804D
N   .FILL   x804E
N1  .FILL   x
O   .FILL   x804F
P   .FILL   x8050
R   .FILL   x8052
S   .FILL   x8053
T   .FILL   x8054
  • 2
    Do you mean the input is an ASCII text *mnemonic*? Or is the input in some numeric format? Regardless, this is off-topic for Stack Overflow. "I'm lost" isn't a specific enough question. [ask]. It looks like you're making a lookup table of some sort, so maybe you want to ask about how to do that specific part of your program. – Peter Cordes Nov 04 '18 at 00:50
  • Ok, now your question was clear enough for me to edit the terminology into something that describes what you're doing, but what problem are you having? You've shown some code, but haven't said what problem you're having with it. It's not a [mcve], and you haven't described what you're stuck on. The data table doesn't look useful *at all*, though: label names aren't accessible at run-time, and you don't need a table to map ASCII codes to themself. If you read an `'A'` from the input, it's already `0x41`. http://www.asciitable.com/. So a linear search in this table makes no sense. – Peter Cordes Nov 06 '18 at 01:26
  • Perhaps you want to make a table of *strings* you can search, to check for a valid multi-character mnemonic. Or maybe a DFA to match a string; find a valid first character, then see if the next character is a valid next one given the table entry for the first one. https://www.ics.uci.edu/~eppstein/161/960222.html. Since you need an opcode at the end, a Radix Tree might be exactly what you want: each node has a list of valid next characters. https://en.wikipedia.org/wiki/Radix_tree and https://en.wikipedia.org/wiki/Trie. Not sure if LC-3 has indirect addressing modes for pointers, though. – Peter Cordes Nov 06 '18 at 01:30
  • Hey, thanks for the feedback I understand better what you're looking for. I actually was able to figure it out, is it better for the site to add what my original problem was or just to delete this post I just want to do my part to make this an even better resource for those interested in Computer Science! – Davis Young Nov 06 '18 at 20:50
  • Also thanks so much for the above, cause that looks a lot better than what I have. Mine is currently really wrong and while works may not be the most efficient build. – Davis Young Nov 06 '18 at 20:51
  • If you have time to edit the question and post a useful answer, that might be good. LC-3 is only used as a teaching architecture, so presumably the only people that could benefit would be students (or self-learners). Mapping a string to a number is a not an asm Q&A I've seen much before, though, so a decent answer might possibly be useful for people on other architectures. – Peter Cordes Nov 07 '18 at 00:16
  • *Is there a way to do this without brute forcing it?* The problem statement requires error checking to reject invalid mnemonics, so you do need to reject non-matches. You do need enough info in memory to exactly match everything valid and reject anything invalid. Otherwise you could maybe use a perfect hash function for you token/symbol table if you only wanted to handle valid inputs. https://en.wikipedia.org/wiki/Perfect_hash_function. e.g. which you generate as a C function with https://www.gnu.org/software/gperf/ – Peter Cordes Nov 08 '18 at 02:47

0 Answers0