Distinguish a byte, a length, and an address
The string binding exposes the address of its byte data and its length. In lda @source.data,y, the CPU reads a byte at the string address plus Y. In ldx #<@source.data, X receives the low byte of the address itself. Choose the form from what the receiving instruction or routine expects.
const c64 = @import("c64");
const source = "asm string ok\n";
export fn main() void {
c64.console.clear();
c64.console.print("inline asm strings\n", .{});
var msg = c64.string.buffer(32);
asm {
ldy #0
copy:
lda @source.data,y
sta @msg.data,y
iny
cpy #@source.len
bne copy
lda #@source.len
sta @msg.len
lda #33
sta @msg.data+12
}
c64.console.puts(msg);
c64.console.print("asm string done\n", .{});
c64.vic.border = 5;
}
The loop copies the bytes into the writable buffer and sets its length before printing. The store at offset 12 replaces the K with an exclamation mark. Expect ASM STRING O!, then ASM STRING DONE. Keep the copy within the buffer's 32-byte capacity. #@source.len is an immediate for a constant. @msg.len names a runtime cell that the block can write.
Use a reserved zero-page pair for an indirect read
When the instruction requires indirect addressing, split the address into low and high bytes and put them in a zero-page pair. The example reserves $30–$31 from compiler allocation, saves what was there, reads through ($30),y, and restores the pair. The compiler wrapper saves the registers and status that the block clobbers. The source explicitly handles the memory it borrows.
const c64 = @import("c64");
const text = "A";
memory {
zero_page {
profile = .kernal;
reserve at 0x30 size 2;
restore_basic = true;
interrupts = .system;
}
}
export fn main() void {
c64.console.clear();
var glyph: u8 = 0;
var saved_lo: u8 = 0;
var saved_hi: u8 = 0;
asm {
lda $30
sta @saved_lo
lda $31
sta @saved_hi
lda #<@text.data
sta $30
lda #>@text.data
sta $31
ldy #0
lda ($30),y
sta @glyph
lda @saved_lo
sta $30
lda @saved_hi
sta $31
}
c64.console.print("GLYPH {x}\n", .{glyph});
}
Expect GLYPH 41 from the string data. Compare this with the screen code for A in the hardware chapter. Keep interrupts and firmware workspace in mind when choosing which zero-page bytes to borrow.
Supply the filename and capture carry before it is restored
Read loadTile as three calls. SETNAM takes the filename length in A and its address in X/Y. SETLFS selects logical file 1, device 8, and secondary address 0. LOAD then takes the destination in X/Y, here $9000. The tutorial disk contains a one-byte PRG called tile.
const c64 = @import("c64");
const filename = "tile";
memory {
region staging: c64 at 0x9000 size 0x0100;
}
fn loadTile() bool {
var loaded: bool = false;
asm {
lda #@filename.len
ldx #<@filename.data
ldy #>@filename.data
jsr $ffbd
lda #1
ldx #8
ldy #0
jsr $ffba
lda #0
ldx #$00
ldy #$90
jsr $ffd5
bcs done
lda #1
sta @loaded
done:
}
return loaded;
}
export fn main() void {
c64.console.clear();
if (loadTile()) {
const tile: *volatile u8 = @ptrFromInt(0x9000);
c64.console.print("TILE {x}\n", .{tile.*});
c64.vic.border = 5;
} else {
c64.console.print("LOAD FAILED\n", .{});
c64.vic.border = 2;
}
}
Carry set means LOAD failed. bcs done skips the store to @loaded, leaving it false. On success the block writes 1. Both paths reach the compiler's restoration sequence. After the wrapper restores the original status and registers, the Boolean cell still carries the outcome. Compare with GETIN returning its result in A.
The supplied disk should print TILE 55. The disk with the same boot program and no tile should print LOAD FAILED. Keep the supplied payload for this exercise. The named RAM reservation keeps other compiler allocations out of staging, while KERNAL LOAD can still write beyond it if given a larger file.
Choose how control leaves the block
Use a local branch or jump to reach a label in the same block. For a conditional branch beyond its signed byte displacement, invert the condition to skip over a local absolute jump. An external jsr must return with a balanced hardware stack so the wrapper can restore the saved registers and flags.
A single absolute jmp $hhhh is the terminal form. It transfers to a documented target and bypasses ordinary ZeroPage return. It is intended for a caller with a host contract, such as the ReadyOS launcher vector. Prepare required memory before the jump. A normal block that precedes it restores registers when it ends. The assembly guide gives the rejected forms and the complete branch-distance example.