assemblylc3

What is the equivalent of .WORD in LC3?


; Define memory locations for user input buffer and opcode output
INPUT_BUFFER .WORD x2000 ; Memory location to store user input
OPCODE_OUTPUT .WORD x2004 ; Memory location to store opcode output

I tried = . and =. they didn't work unfortunately


Solution

  • You're looking for .FILL ... to provide a value for an initialized data word as .word ... would work in other assembly languages.

    INPUT_BUFFER .FILL x2000
    OPCODE_OUTPUT .FILL x2004
    

    Use .BLKW ... to provide a count of a number of zero initialized data words.


    There is no equ or equate or #define or = to declare numeric constants as far as I know.

    But .word doesn't declare a constant either, it declares an initialized data word, which is exactly what .fill does.  (There is a difference in that that .word ... usually accepts multiple comma separated values, whereas LC-3's .fill ... requires one argument per line.)


    You can use these labels with LC-3's indirect loads and stores, LDI and STI.  For example,

    LDI R0, INPUT_BUFFER
    

    will use the data memory location at INPUT_BUFFER to get the address of where to go to get the value to put into R0.  So, the instruction itself refers to INPUT_BUFFER, and INPUT_BUFFER stores x2000, which is the effective address used to fetch from memory for R0.

    You can also load the pointer value directly:

    LD R1, INPUT_BUFFER    # now have x2000 in R1
    LDR R0, R1, #0         # load from location at R1+#0, so x2000
    

    And further, you can increment/modify the memory location at INPUT_BUFFER, so

    LD R1, INPUT_BUFFER
    ADD R1, R1, #1
    ST R1, INPUT_BUFFER
    

    That will change the memory location at INPUT_BUFFER to x2001 (assuming it was x2000 to start with), so for example, a next LDI or STI to that memory location will ultimately address memory location x2001.


    Personally for small algorithms I would prefer to load pointers into registers and use them there including incrementing in registers in loops for example.  LC-3, with 8 usable registers, is register rich compared to MARIE, HACK, LMC and some others.