Copy a string: Difference between revisions

Line 39:
B DS CL64 b
REFA DS A @a</lang>
=={{header|6502 Assembly}}==
<lang 6502asm>source equ $10 ;$10 was chosen arbitrarily
source_hi equ source+1 ;the high byte MUST be after the low byte, otherwise this will not work.
dest equ $12
dest_hi equ dest+1
 
LDA #<MyString ;get the low byte of &MyString
STA source
LDA #>MyString ;get the high byte
STA source_hi ;we just created a "shallow reference" to an existing string.
;As it turns out, this is a necessary step to do a deep copy.
 
LDA #<RamBuffer
STA dest
LDA #>RamBuffer
STA dest_hi
 
 
strcpy:
;assumes that RamBuffer is big enough to hold the source string, and that the memory ranges do not overlap.
;if you've ever wondered why C's strcpy is considered "unsafe", this is why.
 
LDY #0
.again:
LDA (source),y
STA (dest),y
BEQ .done
INY
BNE .again ;exit after 256 bytes copied or the null terminator is reached, whichever occurs first.
RTS
 
MyString:
byte "hello",0
RamBuffer:
byte 0,0,0,0,0,0</lang>
 
=={{header|68000 Assembly}}==
Making a reference to an existing string is simple. Just load its memory location into an address register.
1,489

edits