124 of 208
ZBasic Language Reference 116 ZX Microcontroller Family Program Element Prefixes Element Prefix (if Public) Prefix (if Private) subroutine or function zf_ mzf_ variable zv_ mzv_ structure zs_ mzs_ structure member zm_ zm_ parameter zp_ function return value zr_ Accessing ZBasic program elements in inline assembly code is possible but is more complicated partly due to issues regarding the register allocation, code generation and optimization stragegies employed by the compiler that translates the generated C code to native object code, discussion of which is beyond the scope of this document. That said, here is a simple example using inline assembly code. Public b as Byte #c char ch; #endc Sub Main() b = 5 #asm ; save the registers used push r30 push r31 push r24 ; load the address of the variable ldi r30, lo8(zv_b) ldi r31, hi8(zv_b) ; load the variable's value and save it ld r24, Z sts ch, r24 ; restore the registers used pop r24 pop r31 pop r30 #endasm End Sub It is important to note that inline assembly code must avoid altering certain registers that the compiler expects to remain unchanged. (The same is true, of course, of any assembly language code.) The code in the example above saves registers that it uses on the stack before using them and then restores their values afterward. There is a mechanism to inform the compiler as to which registers are altered in the inline assembly code thus allowing the compiler to automatically perform the save/restore if necessary. Discussion of that mechanism is an advanced topic that is beyond the scope of this document. 5.2 Defining and Using External Subroutines, Functions and Variables For the native mode devices you may include .c files (C language), .S files (AVR assembly language), .o files (AVR object code) and .a files (AVR object code archives) in your project. When processing the component files of a project, the ZBasic compiler will note the presence of these special files but will otherwise ignore them except for including them in the final build phase. In order to invoke subroutines and functions contained in these special files, you must define them so the ZBasic compiler knows how to check for invocation syntax errors and how to generate code to invoke them.