Which registers should be pushed in a naked ISR

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

Which registers should be pushed in a naked ISR

Post by mikep »

If you don't use a naked ISR, then the compiler automatically pushes and pops all of the required 15 registers (r0, r1, SREG, r18-r27, r30, and r31).

Presumably you would write a naked ISR if you wanted to make the ISR faster. The problem comes in when you mix a naked ISRs with ZBasic code and knowing which registers to push. For example:

Code: Select all

ISR Timer1_CompB() Attribute(Naked)
#asm
	; push registers here
#endasm
	Call PutPin(D.7, 0) ' which registers are touched by this call
#asm
	; pop registers here    
	reti            ; always needed
#endasm
End ISR
Through careful research it would appear that PutPin requires R22 and R24 and also uses r16 and r25. The question is how can this be determined easily.

Perhaps it should be a compiler warning/error to use ZBasic functions inside a native ISR - it is too dangerous.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Which registers should be pushed in a naked ISR

Post by dkinzer »

mikep wrote:t would appear that PutPin requires R22 and R24 and also uses r16 and r25.
Unless we've overlooked a coding error, the ZX Library routine putPin() doesn't modify r16. If you were looking at the generated code, you may have seen someting like the code below near the putPin() code. This is actually a data table rather than being code so the disassembly is meaningless.

Code: Select all

00000fc0 <maskTable>&#58;
     fc0&#58;	01 02   muls  r16, r17
     fc2&#58;	04 08   sbc   r0, r4
     fc4&#58;	10 20   and   r1, r0
     fc6&#58;	40 80   ld    r4, Z
The ZX Library routines observe the avr-gcc convention of preserving r1-17, r28 and r29. All other registers may be modified due to a call.
mikep wrote:Perhaps it should be a compiler warning/error to use ZBasic functions inside a [naked] ISR - it is too dangerous.
Perhaps. There are many other things that you can do in native mode that are dangerous, too, such as using assembly language or C code. Generally speaking, a Naked ISR should written mostly, if not entirely, in assembly language.
- Don Kinzer
Locked