Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: c programming q

Author: Robert Hyatt

Date: 16:58:03 05/11/03

Go up one level in this thread


On May 11, 2003 at 17:24:41, Gareth McCaughan wrote:

>Robert Hyatt wrote:
>
>>>> left-to-right means "ascending address order"...
>>>
>>> If that's what you meant by it, then that bit was correct.
>>> But "from will be the MSbyte, piece will be the LSbyte"
>>> is guaranteed only if you know the architecture the
>>> thing is being compiled for. Ascending address order
>>> needn't mean most significant byte first.
>>>
>>
>> In the context given, my answer is correct.  He put four chars into a
>> struct. The first char will definitely be the "MSB" which is the left-most
>> byte or the byte with the lowest memory address.  The second char will follow
>> sequentially...
>>
>>There can be no exception to that.
>
>"Most significant" doesn't mean "lowest address". It means "represents
>the largest power of 256 when viewed as an integer". And, as I said,
>on ia32 the most significant byte has the *highest* address within
>a word.

THat's correct.  And when you put four bytes into a word, which is a memory
unit of four consecutive bytes, the first byte _always_ goes in the leftmost
byte of that word, with _no_ exceptions.  Note that I am _specifically_ talking
about this case and _only_ this case:

struct {

  char a;
  char b;
  char c;
  char d;

}


Wherever the struct is located in memory, a will always be at address + 0,
b at address + 1, etc.  That is what I would call "MSB to LSB order within
the structure."

As I said clearly, I am not stepping into endianness issues whatsoever here,
as it is meaningless in the above struct context.

>
>$ cat endian.c
>#include <stdio.h>
>int main(void) {
>  union { struct { char a,b,c,d; }; int i; } x;
>  x.i = 0x01020304;
>  printf("%d %d\n", x.a, x.b);
>  return 0;
>}
>$ gcc endian.c
>$ ./a.out
>4 3
>
>The first char in the struct gets the lowest address, and
>the *least* significant byte of the integer.

However, that wasn't the point.  And I clearly mentioned that endianness
didn't figure in since we were not talking about referencing the struct as
a single word for any 32 bit operations...

If you change to that "gear" then you are correct for little-endian machines
like the PC.




>
>>> In fact, on ia32 systems it's exactly the other way around,
>>> so if Zach's code is going to run on a PC then "from" will
>>> be the least significant byte and "piece" the most.
>>
>> Only if you load the entire thing at one "gulp" from something like
>> mov  eax, struct_addr.
>
>If you don't do that, then the terms "least significant" and
>"most significant" don't mean anything.

In a sense, you are correct.  But in the context of low to high addresses,
it doesn't matter in this example.


>
>>>>> There may be padding anywhere within the structure, including
>>>>> between those single-byte fields.
>>>
>>> Not that this is likely to be a problem in practice, in this
>>> particular case.
>>
>> I'm not aware of padding between chars.  It would break _everything_.
>
>I'm fairly sure it's legal, even so. But it doesn't happen in practice.


I don't think it is legal for chars.  Otherwise compatibility between C and
other languages would be non-existant...




>
>--
>g



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.