Differences
This shows you the differences between two versions of the page.
| Previous revision | |||
| — | asm [2026/09/14 09:50] (current) – [Referencing Strings in Assembly Code] jjflash | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ======ASM====== | ||
| + | [pet] [vic20] [c64] [c16] [cplus4] [c128] [x16] [m65] | ||
| + | =====Syntax===== | ||
| + | <code xcbasic> | ||
| + | ASM | ||
| + | < | ||
| + | < | ||
| + | END ASM | ||
| + | </ | ||
| + | |||
| + | The '' | ||
| + | |||
| + | ===== Example ===== | ||
| + | <code xcbasic> | ||
| + | REM -- set border to black using XC=BASIC code | ||
| + | POKE $d020, 0 | ||
| + | REM -- set background to black using inline assembly | ||
| + | ASM | ||
| + | lda #$00 | ||
| + | sta $d021 | ||
| + | END ASM | ||
| + | REM -- the XC=BASIC program continues on from here | ||
| + | PRINT " | ||
| + | END | ||
| + | </ | ||
| + | The above example illustrates how to use '' | ||
| + | |||
| + | Note the spaces before the opcodes on each line of dasm assembly code. Leading spaces are required by dasm for any line of assembly that is not a valid label in dasm. | ||
| + | |||
| + | <adm warning> | ||
| + | Each line in dasm assembly code must start with a label or at least one space. | ||
| + | </ | ||
| + | |||
| + | <adm note>The **XC=BASIC** compiler cannot validate the in-line assembly code. It will be copied verbatim into the XC=BASIC assembly before being compiled by dasm, so you must take special care with the '' | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Referencing BASIC Non-String Variables in Assembly Code ===== | ||
| + | |||
| + | To use BASIC non-string variables in assembly code, use the syntax '' | ||
| + | |||
| + | <code xcbasic> | ||
| + | a = 2 ' global variable | ||
| + | b = 7 | ||
| + | |||
| + | SUB test () STATIC | ||
| + | DIM a AS BYTE ' local variable | ||
| + | a = 5 | ||
| + | ASM | ||
| + | lda #$06 | ||
| + | sta {a} ; {a} is resolved to the local " | ||
| + | sta {b} | ||
| + | END ASM | ||
| + | PRINT a | ||
| + | END SUB | ||
| + | |||
| + | CALL test () | ||
| + | PRINT a | ||
| + | PRINT b | ||
| + | </ | ||
| + | <adm warning> | ||
| + | Only STATIC variables can be referenced using the curly braces syntax. | ||
| + | </ | ||
| + | |||
| + | ===== Referencing Strings in Assembly Code ===== | ||
| + | Passing an **XC=BASIC** string by reference will not always produce expected results, because **XC=BASIC** strings in memory are preceded by a single byte string length and are not zero-terminated. Therefore, strings for operations like KERNAL calls (SETNAM, for example) must be be converted first. There are a couple of techniques you can use to accomplish this: | ||
| + | * Place the string into unused memory and terminate it with a zero byte, then reference that location in the assembly code | ||
| + | * Create a byte array with stringlen + 1 elements, convert the string to a KERNAL-friendly format, then pass the array by reference in the assembly code | ||
| + | |||
| + | An example of the byte array technique: | ||
| + | <code xcbasic> | ||
| + | ' create a byte array, fill it with the string " | ||
| + | DIM fname(5) AS BYTE | ||
| + | fname(0) = ASC(" | ||
| + | fname(1) = ASC(" | ||
| + | fname(2) = ASC(" | ||
| + | fname(3) = ASC(" | ||
| + | fname(4) = 0 | ||
| + | |||
| + | ' set KERNAL SETNAM routine to point to fname | ||
| + | ASM | ||
| + | lda #$01 | ||
| + | ldx #< | ||
| + | ldy #> | ||
| + | jsr $ffbd ; SETNAM Kernal routine | ||
| + | END ASM | ||
| + | </ | ||
| + | Or, if you have a longer string that you want to pass, consider iterating over the string with '' | ||
| + | |||
| + | <code xcbasic> | ||
| + | DIM a(22) AS BYTE | ||
| + | DIM x AS BYTE | ||
| + | DIM b AS STRING * 20 | ||
| + | b = "the quick brown fox" | ||
| + | |||
| + | ' will result in a byte array of "the quick brown fox" | ||
| + | ' and a zero byte in the next array element, ready | ||
| + | ' for use by a KERNAL call | ||
| + | FOR x = 0 to LEN(b) | ||
| + | a(x) = ASC(MID$(b, x, 1)) | ||
| + | NEXT | ||
| + | a(len(b) + 1) = 0 | ||
| + | </ | ||