I am playing around SH4 assembly function call in C today.
My goal is simply to use assembly inlined code in a C program to call a function of KOS.
Below you can see a sample of calling a function in pure C.
The "_icache_flush_range" is used as a test function, calling it with 0 / 0 parameters produce nothing.
I use some pragma to not be annoyed by any optimization.
So this C code works perfectly.
Code: Select all
#pragma GCC push_options
#pragma GCC optimize ("O0")
int main(int argc, char* argv[])
{
icache_flush_range(0, 0);
return 0;
}
You can see a sample of my assembly code below.
I just take r4 and r5 as parameters containers then push the "_icache_flush_range" address in r1 before jump to it.
After execution of the function, I made a BRA to not execute accidentally the words storing the function address.
Code: Select all
8c010252: 00 e4 mov #0,r4
8c010254: 00 e5 mov #0,r5
8c010256: 02 d1 mov.l 8c010260 <_main+0x20>,r1 ! 8c028720 <_icache_flush_range>
8c010258: 0b 41 jsr @r1
8c01025a: 09 00 nop
8c01025c: 05 a0 bra 8c01026a <END>
8c01025e: 09 00 nop
8c010260: 20 87 .word 0x8720
8c010262: 02 8c .word 0x8c02
8c010264: 09 00 nop
8c010266: 09 00 nop
8c010268: 09 00 nop
8c01026a <END>:
Code: Select all
#pragma GCC push_options
#pragma GCC optimize ("O0")
int main(int argc, char* argv[])
{
__asm__ __volatile__(".BYTE 0x00");__asm__ __volatile__(".BYTE 0xe4");
__asm__ __volatile__(".BYTE 0x00");__asm__ __volatile__(".BYTE 0xe5");
__asm__ __volatile__(".BYTE 0x02");__asm__ __volatile__(".BYTE 0xd1");
__asm__ __volatile__(".BYTE 0x0b");__asm__ __volatile__(".BYTE 0x41");
__asm__ __volatile__(".BYTE 0x09");__asm__ __volatile__(".BYTE 0x00");
__asm__ __volatile__("BRA END");
__asm__ __volatile__(".BYTE 0x09");__asm__ __volatile__(".BYTE 0x00");
__asm__ __volatile__(".BYTE 0x20");__asm__ __volatile__(".BYTE 0x87");
__asm__ __volatile__(".BYTE 0x02");__asm__ __volatile__(".BYTE 0x8c");
__asm__ __volatile__(".BYTE 0x09");__asm__ __volatile__(".BYTE 0x00");
__asm__ __volatile__(".BYTE 0x09");__asm__ __volatile__(".BYTE 0x00");
__asm__ __volatile__(".BYTE 0x09");__asm__ __volatile__(".BYTE 0x00");
__asm__ __volatile__("END:");
return 0;
}
I certainly missing something important (one or several instructions) but i did not found information about it in the Hitachi manuals.
Unhandled exception: PC 8c010278, code 1, evt 00e0
R0-R7: 00000000 00000000 00001fe0 fffffc00 00000020 00000000 f0000000 00000000
R8-R15: 00000000 00000000 00000000 00000000 00000000 00000000 00000007 00000007
SR 40000101 PR 8c01025c
Stack Trace: frame pointers not enabled!
kernel panic: unhandled IRQ/Exception
arch: aborting the system
Thanks for any kind of help.