Domanda

Test is on Linux 32bit:

I found a bug in my C code and I simplify the code and put it here:

#define al *(char*)(eax_ptr)
int eax = 0;
int *eax_ptr = &eax;
int edx = 0;
char hh = 254;
.......

eax = hh;
edx = al;

The problem is that, edx should be 254 but where I use gdb to debug, I fould edx equal zero.

Could anyone give me some help on this problem..?

Thank you!

È stato utile?

Soluzione

The result of this code is not predictable. The initialization

char hh = 254;

Is not assured to be successful on every platform. char may not be able to hold 254 (and it is not if your platform uses signed chars and 8 bit characters, a common choice).

Assuming that a char can hold a value such as 254, this assignment:

eax = hh;

Will expand hh to the size of an integer by inserting leading zeros. When you later interpret eax (an integer) as if it were a char (which is really what you're doing with the cast), the result will depend on your machine's endianness: if it's little endian, it will read 254, otherwise, it will read 0.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top