Friday, 20 December 2013

We can very easily turn the array created in exercise 3 into a c-string: just make it an 11 element array, with the last element a null (remember that in the LC-3, each 8-bit character occupies the lower byte of a 16-bit word, so the last word in the array will be x0000). Adjust your code from exercise 3 to create a 10 character c-string (i.e. 10 input characters terminated with the null character). Load the starting address of the string into R0, then use TRAP x22 to output it to the monitor, just as you did with the c-string constructed by the .STRINGZ pseudo-op.

    .ORIG x3000

; take 10 character input from user
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)
Input
    TRAP x23    ; input character
    STR R0,R3,#0    ; store inputted char in memory
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Input    ; branch to input if R2 is positive
    LD R0,Null    ; load Null in R0
    STR R0,R3,#0    ; store Null at end of array
       
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)

; add 1 to each character
Add1
    LDR R0,R3,#0    ; load value from mem in R0
    ADD R0,R0,#1    ; increment R0 so it'll be plus 1 character
    STR R0,R3,#0    ; store inputted char in memory
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Add1    ; branch to Add1 if R2 is positive

; output characters by using TRAP x22 from memory address stored in R0 to Null
    LEA R0,Label    ; store base array addr in R0
    TRAP x22    ; display output from memory address from R0 untill it founds Null

    HALT
Label    .BLKW #11
Null    .FILL x0000
Ten    .FILL #10
    .END

No comments:

Post a Comment