Page 1 of 2
converting pointer to integer
Posted: Mon Mar 26, 2007 4:26 am
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!
Posted: Mon Mar 26, 2007 4:42 am
by os64dev
ehh... youre
ads is an integer and start at E0000 and you want to now the address?
well eh.. maybe E0000 + ads?
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).
Re: converting pointer to integer
Posted: Mon Mar 26, 2007 4:56 am
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.
Posted: Mon Mar 26, 2007 5:44 am
by Combuster
the most obvious way to get the address is to
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)
Posted: Mon Mar 26, 2007 8:53 am
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;");
Posted: Mon Mar 26, 2007 9:50 am
by iammisc
call far is intel syntax use lcall.
Posted: Sun Apr 15, 2007 8:56 am
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
Posted: Sun Apr 15, 2007 2:48 pm
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.
}
}
Posted: Mon Apr 16, 2007 1:39 am
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
Posted: Mon Apr 16, 2007 12:51 pm
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.
Posted: Tue Apr 17, 2007 2:13 pm
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*
Posted: Tue Apr 17, 2007 11:52 pm
by os64dev
Candy wrote:*cough* intptr_t in stdint.h *cough*
Me don't have libc, me don't want libc
Posted: Wed Apr 18, 2007 12:30 am
by Aali
stdint.h does not require a libc
Posted: Wed Apr 18, 2007 12:32 am
by Mikey
os64dev wrote:Candy wrote:*cough* intptr_t in stdint.h *cough*
Me don't have libc, me don't want libc
faggot!, go programme in Visual Basic you homosexual!
Posted: Wed Apr 18, 2007 12:54 am
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!
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.