Practice Questions for “Behavioral model of memory”:

Updated January 17,2012 ŕ see part (B)

 

 

A) Test this program on various machines to see if they are little- or big-endian:

 

#include <stdio.h>

unsigned int value = 0x01020304;

 

main ()

{

 

  unsigned char *byte_address = (unsigned char *) &value;

  int i;

 

  printf (“The value as a 32-bit quantity is in hex: 0x%08x\n”, value);

  printf (“The value is stored starting in memory location in hex: 0x%08x\n”, byte_address);

 

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

  {

     printf (“The value stored in the byte at memory location 0x%08lx (hex) is in hex 0x%02x\n”, byte_address, *byte_address);

     byte_address++;

  }

}

 

Use copy/paste but you may have to change the “” characters to the double-quotes character in ASCII.

 

On a little-endian machine you should see something like this:

 

The value as a 32-bit quantity is in hex: 0x01020304

The value is stored starting in memory location in hex: 0x080496b4

The value stored in the byte at memory location 0x080496b4 (hex) is in hex 0x04

The value stored in the byte at memory location 0x080496b5 (hex) is in hex 0x03

The value stored in the byte at memory location 0x080496b6 (hex) is in hex 0x02

The value stored in the byte at memory location 0x080496b7 (hex) is in hex 0x01

 

Notice how the number is stored starting from the least-significant byte.

 

On a big-endian machine you should see something like this:

 

The value as a 32-bit quantity is in hex: 0x01020304

The value is stored starting in memory location in hex: 0x2000000

The value stored in the byte at memory location 0x2000000 (hex) is in hex 0x01

The value stored in the byte at memory location 0x2000001 (hex) is in hex 0x02

The value stored in the byte at memory location 0x2000002 (hex) is in hex 0x03

The value stored in the byte at memory location 0x2000003 (hex) is in hex 0x04

 

 

B) Given the following initial contents of memory:

 

 

+0

+1

+2

+3

0

0x11

0x22

0x33

0x44

4

0x55

0x66

0x77

0x88

8

0x99

0xaa

0xbb

0xcc

12

0xdd

0xee

0xff

0x00

 

What will the following operations do (highlight to see the answers)?

 

LOAD.W 4 ŕ will return 0x55667788 (big endian) or 0x88776655 (little endian)

LOAD.H 0xe ŕ will return 0xff00 (big endian) or 0x00ff (little endian)

Can you sign-extend the last result to 32-bits? 0xff00 ŕ 0xffffff00 (big endian) and 0x00ff ŕ 0x000000ff (little endian)

What if you were to zero-extend? 0xff00 ŕ 0x0000ff00 (big endian) and 0x00ff ŕ 0x000000ff (little endian)

STORE.H 0xa, 0x1234 ŕ

 

For BIG ENDIAN

 

+0

+1

+2

+3

0

0x11

0x22

0x33

0x44

4

0x55

0x66

0x77

0x88

8

0x99

0xaa

0x12

0x34

12

0xdd

0xee

0xff

0x00

 

For LITTLE ENDIAN

 

+0

+1

+2

+3

0

0x11

0x22

0x33

0x44

4

0x55

0x66

0x77

0x88

8

0x99

0xaa

0x34

0x12

12

0xdd

0xee

0xff

0x00

 

LOAD.W 0x8 ŕ assuming the corresponding state above it will return 0x99aa3412 (Big endian) or 0x1234aa99 (little endian)