Author Topic: HOWTO call functions from assembler..  (Read 9387 times)

Offline m_mikula

  • Newbie
  • *
  • Posts: 14
    • View Profile
HOWTO call functions from assembler..
« on: March 25, 2008, 07:08:54 PM »
Hi mark,

How can I call C functions from assemler when REG_ABI is enabled? I want to write my own exception processing function but I dont know how to write this small assembler code:

in m5223X:
Code: [Select]
for (n = 0; n < 256; n++) {
//__VECTOR_RAM[n] = (unsigned long)undef_int;                      // set undefined interrupt for all interrupts at start
__VECTOR_RAM[n] = (unsigned long)asm_exception_handler;
}

added file m5223X_ext:
Code: [Select]
#define GET_FORMAT(ESF)  ((*((unsigned short *)(ESF)) >> 12) & 0x00FF)
#define GET_VECTOR(ESF)   ((*((unsigned short *)(ESF)) >>  2) & 0x00FF)
#define GET_FS(ESF) ( ((*((unsigned short *)(ESF)) & 0x0C00) >> 8) | (*((unsigned short *)(ESF)) & 0x0003) )
#define GET_PC(ESF) *((unsigned long *)(ESF)+1)

void real_exception_handler (unsigned long *esf)
{
unsigned char fmt = GET_FORMAT(esf);
unsigned char vector = GET_VECTOR(esf);
unsigned char fs = GET_FS(esf);
unsigned char epc = GET_PC(esf);

// log this
fnErrorMsg("Exception - FMT: "); fnErrorDec(fmt);
fnErrorMsg(", VECTOR: "); fnErrorDec(vector);
fnErrorMsg(", FS: "); fnErrorDec(fs);
fnErrorMsg(", EPC: "); fnErrorDec(epc);
fnErrorMsg("\n\r");

// test if is critical...
if (isCritical(fmt, vector, fs))
while(1) {}

// else continue with program execution so return from handler...
}

and i dont know write asm_exception_handler in Startup.s
Code: [Select]
asm_exception_handler:
_asm_exception_handler:
// save registers
// load values from SP
    jsr     real_exception_handler // call real handler
// restore registers
    rte // return from handler

please can you help me with missing code?

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: HOWTO call functions from assembler..
« Reply #1 on: March 25, 2008, 08:38:06 PM »
Hi

When calling routines when REG_ABI is enabled, any parameters which need to be passed need to be copied in internal registers - interrupts are more tricky because most registers need to be actively copied to stack to avoid anything getting lost in the process and returned afterwards. Interrupts declared as such are compiled by the C-compiler to respect this so it is a good idea to look at their disassembled code to get some ideas.

When passing parameters to function it is necessary to know the passing strategy to use. For example d0 is usually used for passing the first data value. Returned values can also change between d0 and a0 depending on compiler settings and whether the content is data or an address (pointer).

Again it is probably best to write a small c routine and call it from code - then look at in assembler mode in the debugger, or view it in disassembled form (CW allows this by right click + disassemble). You should quite quickly see the rules used for passing the parameters and you can copy these to get the same thing working in a compatible manor from your own assembler code.

Regards

Mark

Offline m_mikula

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: HOWTO call functions from assembler..
« Reply #2 on: March 26, 2008, 02:55:02 PM »
Hi,

thanks for advice... It seems that this code works: ( if somebody interested :) )

Code: [Select]
asm_exception_handler:
_asm_exception_handler:
    lea     -64(SP),SP                                 // move SP forward 64 bytes (16*4)
    movem.l D0-D7/A0-A7,(SP) // save registers
    lea     64(SP),A0                                  // get value of SP 64 bytes backwarded and store it in A0
    jsr     real_exception_handler                  // call function
    movem.l (SP),D0-D7/A0-A7 // restore registers
    lea     64(SP),SP                                  // move SP back...
    rte

Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: HOWTO call functions from assembler..
« Reply #3 on: March 26, 2008, 05:42:27 PM »
Hi Martin

Thanks for the reference.

What is stored in A0? Is it a reference to the actual interrupt being called, which is being passed to the handling routine?

Regards

Mark

Offline m_mikula

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: HOWTO call functions from assembler..
« Reply #4 on: March 27, 2008, 07:24:29 AM »
There is pointer to "Exception Stack" in A0. So witch a little macros I can easily find out which exception was happend, I don't need to start debugger, but i see all needed informations on my debug uart...

--
Martin