This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: poke function ....


>> /* pokeb(char *base_addr, int offset, char data) */
>>         .global pokeb
>> pokeb:
>>         move.l  4(%sp),%a0      /* get base_addr */
>>         add.l   8(%sp),%a0      /* add offset */
>>         move.b  15(%sp),(%a0)   /* move data byte */
>>         rts
>> 
>> Art
>
>Hii,
>Can anybody tell me how the argument will be passed into stack for
>function pokeb(char *base_addr, int offset, char data)??
>Is it like below??
>
>data highest byte (3rd arg, 4th byte)		->	sp+15
>data high byte (3rd arg, 3rd byte)		->	sp+14
>data low byte (3rd arg, 2nd byte)		->	sp+13
>data lowest byte (3rd arg, 1st byte)		->	sp+12
>offset highest byte (2nd arg, 4th byte)		->	sp+11
>offset high byte (2nd arg, 3rd byte)		->	sp+10
>offset low byte (2nd arg, 2nd byte)		->	sp+9
>offset lowest byte (2nd arg, 1 byte)		->	sp+8
>base_addr highest byte (1st arg, 4th byte)	->	sp+7
>base_addr high byte (1st arg, 3rd byte)		->	sp+6
>base_addr low byte (1st arg, 2nd byte)		->	sp+5
>base_addr lowest byte (1st arg, 1st byte)	->	sp+4
>sp highest byte					->	sp+3
>sp high byte					->	sp+2
>sp low byte					->	sp+1
>sp lowest byte					->	sp+0

Since the m68k is a big-endian machine, you've got the order of bytes
within a 32 bit word wrong.  And the first thing on the stack will
be the return PC address, not the old stack pointer.

In the m68k calling interface, arguments are pushed onto the stack
from right to left (so that the first argument will be the smallest
offset).  Arguments that are 8 or 16 bits are extended to 32 bits
before being pushed onto the stack (to maintain a 32 bit stack
alignment).  Finally the subroutine is called with a jsr instruction
which pushes the PC address following the jsr onto the stack.  The
called function is expected to preserve the contents of D2-D7 and
A2-A6.  Registers D0, D1, A0, and A1 may be modified.  Function
return values will be returned in D0 (and D1 if between 32 and 64
bits).  So, on your call the stack should look like:

	   MSB			   LSB
	   +0	   +1      +2      +3
	+-------+-------+-------+-------+
 SP+12	|   data (extended to 32 bits)	|
	+-------+-------+-------+-------+
 SP+8	|            offset		|
	+-------+-------+-------+-------+
 SP+4	|	   base_addr		|
	+-------+-------+-------+-------+
 SP->	|	return PC addr		|
	+-------+-------+-------+-------+


Of course as everyone has pointed out, you ought to just use a pointer
under C.

inline void pokeb(char *base_addr, int offset, char data)
{
	*(base_addr+offset) = data;
}

Art