linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn@gclements.plus.com>
To: nanakos@wired-net.gr
Cc: linux-c-programming@vger.kernel.org
Subject: Re: x86 Endiannes and libc printf
Date: Sat, 9 Jul 2005 18:43:02 +0100	[thread overview]
Message-ID: <17104.3238.592473.67933@gargle.gargle.HOWL> (raw)
In-Reply-To: <55800.62.38.141.148.1120762110.squirrel@webmail.wired-net.gr>


Nanakos Chrysostomos wrote:

> i am searching for a few hours now the endianness in the x86
> environment,and i have the following
> snippets of code,which in some places i cant understand.Please help me!!!
> 
> endian.c
> ---------
> #include <stdio.h>
> #include <fcntl.h>
> #include <sys/types.h>
> 
> 
> int main()
> {
>         char *filename= "endian.txt";
>         unsigned long buf;
>         char *k=(char *)&buf;
>         int fd;
> 
>         fd = open("makis",O_RDONLY);
> 
>         read(fd,&buf,4);
> 
>         printf("%.4s\n",&buf);
>         printf("%p\n",buf);
>         printf("&buf: %p %#x %p\n",&buf,*k,k);
>         return 0;
> }
> 
> endian.txt
> ----------
> DBCA
> 
> 
> #./read
> DCBA
> 0x41424344
> &buf: 0xbffff8b0 0x44 0xbffff8b0
> #
> 
> 
> In the first printf everything is fine.In the second printf we see that
> the 0x44,0x43,0x42,0x41 byte-data is printed in the revserse order,while
> we can see
> that in memory it is in the right order after the read system call.Why
> this happens?Is it being internal in printf???

No. x86 is little-endian, which means that multi-byte integers are
stored in memory with the lowest byte first, e.g. the integer
0x41424344 (1094861636 decimal) is stored 0x44,0x43,0x42,0x41.

> The same as above.Should i assume that an internal operation in printf is
> doing this???

No. It's the way that the CPU reads/writes integers to/from memory.

> I also used the above assembly example,to see what
> happens.Memory-to-memory movements (with push & pop) dont inherit the
> little-endian way.Is this
> happens only from memory-to-register and the opposite????

Yes. Endianness doesn't matter if you are just moving bytes around. It
only matters if you are treating a block of 2/4/8 bytes as a
16/32/64-bit integer.

-- 
Glynn Clements <glynn@gclements.plus.com>

  reply	other threads:[~2005-07-09 17:43 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-06 20:15 process information Matías Aguirre
2005-07-07  4:46 ` sumit kalra
2005-07-07 12:09 ` Luiz Fernando Capitulino
2005-07-07 16:26   ` Matías Aguirre
2005-07-08 19:48     ` Luiz Fernando Capitulino
2005-07-07 18:48   ` x86 Endiannes and libc printf Nanakos Chrysostomos
2005-07-09 17:43     ` Glynn Clements [this message]
     [not found] <42117.62.38.142.34.1120849517.squirrel@webmail.wired-net.gr>
     [not found] ` <20050709101937.0aad4448@localhost>
2005-07-09 14:41   ` Nanakos Chrysostomos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=17104.3238.592473.67933@gargle.gargle.HOWL \
    --to=glynn@gclements.plus.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=nanakos@wired-net.gr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).