Literals/String: Difference between revisions

m (→‎{{header|6502 Assembly}}: formatting for additional clarity. Why doesn't 6502 highlight opcodes?)
Line 3,049:
 
Double and single quote characters may also be escaped with XML entities: " and ' respectively.
 
=={{header|Z80 Assembly}}==
{{trans|6502 Assembly}}
Strings are enclosed in double quotes.
<lang z80>db "Hello World"</lang>
Any typed character in double quotes is assembled as the ASCII equivalent of that character. Therefore the following two data blocks are equivalent:
<lang z80>db "Hello World"
db $48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64</lang>
 
The assembler typically assumes nothing with regard to special characters. A <code>\n</code> will be interpreted literally, for example. How special characters are handled depends on the printing routine of the hardware's BIOS, or one created by the programmer. If your printing routine is able to support a null terminator and ASCII control codes, the following represents "Hello World" with the new line command and null terminator:
<lang z80>db "Hello World",13,10,0</lang>
 
Creating your own printing routine is a bit out of the scope of this task but here's a simple demonstration that supports the \n and null termination:
<lang z80asm>PrintString:
; HL contains the pointer to the string literal.
ld a,(hl)
or a ;compares to zero quicker than "CP 0"
ret z ;we're done printing the string, so exit.
cp '\' ; a single ascii character is specified in single quotes. This compares A to the backslash's ASCII value.
jr z,HandleSpecialChars ; if accumulator = '\' then goto "HandleSpecialChars"
call PrintChar ;unimplemented print routine, depending on the system this is either a BIOS call
; or a routine written by the programmer.
 
inc hl ;next character
jr PrintString ;back to top
 
 
HandleSpecialChars:
inc hl ;next char
ld a,(hl)
cp 'n'
call z,NewLine ;unimplemented routine, advances text cursor to next line. Only called if accumulator = 'n'.
inc hl ;advance past the 'n' to the next char.
jr PrintString ;jump back to top. Notice that neither the backslash nor the character after it were actually printed.</lang>
 
=={{header|zkl}}==
1,489

edits