converting pointer to integer

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
sevobal
Member
Member
Posts: 63
Joined: Sun Oct 22, 2006 7:11 am

converting pointer to integer

Post by sevobal »

Hi guys,
I've written the following code to detect if there is the Signature "_32_"

Code: Select all

void get_bios32_signature()
{
  int *bios32 = (int *) 0xE0000;
  unsigned int ads=0;

	while(ads < 1048560)
	{
		if(bios32[ads] == 0x5F32335F)
		{
			signature = 0x5F33325F;
			goto ende;
		}
		ads++;
	}
ende:
  ads = 0;
}
Everything works fine and I found the signature. BUT I also want to get the base address where he found this signature. So I want to convert the pointer into an integer which contains the base addresse where he found it (e.g. 0E00FF or something like this). I'm using gcc.
I hope you can help me with this problem.

THX!
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

ehh... youre ads is an integer and start at E0000 and you want to now the address? :? well eh.. maybe E0000 + ads? :wink:

ok, now for the reasonable answer, the address in c/c++ is &bios32[ads] the & means address off and depending on wheter you use 32-bit or 64-bit addressing you need to store the address in either an int (32-bit) or long long (64-bit).

also don't use goto it is bad programming practise. you could use the break statement which would jump out of the while loop.

schuss und aufwieder sehen (based on the ende).
Author of COBOS
mara
Posts: 10
Joined: Sun Nov 07, 2004 12:00 am

Re: converting pointer to integer

Post by mara »

sevobal wrote:Hi guys,
I've written the following code to detect if there is the Signature "_32_"

Code: Select all

void get_bios32_signature()
{
  int *bios32 = (int *) 0xE0000;
  unsigned int ads=0;

	while(ads < 1048560)
	{
		if(bios32[ads] == 0x5F32335F)
		{
			signature = 0x5F33325F;
			goto ende;
		}
		ads++;
	}
ende:
  ads = 0;
}
Everything works fine and I found the signature. BUT I also want to get the base address where he found this signature. So I want to convert the pointer into an integer which contains the base addresse where he found it (e.g. 0E00FF or something like this). I'm using gcc.
I hope you can help me with this problem.

THX!
Hi,
It is in general a bad idea to convert a pointer into an integer but use a pointer. But, if you insist, you can code this:

Code: Select all

int int_adr;
int_adr=(int)(bios32+ads);
You may consider changing you code to this:

Code: Select all

#define BIOS32_ADDR 0xE0000;

void get_bios32_signature()
{
  int *access_ptr = (int *) BIOS32_ADDR;

	while(access_ptr < (1048560+BIOS32_ADDR))
	{
		if(*access_ptr == 0x5F32335F)
		{
			signature = 0x5F33325F;
			break;
		}
		ads++;
	}
/* now "access_ptr" contains the adress */
}
See ya later.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

the most obvious way to get the address is to

Code: Select all

 void * address = &(bios[ads]); 
which gives you a pointer to where the information is stored.
If you want to get the address as an integer, you'd simply have to cast it:

Code: Select all

 unsigned int address_num = (unsigned int) address; 
(note the unsigned int, there are no negative memory locations)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
sevobal
Member
Member
Posts: 63
Joined: Sun Oct 22, 2006 7:11 am

Post by sevobal »

Hey thanks to all of you. Now everything works fine.
There is an other litlle question for people who knows the inline asm of gcc (I don't want to start a new topic for this little thing). How can I perform a far call? I tried this, but get an error from the inline assembler:

Code: Select all

asm("movl $code, %eax; movl 0x00, %ebx; call far $entry_point;");
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post by iammisc »

call far is intel syntax use lcall.
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

if you have access to some standard headers (if not, you should make sure you do), use intptr_t instead of just int
there is no guarantee what-so-ever that int == address size in C
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

The problem with converting a pointer to an integer is that the code become no longer portable acrross processors. (i.e. porting from a 32it address space to a 64 bit address space.)

Code: Select all

void get_bios32_signature()
{
   unsigned int *bios32 = (unsigned int *) 0xE0000;
   unsigned int *bios32end = bios32 + 1048560;
   for(;(bios32 < bios32end) && (*bios32 !=  0x5F32335F);++bios32);

   if (*bios32 !=  0x5F32335F){
      signature = *bios32; 
      // variable bios32 now contains the address where it '_32_' was found.
   }
}
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

B.E wrote:The problem with converting a pointer to an integer is that the code become no longer portable acrross processors. (i.e. porting from a 32it address space to a 64 bit address space.)
That's why i declared the type vintp to hold a pointer in an integer and based on the platform i change the declaration of the integer.

Code: Select all

#ifdef BIT32
typedef int vintp;
#endif
#ifdef BIT64
typedef long vintp;
#endif
Author of COBOS
oscoder
Member
Member
Posts: 59
Joined: Mon Mar 27, 2006 12:00 am
Location: UK

Post by oscoder »

sevobal wrote:Hey thanks to all of you. Now everything works fine.
There is an other litlle question for people who knows the inline asm of gcc (I don't want to start a new topic for this little thing). How can I perform a far call? I tried this, but get an error from the inline assembler:

Code: Select all

asm("movl $code, %eax; movl 0x00, %ebx; call far $entry_point;");
What kind of error are you getting? Aside from the fact that you need to use 'lcall', I think you must supply an offset as well as a segment address.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

os64dev wrote:
B.E wrote:The problem with converting a pointer to an integer is that the code become no longer portable acrross processors. (i.e. porting from a 32it address space to a 64 bit address space.)
That's why i declared the type vintp to hold a pointer in an integer and based on the platform i change the declaration of the integer.

Code: Select all

#ifdef BIT32
typedef int vintp;
#endif
#ifdef BIT64
typedef long vintp;
#endif
*cough* intptr_t in stdint.h *cough*
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

Candy wrote:*cough* intptr_t in stdint.h *cough*
Me don't have libc, me don't want libc :twisted:
Author of COBOS
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

stdint.h does not require a libc
Mikey
Posts: 1
Joined: Mon Apr 16, 2007 11:29 am

Post by Mikey »

os64dev wrote:
Candy wrote:*cough* intptr_t in stdint.h *cough*
Me don't have libc, me don't want libc :twisted:
faggot!, go programme in Visual Basic you homosexual! :twisted:
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

Aali wrote:stdint.h does not require a libc
True, but it is part of the C99 standard library and because i am using the C++98 it is not applicable to me.
Mikey wrote:faggot!, go programme in Visual Basic you homosexual! :twisted:
Way to go for a first post you stupid git. Your intelligence level clearly is zero if you think that the only alternative to C is Visual Basic. Second to that disrespecting the gay communitie twice in one sentence is only an affermation of the lack of Intelligence. People like you should be removed from the gene-pool and given the Darwin award.
Author of COBOS
Post Reply