Sample Solution from
Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092669
Chapter 2
Problem 2.55HW
Try another sample solution
Textbook Problem

Compile and run the sample code that uses show_bytes (file show-bytes. c) on different machines to which you have access. Determine the byte orderings used by these machines.

Expert Solution
Program Plan Intro

Byte ordering:

  • Some machines decide to store the objects in memory ordered from least significant byte to most, while other machines store them from most to least.
  • The byte ordering are made by the two ways:
    • Little Endian
      • In little Endian, the least significant byte comes first.
    • Big Endian
      • In big Endian, the most significant byte comes first.

Example:

The example for find the little-endian and big-endian for hexadecimal value is shown below:

Here assume that the hexadecimal value is “0x13244860”. Then address range for given ordering byte is “0x200” through “0x203”.

Big Endian for given hexadecimal value is

0x2000x2010x2020x203
13244860

Little Endian for given hexadecimal value is

0x2000x2010x2020x203
60482413

Explanation of Solution

Corresponding code from given question:

#include <stdio.h>

//Define variable "byte_pointer" in char datatype.

typedef unsigned char* byte_pointer;

//Function definition for show_bytes.

void show_bytes(byte_pointer start, size_t len)

{

//Declare variable "i" in int data type.

int i;

/* "For" loop to determine byte representation in hexadecimal */

for (i = 0; i < len; i++)

//Display each bytes in "2" digits hexadecimal value.

printf(" %.2x", start[i]);

printf("\n");

}

//Function to determine byte for "int" number.

void show_int(int x)

{

//Call show_bytes function with integer value.

show_bytes((byte_pointer) &x, sizeof(int));

}

//Function to determine byte for "float" number.

void show_float(float x)

{

//Call show_bytes function float value.

show_bytes((byte_pointer) &x, sizeof(float));

}

//Function to determine byte for "pointer" number.

void show_pointer(void *x)

{

//Call show_bytes function with pointer value.

show_bytes((byte_pointer) &x, sizeof(void *));

}

//Test all show bytes.

void test_show_bytes(int val)

{

//Define variables.

int ival = val;

float fval = (float) ival;

int *pval = &ival;

//Call function.

show_int(ival);

show_float(fval);

show_pointer(pval);

}

//Main function.

int main(int argc, char* argv[])

{

//Define the sample number.

int sampleNumber = 682402;

//Call test_show_bytes function.

test_show_bytes(sampleNumber);

return 0;

}

The given program is used to display the byte representation of different program objects by using the casting.

  • Define “byte_pointer” using “typedef”.
    • It is used to define data type as a pointer to an object of type “unsigned char”.
  • The function “show_bytes” is used to display the address of a byte sequence by using the argument that is byte pointer and a byte count.
    • Each byte is displayed by “2” digit.
  • The function “show_int” is to display the byte representations of object of “int” data type.
  • The function “show_float” is to display the byte representations of object of “float” data type.
  • The function “show_pointer” is to display the byte representations of object of “void *” data type.
  • Test all the data type values by using function “test_show_bytes”.
  • Finally, assign the sample number in main function and call the “test_show_bytes” with argument “sampleNumber”.

Byte ordering used by the given machines:

After compiling and running the above code, the following output will be appear

a2 69 0a 00

20 9a 26 49

3c cc e9 18 ff 7f 00 00

From the above output,

  • The byte representation for “int” data type is “a2 69 0a 00”.
  • The byte representation for “float” data type is “20 9a 26 49”.
  • The byte representation for “int *”data type is “3c cc e9 18 ff 7f 00 00”.

The byte ordering used by these machines is “big-endian”.

  • Reason:
    • Consider, the byte representation of “int” value is “a2 69 0a 00”.
    • From this, the value is ordered from most significant byte to least significant byte. Hence, it is referred as big-endian.
Not sold yet?Try another sample solution