Copy a string: Difference between revisions

mNo edit summary
Line 2,442:
StrCopy(S1, S3); \S3 points to a separate copy of the string
]</lang>
 
=={{header|Z80 Assembly}}==
===Making An Additional Reference===
Making an additional reference to a string is easy. If you know the address of the beginning of the string, store that address in RAM somewhere else.
 
<lang z80>ld hl,MyString
ld (PointerVariable),hl
 
MyString: ;assembler equates this label to a memory location at compile time
byte "Hello",0
 
PointerVariable:
word 0 ;placeholder for the address of the above string, gets written to by the code above.</lang>
 
'''NOTE:''' If you're programming for the Game Boy, you can't store a 16-bit value directly into RAM from <code>HL</code>. There are other methods to achieve the same result, and here's one:
<lang z80>ld a,<MyString ; < represents the low byte of the address. Some assemblers use LOW() with the label in the parentheses.
ld (PointerVariable),a
ld a,>MyString ; > represents the high byte of the address. Some assemblers use HIGH() with the label in the parentheses.
ld (PointerVariable+1),a</lang>
 
===Copying A String===
As long as you have enough RAM space to hold the entire string, you can copy it somewhere else in memory. If you know the string's length in advance a simple <code>LDIR</code> will be sufficient. This method will use the null terminator to tell the copy function when to stop:
 
<lang z80>StrCpy:
;input: HL = base address of string you wish to copy
; DE = where you want to copy it to.
; This program assumes that the string is null-terminated, and that there is enough RAM to hold the entire string.
 
ld a,(hl)
or a ;compare A to 0.
ret z
ld (de),a
inc hl
inc de
jr StrCpy</lang>
 
 
=={{header|Zig}}==
1,489

edits