How to implement SignExtend() function in C ?

Programming, for all ages and all languages.
Post Reply
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

How to implement SignExtend() function in C ?

Post by kestaz »

I looking how to implement SignExtend function in C:
EIP = EIP + SignExtend(Destination);
I read it in intel manuals. Intel manuals has this function explanation but still don't know .. So how ?
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:

Re: How to implement SignExtend() function in C ?

Post by Combuster »

What about the explanation don't you get?

Since you're probably talking about emulating some x86 hardware, you basically add a signed byte to an int. Sign extension happens automatically in C once you convert between the correct types.
"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 ]
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

Re: How to implement SignExtend() function in C ?

Post by kestaz »

Just SET most significant byte ?
a = (a & 0x8000) ?

One man do to implement such function like this

unsigned char c ;

signed extended = -c ; ? Does this correct ?
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

Re: How to implement SignExtend() function in C ?

Post by kestaz »

i was not right like this: a |= 0x8000 ;
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: How to implement SignExtend() function in C ?

Post by bewing »

Code: Select all

void foobar()
{
	uint16 i;
	int64 s;
	i = 45000;
	s = (int64) i;		// sign extension happens automatically -- s is negative
}
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

Re: How to implement SignExtend() function in C ?

Post by kestaz »

Ok i wrote this:

void emulator::jcc8(unsigned char rel8)
{
short offset = rel8 ; // sign extended
ip = (ip + offset - 1) & 0xFFFF;
}
Post Reply