C <-> ASM

Since we do not have native C compilers on the Oric, this forum will be mostly be used by people using CC65 or the OSDK. But any general C related post will be welcome !
User avatar
Hialmar
Flight Lieutenant
Posts: 349
Joined: Tue Mar 04, 2014 11:25 am
Location: Toulouse, France
Contact:

C <-> ASM

Post by Hialmar »

When you call some ASM from C, the parameters are on the stack and you use code like this to get them:

Code: Select all

	ldy #0
	lda (sp),y ; address lo
	sta tmp0
	iny
	lda (sp),y ; address hi
	sta tmp0+1
       ...
But for returning data, I thought you had to put the low byte in A and the high byte in X but it looks like it is the reverse.
Or is it that when returning a single byte (a char for example) you need to put it in X ?

Can someone explain this to me ?

Thanks.
Hialmar
CEO and Silicium member.
User avatar
iss
Wing Commander
Posts: 1637
Joined: Sat Apr 03, 2010 5:43 pm
Location: Bulgaria
Contact:

Re: C <-> ASM

Post by iss »

With OSDK to return 0x1234:

Code: Select all

lda #$12
ldx #$34
rts
to return 1 byte use register X.

The same thing when using CC65 is the opposite

Code: Select all

ldx #$12
lda #$34
rts
to return 1 byte use register A.

So, both are correct but depending what tool-chain is used ;)
User avatar
Hialmar
Flight Lieutenant
Posts: 349
Joined: Tue Mar 04, 2014 11:25 am
Location: Toulouse, France
Contact:

Re: C <-> ASM

Post by Hialmar »

Ah ok thanks a lot :)
Hialmar
CEO and Silicium member.
User avatar
coco.oric
Squad Leader
Posts: 720
Joined: Tue Aug 11, 2009 9:50 am
Location: North of France
Contact:

Re: C <-> ASM

Post by coco.oric »

@iss :
As the low and high values are inverted on rts assembler routine, can you tell us if it the same way to pass parameters from osdk to ass compared to cc65 to assembler code.
coco.oric as DidierV, CEO Member
Historic owner of Oric, Apple II, Atari ST, Amiga
User avatar
Hialmar
Flight Lieutenant
Posts: 349
Joined: Tue Mar 04, 2014 11:25 am
Location: Toulouse, France
Contact:

Re: C <-> ASM

Post by Hialmar »

I suppose that on the stack values are stored in Little Endian mode as is normal for a 6502.

The thing is that there is no order in registers so telling that X is before A or the reverse is only a matter of arbitrary choice.
Hialmar
CEO and Silicium member.
User avatar
iss
Wing Commander
Posts: 1637
Joined: Sat Apr 03, 2010 5:43 pm
Location: Bulgaria
Contact:

Re: C <-> ASM

Post by iss »

Passing parameters from C to assembler is the same in OSDK and CC65.
Parameters can be accessed using the stack pointer sp in the same way in OSDK and CC65.
Note: sp is NOT the processor's stack pointer (i.e. the one in memory range $100-$1FF)!

The only difference is that in CC65 you have to properly align the 'sp' before returning from the assembler routine.
For this are predefined functions 'incsp1', 'incsp2', 'incsp3'. etc. The number represents the sum of all parameters in bytes.
Post Reply