0

I am new to messing around with LC-3 and am stuck on a problem. I want to be able to input an entire string, such as "Hello, my name is Connor" and when I press enter, THEN it is echoed in the console. I have done a problem prior where I read a character and then echoed it continuously until space was pressed, But I am not too sure how I can accomplish a whole string and then echoing it on enter. Any help to get me started?

What I end up with would look like this (all .fill commands):

.ORIG x3000     ; specify the "origin"; i.e., where to load in memory

; machine instructions
.FILL x2006
.FILL x2206
.FILL x0403
.FILL xF021
.FILL X127F
.FILL x0FFC
.FILL xF025
.FILL x005A
.FILL x0064

.END
Jolta
  • 2,620
  • 1
  • 29
  • 42
connor moore
  • 611
  • 1
  • 8
  • 18

1 Answers1

1

In your previous problem, you were able to call a trap for OUT after getting a character from the user. In order to read the entire string from the user until a new line, you need to store each character in memory until they enter a new line, then call a trap for PUTS.

I don't know if you're this far into your course yet, but first, I wrote the program in assembly, just to make sure I had the basic idea down.

MAIN
    LEA r1, INPUT       ; r1 = address of input

PROMPT
    TRAP x20            ; getc, r0 = character input
    STR r0, r1, #0      ; mem[r1 + INPUT] = r0
    ADD r1, r1, #1      ; r1 = r1 + 1
    ADD r0, r0, #-10    ; r0 = r0 - new line
    BRnp PROMPT         ; branch if r0 != 0

END
    ADD r1, r1, #-1     ; subtract one from r1
    STR r0, r1, #0      ; mem[r1 + INPUT] = r0 (which is zero)
    LEA r0, INPUT       ; r0 = address of input

    TRAP x22            ; puts, display string
    TRAP x25            ; halt

; data segment
INPUT .BLKW 64

Then, just go line by line and create the instructions manually in binary:

.ORIG x3000

; main
    1110 001 000001010      ; lea r1, INPUT

; prompt
    1111 0000 x20           ; TRAP x20, halt
    0111 000 001 000000     ; STR r0, r1, #0
    0001 001 001 1 00001    ; ADD r1, r1, #1
    0001 000 000 1 10110    ; ADD r0, r0, #-10
    0000 101 111111011      ; BRnp PROMPT (jump -5 if zero)

; end
    0001 001 001 1 11111    ; ADD r1, r1, #-1
    0111 000 001 000000     ; STR r0, r1, #0
    1110 000 000000010      ; LEA r0, input ( + 2)

    1111 0000 x22           ; TRAP x22, puts
    1111 0000 x25           ; TRAP x25, halt

.BLKW 64                    ; input
.END

And then finally, convert those to hex and put them each in a .FILL

.ORIG x3000

; main
    .FILL xE20A

; prompt
    .FILL xF020
    .FILL x7040
    .FILL x1261
    .FILL x1036
    .FILL x0BFB

; end
    .FILL x127F
    .FILL x7040
    .FILL xE002

    .FILL xF022
    .FILL xF025

; data
.BLKW 64

.END
Ryan Pendleton
  • 2,566
  • 19
  • 29