Assembly files
Write a routine in assembly when you need speed or direct hardware access, then call it from Python like any other function. This works on the CE and classic targets, but not in Flash Apps.
A first routine
Put fastmath.asm next to your Python file:
; tipy: def add3(a, b, c)
add3:
add hl,de
add hl,bc
ret
Then import it:
from fastmath import add3
total = add3(1, 2, 3)
The ; tipy: def line declares the routine and its parameters. Only declared
routines can be imported, and each needs a label with the same name.
Calling convention
What |
Where |
|---|---|
First argument |
|
Second argument |
|
Third argument |
|
Result |
|
A routine can use every other register, but it must:
preserve
IY, because the OS uses it for its flags;return with
ret, leaving the stack as it found it.
Integers are 24-bit on CE and 16-bit on classic, so the arguments fill the whole
register on each target. A routine takes at most three parameters. Calls accept
keyword arguments and as aliases, and from fastmath import * imports every
declared routine.
Finding the file
For from fastmath import add3, the compiler looks next to the main file for, in
order:
fastmath.ce.asmorfastmath.classic.asm, for the target being builtfastmath.ce.sorfastmath.classic.sfastmath.asmfastmath.s
Dotted names reach into folders, so from lib.fastmath import add3 reads
lib/fastmath.asm.
CE files are assembled by fasmg and classic files by SPASM-ng, and their syntax differs.
When it matters, write one file for each target. Simple instructions such as
add hl,de mean the same on both, so one file can serve both targets.
Rules
Do not include
ti83plus.incor the CE include files. Every program already has them, soti.names work on CE andbcallworks on classic.Labels starting with
_orvar_, and the labelsstartandprogram_exit, belong to TIpy-asm. Use other names.Each file is included once, after the program’s own code, however many files import from it.
A routine and a Python function cannot share a name.
Editor support
Editors do not know about assembly modules. To stop them flagging the import, add a
stub, fastmath.pyi, next to the file:
def add3(a: int, b: int, c: int) -> int: ...
To see the code the compiler generated around your routine, build with --keep-asm.
Example
examples/asm_demo in the repository has one file shared by both targets,
fastbits.asm, and a screen routine written separately for each target, in
screen.ce.asm and screen.classic.asm. It also includes the editor stubs.