EASy68K
http://www.easy68k.com/EASy68Kforum/

copying strings
http://www.easy68k.com/EASy68Kforum/viewtopic.php?f=6&t=968
Page 1 of 1

Author:  alyshaz103 [ Sat Mar 12, 2011 8:03 pm ]
Post subject:  copying strings

is this correct?

* Copy the characters of the string S1 to the string S2
Code:
           LEA        S1,A0
      LEA   S2,A1      ;loads S2 into A1
COPY   MOVE.B    (A0),(A1)   ;copy a character from A0 to A1
      ADD.L   #1, A0      ; increments address in A0
      ADD.L    #1, A1      ; increments address in A1
      CMP.B    #0, (A0)   ; compares the value of A0 to zero
      BNE   COPY  ; If zero has not been reached, it continues to loop
* Variables and Strings
   ORG     $2000
DATA
S1:   DC.B    'red',0   ; A Declaration of a string(including null)
S2:   DS.B            80      ; A string of max 80 characters (including null)   

[/code]

Author:  clive [ Sun Mar 13, 2011 1:07 am ]
Post subject: 

No, it doesn't copy the NUL terminating character, and will fail with the empty string case.

MOVE.B (A0)+,D0
MOVE.B D0,(A1)+

Might be more efficient too.

Author:  alyshaz103 [ Wed Mar 23, 2011 1:13 pm ]
Post subject:  Re: copying strings

alyshaz103 wrote:
thanx for replying so the code should be

* Copy the characters of the string S1 to the string S2
Code:
      LEA     S1,A0
      LEA     S2,A1      ;loads S2 into A1
COPY  MOVE.B (A0)+,D0   ;copy a character from A0 to D0
      MOVE.B  D0,(A1)+
      CMP.B    #0, (A0)   ; compares the value of A0 to zero
      BNE   COPY  ; If zero has not been reached, it continues to loop
* Variables and Strings
   ORG     $2000
DATA
S1:   DC.B    'red',0   ; A Declaration of a string
S2:   DS.B        80      ; A string of max 80 characters (including null)   

[/code] And also how do i get the length of S1?

Author:  clive [ Wed Mar 23, 2011 2:40 pm ]
Post subject:  Re: copying strings

Your code *still* fails to copy the NUL properly, and it will fail with an empty string. You should step through it with the simulator and look at the memory window. You should try it with some corner/boundary conditions.
alyshaz103 wrote:
And also how do i get the length of S1?

You could either count the number of characters, or subtract the before/after addresses to determine how many characters were moved.

I would do it like this.
Code:
   ORG   $1000
START:            ; first instruction of program

      LEA     S1,A0     ; loads address of S1 into A0
      MOVE.L  A0,D1     ; saves S1 address into D1
      LEA     S2,A1     ; loads address of S2 into A1
COPY  MOVE.B (A0)+,D0   ; copy a character from A0 to D0
      MOVE.B  D0,(A1)+
      BNE   COPY     ; If zero has not been reached, it continues to loop

      MOVE.L  A0,D0   ; copy address beyond null
      SUBQ.L  #1,D0     ; step back to null
      SUB.L   D1,D0     ; D0 = Length of String S1
     
      MOVE.B   #9,D0
      TRAP   #15   ; halt simulator

* Variables and Strings
   ORG     $2000
DATA
S1:   DC.B    'red',0   ; A Declaration of a string
S2:   DS.B        80      ; A string of max 80 characters (including null)   

or
Code:
      LEA     S1,A0     ; loads address of S1 into A0
      LEA     S2,A1     ; loads address of S2 into A1
      MOVE.L  #-1,D0    ; Clear char counter
COPY2 ADDQ.L  #1,D0     ; Increment char counter
      MOVE.B  (A0)+,(A1)+   ; copy a character from [A0++] to [A1++]
      BNE     COPY2     ; If zero has not been reached, it continues to loop

Author:  alyshaz103 [ Wed Mar 23, 2011 3:48 pm ]
Post subject:  Re: copying strings

THANKS FOR REPLYING CODE WORKS WELL CAN YOU ALSO LOOK AT THIS CODE FOR FINDING STRING LENGTH
Code:
      LEA   STR, A0  ; Get address of string
      CLR.B D1        ; Set counter to 0
LOOP: LOAD.B (A0),D2   ; Get a character from string
      BEQ   DONE  ; Check for null (end of string)
      ADD.B #1, D1  ; Count character
      ADD.L #1, A0  ; Increment address (pointer)
      BRA   LOOP  ; Check again
STR:  DC.B ‘H’, ‘e’,’l’,’l’,’o’,0 ; Declare a list of bytes (string)

Author:  clive [ Wed Mar 23, 2011 5:21 pm ]
Post subject: 

Code:
      LEA   STR, A0     ; Get address of string
      CLR.B D1           ; Set counter to 0
LOOP: MOVE.B (A0)+,D2   ; Get a character from string
      BEQ   DONE   ; Check for null (end of string)
      ADD.B #1, D1     ; Count character
      BRA   LOOP  ; Check again
DONE:
   ; D1.B = String Length

; ...

STR:  DC.B    'H', 'e','l','l','o',0 ; Declare a list of bytes (string)

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/