EASy68K  
It is currently Tue Dec 11, 2018 12:25 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: copying strings
PostPosted: Sat Mar 12, 2011 8:03 pm 
Offline

Joined: Sat Mar 12, 2011 2:53 am
Posts: 9
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]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2011 1:07 am 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
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.


Top
 Profile  
 
 Post subject: Re: copying strings
PostPosted: Wed Mar 23, 2011 1:13 pm 
Offline

Joined: Sat Mar 12, 2011 2:53 am
Posts: 9
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?


Top
 Profile  
 
 Post subject: Re: copying strings
PostPosted: Wed Mar 23, 2011 2:40 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
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


Top
 Profile  
 
 Post subject: Re: copying strings
PostPosted: Wed Mar 23, 2011 3:48 pm 
Offline

Joined: Sat Mar 12, 2011 2:53 am
Posts: 9
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)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2011 5:21 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
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)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group