-2

The task at hand is to write a subroutine STRCPY to implement a string copy function like the C the programming language's strcpy() function.

I know:

R1 is the address of the string to copy from

R2 is the address of where the string is to be copied to

It's to be assumed that the function should copy every character of the source string to the destination address (including the null terminator), thus creating a complete duplicate copy of the source string. Also, it can be assumed that the caller has allocated sufficient space for the new string, and the subroutine will not return any information to the caller.

The starting code is given here.

I guess I'm just somewhat overwhelmed by all of the LEA commands at the beginning and as such, any guidance/assistance would be tremendously appreciated.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jeremy
  • 7
  • 1

1 Answers1

1

I don't have your starting code, but guessing at what is required...

You might start by producing a more detailed explanation in English.

For example:

You have a string to copy. The string is in memory, beginning at a fixed address. The fixed address is in R1. You are going to copy the string to memory, to another fixed address. That other fixed address is in R2.

You can approach this by copying one character, moving to the next, and repeating. You will stop when you hit the last character (the one with value 0).

  1. Copy the character at the address in R1 to the address in R2
  2. Check if the character copied was the null terminator
  3. If it was, stop.
  4. If it was not, move the addresses in R1 and R2 up one character (point to next character), and go to step (1).

Then turn that into assembly:

Some of the sorts of instructions you will to translate this English recipe into LC3 assembly are likely to be:

LDR R3, R1, #0 will load the character at the address in R1 into R3.

STR is the analogous command you can use for storing the character in R2 (but you should work that out yourself if this is homework).

ADD R1, R1, #1 will add 1 to the address in register R1 (R1 = R1 + 1).

AND R4, R3, x1111 will set R4 to 0 if R3 is 0 (the null character) (R4 = R3 & 0x1111).

BRZ DONE will go to the label "DONE:" if the last instruction set the zero flag.

LEA R5, NEXT followed by JMP R5 will go to the label "NEXT:" by loading the address of the label "NEXT:" into R5 and then jumping to that value.

I imagine your code will look something like:

LEA R5, NEXT                Put the address of NEXT in R5
NEXT:                       
    LDR R3, R1, #0          Copy what is in the address in R1 (a character) to R3
    STR...                  Store the character in R2
    AND R4, R3, x1111       See if the character in R3 (the one copied) is 0
    BRZ DONE                If it is, finish
    ADD R1, R1, #1          If not, go to the next character
    ADD ...
    JMP R5                  Jump to the address in R5 (which is NEXT)
DONE:
    ...

You should not assume I have this correct. I don't have an LC3 simulator handy.

Best of luck.

Tony
  • 1,645
  • 1
  • 10
  • 13