From: Robert Plantz <plantz@sonoma.edu>
To: nanakos@wired-net.gr
Cc: Paolo Ornati <ornati@fastwebnet.it>, linux-assembly@vger.kernel.org
Subject: Re: x86 Endiannes and libc printf
Date: Sat, 09 Jul 2005 07:51:08 -0700 [thread overview]
Message-ID: <1120920668.8305.17.camel@localhost.localdomain> (raw)
In-Reply-To: <58406.62.38.142.246.1120920114.squirrel@webmail.wired-net.gr>
The write system call function is at a lower level than printf. The job
of printf is to convert C data types to character strings for display.
That is, the decimal integer 123 is converted to the characters '1',
'2', '3', and '\0' (the null character).
This character string is sent to write, which writes one byte
(character) at a time to standard out (usually the screen).
printf must take endianness into account when doing its type
conversions.
write is at a much lower level. It knows nothing about data types or
endianess. I simply writes one byte at a time to the specified file
descriptor.
Bob
On Sat, 2005-07-09 at 17:41 +0300, Nanakos Chrysostomos wrote:
> Thanks very much for your response.
> I know that x86 CPU is little endian,but how the printf prints out the
> that number which starts from the first lowest byte in mem /stack which is
> 0x44??
> Maybe internally knows that this is a cast???
> Lest assume the following example:
> #include <stdio.h>
>
> int main(void) {
> unsigned int buf = {0x44,0x43,0x42,0x41};
> unsigned char *x = (unsigned char*)&buf;
> printf("%#x\n", *(int *)x);
> return 0;
> }
>
> prints out the buf array in reverse order ,little-ednian, and treats it as
> a number,
>
> 0x41424344
>
> But check out the following example:
>
> endian.txt
> -----------
> DBCA
> .section .data
> .filename: .string "endian.txt"
>
>
> .text
> .globl _start
>
> _start:
> pushl %ebp
> movl %esp,%ebp
>
> movl $5,%eax
> movl $.filename,%ebx
> movl $0x00,%ecx
> int $0x80
>
> movl %eax,%ebx
> movl $3,%eax
> leal -8(%ebp),%ecx
> movl $4,%edx
> int $0x80
>
> movl $4,%eax
> movl $1,%ebx
> movl $0x0a,-4(%ebp)
> leal -8(%ebp),%ecx
> movl $5,%edx
> int $0x80
>
> movl $1,%eax
> movl $0,%ebx
> int $0x80
>
>
> #as -o example.o example.s
> #ld -o example example.o
> #./example
> DBCA
>
> We print out the memory from the lowest byte-order.
> How can we print out by using the system call 'write' this byte-order and
> treat it like a number,as printf does.????????????????????????????????
next prev parent reply other threads:[~2005-07-09 14:51 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-08 19:05 x86 Endiannes and libc printf Nanakos Chrysostomos
2005-07-08 19:25 ` Vadim Lobanov
2005-07-08 20:17 ` Richard B. Johnson
2005-07-09 8:19 ` Paolo Ornati
2005-07-09 14:41 ` Nanakos Chrysostomos
2005-07-09 14:51 ` Robert Plantz [this message]
2005-07-09 16:31 ` Paolo Ornati
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=1120920668.8305.17.camel@localhost.localdomain \
--to=plantz@sonoma.edu \
--cc=linux-assembly@vger.kernel.org \
--cc=nanakos@wired-net.gr \
--cc=ornati@fastwebnet.it \
/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).