gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > function pointer not correct?

#170405 - Tommmie - Mon Sep 21, 2009 7:23 pm

Hey everyone,

I was executing this piece of code(it prints function pointers):

Code:
#include <nds.h>
#include <stdio.h>


int func1(int a)
{
 
   return a;
}
 
char func2(char b)
{

   return 'b';
}
 
int main()
{

   consoleDemoInit();
   fp ptr1;
   
   int temp1;
   char temp2;
   
 
   ptr1 = (fp) func1;
   iprintf("pointer1 is: %d \n", (int)ptr1);
   temp1 = ((int (*)(int)) ptr1)(1);
   
   ptr1 = (fp) func2;
   iprintf("pointer2 is: %d \n", (int)ptr1);
   temp2 = ((char (*)(char))ptr1)('a');
   
   printf("Received %d %c\n", temp1, temp2);
   return 0;
}

and I noticed that the offsets which were displayed are not correctly, almost but not correct:(I looked in the .map file for the real offsets):

real offsets:
func1: 0x0200037c
func2: 0x02000380


results of the program:

func1: 0x0200037D
func2: 0x02000381

but why are the addresses just 1 more? can someone explain it?

thanks,

Tommmie[/code]

#170408 - Min - Mon Sep 21, 2009 8:36 pm

The ARM9 ( and ARM7 ) processor used by the DS has two different sets of instructions; a 32 bit instruction set and a smaller 16 bit instruction set called Thumb. Your functions are compiled into Thumb code by default. The least significant bit of the address is used to tell the processor which instruction set to use, the bit is set for Thumb code thats why the addresses of your functions appear to be +1.

#170426 - Tommmie - Tue Sep 22, 2009 1:32 pm

thanks min for the answer, I did not know that:)

(and about streaming, i got it working. I was so stupid that i didn't understood it:) now i have seeking pause etc. working)