Page 1 of 1

Using Assembler and C constants in ZBasic

Posted: 10 February 2008, 18:43 PM
by mikep
This code works just fine

Code: Select all

#asm
	ldi     r18, 0x07 
	out     0x05, r18
#endasm
What I really want to do is use symbolic constants from GCC in the assembler code. The standard GCC code is something like:

Code: Select all

asm volatile("out %0, 0x07" : "I" ((unsigned short)(PORTB)):);
That of course won't work in ZBasic. Can you confirm that this is the correct way of doing it:

Code: Select all

#asm
	ldi     r18, 0x07
	out     %0, r18 
	: 
	: "I" (_SFR_IO_ADDR(PORTB))
	: "r18"	
#endasm
It took a few attempts with generating the intermediate files to figure this all out.

Re: Using Assembler and C constants in ZBasic

Posted: 10 February 2008, 19:18 PM
by dkinzer
mikep wrote:Can you confirm that this is the correct way of doing it
It is indeed. Eventually, we'll need an application note describing the nuances of using inline C and assembly language. Since you're familiar with using avr-gcc, you likely already know everything that is necessary.

There currently isn't a good way to share constant values between ZBasic code and inline assembly code. One way to get the desired effect is to use a variable with a Constant attribute and an initial value.

Code: Select all

Private foo as Byte = &H55 Attribute(Constant)

Sub Main()
  Debug.Print foo
#asm
   out     %0, %1
   : 
   : "I" (_SFR_IO_ADDR(PORTB)), "r" (zv_foo)
#endasm
End Sub
I've given some thought to emitting ZBasic constants as #define preprocessor macros. That may be the best way to accomplish the desired effect.