Question

I am trying to make a simple program using the LC-3 Architecture. All I am trying to do is read a string from the console, somehow save it in memory, and then print it back out.

This is what I have so Far

; This program attemps to read a string and then output it

        .orig   x3000
        and     r1,r1,0
    lea     r0,prompt 
    puts
loop:
    getc
    putc
    add r4,r4,1
    ld      r7,nlcomp  ; check for 
    add     r7,r7,r0   ; end of line

brz finish

    st  r0,lets
    br      loop

finish:

    lea r0,lets
    puts

    halt

lets:  .blkw   20   
prompt: .stringz "Emter String"
nlcomp  .fill   xfff6        
.end

The output displays only the last char in the string. If I was to enter "steve" it would print out "e"

Obviously my problem is that I need to somehow save each char I read in, into its own memory location. I thought using the .blkw would do this, but apparently all it does it overwrite the bits that are in that position.

MY question is how do I store chars in sequential memory locations and then print them out to the console?

Was it helpful?

Solution

You need to use the STR instruction, which allows you to do base-offset addressing. The syntax for STR is:

STR <src register> <base register> <immediate offset>

So, something like the following would be valid:

    LEA R1,MEMORYSPACE ; saves the address of the storage memory block
loop:
    GETC               ; input character -> r0
    PUTC               ; r0 -> console
    STR R0,R1,#0       ; r0 -> ( memory address stored in r1 + 0 )
    ADD R1,R1,#1       ; increments the memory pointer so that it
                       ; always points at the next available block
    BR loop

MEMORYSPACE .blkw 100  ; declares empty space to store the string

OTHER TIPS

I'm not familiar with LC-3 at all, but when I read through your code one thing I see is that you're always storing to the address pointed to by 'lets' and not the address pointed to by 'lets' plus an offset. Since this is marked as homework I'll let you figure out how to do that! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top