Using Assembler and C constants in ZBasic

A private (members-only) forum for discussing all issues related to the Beta test of Native mode devices.
Locked
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Using Assembler and C constants in ZBasic

Post 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.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Using Assembler and C constants in ZBasic

Post 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.
- Don Kinzer
Locked