Endian

General FreeBASIC programming questions.
Post Reply
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Endian

Post by albert »

What computers are big and little endian???

I have an AMD processor , and if i do a , var = bin( *usp ) ( ushort pointer ) , i have to flop every other byte , after converting them back to chr's

Does Intel have the same output, where you have to flop every other byte?

If i do var+= right( string( 16 , "0" ) + bin( *usp ) , 16 )
and then do var+= chr( val( &B" + mid( str , a , 8 ) ) ) ... The bytes are backwards , and i have to swap every other byte.

Code: Select all


screen 19

dim as string str1 = "0123456789ABCDEF"

dim as string binari = ""
dim as ushort ptr usp = cptr(ushort ptr , strptr(str1))
for a as longint = 1 to len(str1) step 2
    binari+=right( string(16,"0") + bin(*usp),16)
    usp+=1
next

dim as string outputs=""
for a as longint = 1 to len(binari) step 8
    outputs+=chr(val("&B"+mid(binari,a,8)))
next

print str1
print outputs

sleep
end

On my AMD computer the bytes are backwards
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Endian

Post by badidea »

The Intel x86 and also AMD64 / x86-64 series of processors use the little-endian format, and for this reason, it is also known in the industry as the "Intel convention".
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Endian

Post by Munair »

badidea wrote:
The Intel x86 and also AMD64 / x86-64 series of processors use the little-endian format, and for this reason, it is also known in the industry as the "Intel convention".
Yes, and very inefficiently, the network (internet) default is big-endian. Things would be a lot easier and more efficient if everyone would agree on one standard.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Endian

Post by dodicat »

"Only one endian", reminds me of a song from the sixties, but I cannot place it, I'll be trying to remember what it was all day now.
Thank you!
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Endian

Post by marcov »

dodicat wrote:"Only one endian", reminds me of a song from the sixties, but I cannot place it, I'll be trying to remember what it was all day now.
Thank you!
Maybe also a Swift's Gulliver's Travels reference? The term come from it. (and Red Dwarf did the same later with red and blue hats)
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Endian

Post by dodicat »

Not that marcov
but
BINGO - I remember
Although it is so far back in the mists of time I was confused.
https://www.youtube.com/watch?v=tC1auBpqZ4E
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Endian

Post by Munair »

dodicat wrote:Not that marcov
but
BINGO - I remember
Although it is so far back in the mists of time I was confused.
https://www.youtube.com/watch?v=tC1auBpqZ4E
Many great songs were penned by the Bee Gees.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Endian

Post by albert »

Since the majority of computers on the planet , use AMD or Intel processors..
You would think the coders would make the *ptr to be the endian style of the majority..

Are the coders coding for Sun Sparcs?
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Endian

Post by marcov »

albert wrote:Since the majority of computers on the planet , use AMD or Intel processors..
The vast majority is using network. So network order should be default :=)
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Endian

Post by Munair »

marcov wrote:
albert wrote:Since the majority of computers on the planet , use AMD or Intel processors..
The vast majority is using network. So network order should be default :=)
Indeed.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Endian

Post by jj2007 »

I've programmed a lot in 68000 Assembler (big endian) when I was younger. Nowadays it's the Intel family of processors, and I must admit that little Endian code is often more convenient and elegant.
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Endian

Post by jevans4949 »

IBM S/360 mainframes were big-endian, and the most popular machines when the internet was invented. I believe some minicomputers had little-endian format, but based around 16-bit "words"; I distinctly remember seeing one application transferring data to/from a mini-computer which included 6-byte integers consisting of a 4-byte big-endian little end followed by a 2-byte big end! I also worked a system which connected Burroughs accounting machines to IBM mainframes, and Burroughs expected the bytes to be transmitted little-endian.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Endian

Post by caseih »

albert wrote:Since the majority of computers on the planet , use AMD or Intel processors..
You would think the coders would make the *ptr to be the endian style of the majority..

Are the coders coding for Sun Sparcs?
The only way to make the bytes look like you think they should in memory is to use a big-endian architecture.

I was taught in Uni that there is no functional advantage to little endian over big endian and vice versa. From the CPU's point of view either is equally efficient. The only reason I can think of that might favor little endian is that when reading a byte stream you can build a number while reading the bytes, stopping any time you want. Whereas in big endian, you must know the length of the number before you can start assembling it from bytes. But from a human point of view, big-endian is clearly preferred, as the numbers are immediately human readable when looking at straight hex values from bytes.

I'm not at all sure what you mean by "You would think the coders would make the *ptr to be the endian style of the majority.." Typically the endian just doesn't matter. Pointers work the same either way. Casting to bytes on the other hand, that's another story.

Recently I wrote some code to parse RTCMv3 messages, which are composed of a series of bitfields, all done in big-endian. It was very confusing because the fields are not byte-aligned. So parsing involves some bit shifting and masking as well as big-endian to host endian translation. Turned out to be quite simple; I was just confusing myself by over thinking the endian issue.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Endian

Post by marcov »

caseih wrote: From the CPU's point of view either is equally efficient. The only reason I can think of that might favor little endian is that when reading a byte stream you can build a number while reading the bytes, stopping any time you want.
It indeed requires very little CPU support, many older RISC types (like PowerPC) had a bit to switch endianess even. (note that only the computer builder could choose, not the end user, since wiring and chips on the mainboard were also involved)

Any change to the status quo (like changing network order, or making Intels bigendian) would be worse than what can be recooped before the sun goes out, so this is all luckily fairly moot.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Endian

Post by caseih »

ARM is also able to run in big endian or little endian but most ARM systems are running in little endian mode. I'm not sure if this is set by the OS during boot loading, or if it's burned into the chip somehow.
Post Reply