OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 10:32 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: converting pointer to integer
PostPosted: Mon Mar 26, 2007 4:26 am 
Offline
Member
Member

Joined: Sun Oct 22, 2006 7:11 am
Posts: 63
Hi guys,
I've written the following code to detect if there is the Signature "_32_"
Code:
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!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 4:42 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 27, 2007 3:21 pm
Posts: 553
Location: Best, Netherlands
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


Top
 Profile  
 
 Post subject: Re: converting pointer to integer
PostPosted: Mon Mar 26, 2007 4:56 am 
Offline

Joined: Sun Nov 07, 2004 12:00 am
Posts: 10
sevobal wrote:
Hi guys,
I've written the following code to detect if there is the Signature "_32_"
Code:
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:
int int_adr;
int_adr=(int)(bios32+ads);


You may consider changing you code to this:
Code:
#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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 5:44 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
the most obvious way to get the address is to
Code:
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:
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 ]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 8:53 am 
Offline
Member
Member

Joined: Sun Oct 22, 2006 7:11 am
Posts: 63
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:
asm("movl $code, %eax; movl 0x00, %ebx; call far $entry_point;");


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 9:50 am 
Offline
Member
Member

Joined: Thu Nov 09, 2006 6:23 pm
Posts: 269
call far is intel syntax use lcall.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 8:56 am 
Offline
Member
Member

Joined: Sat Apr 14, 2007 12:13 pm
Posts: 58
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 2:48 pm 
Offline
Member
Member
User avatar

Joined: Sat Oct 21, 2006 5:29 pm
Posts: 275
Location: Brisbane Australia
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:
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"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 1:39 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 27, 2007 3:21 pm
Posts: 553
Location: Best, Netherlands
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:
#ifdef BIT32
typedef int vintp;
#endif
#ifdef BIT64
typedef long vintp;
#endif

_________________
Author of COBOS


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 12:51 pm 
Offline
Member
Member

Joined: Mon Mar 27, 2006 12:00 am
Posts: 59
Location: UK
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:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 2:13 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
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:
#ifdef BIT32
typedef int vintp;
#endif
#ifdef BIT64
typedef long vintp;
#endif


*cough* intptr_t in stdint.h *cough*


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 11:52 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 27, 2007 3:21 pm
Posts: 553
Location: Best, Netherlands
Candy wrote:
*cough* intptr_t in stdint.h *cough*

Me don't have libc, me don't want libc :twisted:

_________________
Author of COBOS


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 12:30 am 
Offline
Member
Member

Joined: Sat Apr 14, 2007 12:13 pm
Posts: 58
stdint.h does not require a libc


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 12:32 am 
Offline

Joined: Mon Apr 16, 2007 11:29 am
Posts: 1
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:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 12:54 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 27, 2007 3:21 pm
Posts: 553
Location: Best, Netherlands
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 218 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group