Friday, 20 December 2013

Now write a routine that takes a sequence of 10 characters from the keyboard (echoing each character as it is input) and stores them in an array. Set up a loop that traverses the array adding 1 to each value (this will have the effect of turning a into b, L into M, 8 into 9, etc.) Finally, traverse the array again and output the 10 new characters to the screen. A simple way to set up an array is to use the .BLKW pseudo-op: label .BLKW #10 This sets aside 10 locations in memory, starting at the address "label". You can then load this address into a register using the LEA instruction: LEA R4, label This decodes "label" to the memory address it stands for, and loads that address into register 4 (note that this is quite different from LD, which loads the contents of the memory location "label" into the register; LEA doesn't read anything from memory, it just decodes the address itself).Once you have the starting address of the array in a register, you can use LDR & STR instructions to access it. How will you access the successive addresses? (Note: you may hard-code the number 10 as the loop counter).

    .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

; add 1 to each inputted character
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)
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 each new character
    LEA R3,Label    ; load array base addr in R3
    LD R2,Ten    ; load 10 in R2 (counter)
Output
    LDR R0,R3,#0    ; load value from mem in R0
    TRAP x21    ; display character stored in R0 on console
    ADD R3,R3,#1    ; increment array base addr
    ADD R2,R2,#-1    ; decrement counter register R2
    BRp Output    ; branch to Output if R2 is positive

    HALT
Label    .BLKW #10
Ten    .FILL #10
    .END

No comments:

Post a Comment