torsdag den 26. november 2015

Brave new world (3) - The war between endians

The war between endians

This last article in this series is about the most peculiar thing in the computer industry, I have ever seen: The different implementation of integers.

As you know, a proper integer looks like this: X’12345678’ which is decimal 305.419.896. On Intel computers it is the other way round: X’78563412’ is decimal 305.419.896. That really made me dizzy, when I saw it the first time. Why are they doing it that way? There can be several reasons but my explanation goes back to the paper tape. You could mix text with integers on a paper tape. They knew when to expect an integer and when the first byte was retrieved they could start calculating, f. ex. adding it to an integer in storage. Numbers come in calculating order.

The two types of CPU’s had an argument of which integer was better and some named it “Big-endian” (the right way ;-)’ and “Little endian” (the Intel way) after which address in storage the least significant byte starts.
Wikipedia writes:
“...  the "endian" names were pointedly drawn from Jonathan Swift's 1726 satirical fantasy novel, Gulliver’s Travels, in which civil war erupts over whether the big or the small end of a soft-boiled egg is the proper end to crack open.”

In order to incorporate all peculiarities from the “plastic” world System z also had to make some instructions that dealt with “little endians” *1).

  1. Move Inverse - MVCIN
  2. Load Reversed - LRV
  3. Store Reversed - STRV
*2)

LRV and STRV instructions are very straight forward. They load and store integers as ST and L. They just reverse the integer in the process. However, the MVCIN addresses the last byte of the “from” field and moves from right-to-left to the “to” field that adds the bytes left-to-right. Look at my example and it will probably make sense.
INTEGERS       DS CL8
INTEGER        DS F  
STORE_REVERSED DS F  
MOVE_INVERSED  DS F  
LOAD_REVERSED  DS F  
-------------------------------------------
        MVC   INTEGERS,=C'INTEGERS'               
*                                                  
        MVC   INTEGER,BIG_ENDIAN                  
*                                                  
        L     R1,BIG_ENDIAN                       
        STRV  R1,STORE_REVERSED                   
*                                                  
        MVCIN MOVE_INVERSED,BIG_ENDIAN+L'INTEGER-1
*                                                  
        LRV   R1,BIG_ENDIAN                       
        ST    R1,LOAD_REVERSED
--------------------------------------------
BIG_ENDIAN DC  A(305419896)
Code example of reverse instructions

The storage dump of the integers looks as below
1F700E40       +58  00006FF0 C9D5E3C5 C7C5D9E2 12345678 *..?0INTEGERS....*
1F700E50       +68  78563412 78563412 78563412          *............    *
Storage dump of little-endians and big-endians

*1) If you can’t remember which is which just think of Little Endians are for small computers
*2) Pardon my English but I do not know the difference between "Inverse" and "Reverse"

Ingen kommentarer:

Send en kommentar