231 - SPARC Background for Memory Access Lab

Mark Smotherman, Fall 1998




Memory

  data sizes
    byte        -   8 bits = 1 byte   -  char, unsigned char
    halfword    -  16 bits = 2 bytes  -  short integer
    word        -  32 bits = 4 bytes  -  int, unsigned int, float
    doubleword  -  64 bits = 8 bytes  -  double


  alignment in byte-addressable memory
    bytes can start anywhere
    halfwords must start on a halfword boundary = address divisible by 2
    words must start on a word boundary = address divisible by 4
    memory access is typically on word-by-word basis (aligned)
    alignment restrictions on load/store instructions prevent multiple-byte
      units from spanning across two memory words, thus allows individual
      load or store instruction to make one and only one memory access --
      this makes hardware easier to build
    unaligned access will cause a trap into the operating system
    to avoid unaligned accesses the compiler can reorder or pad data structures
    in some cases (Fortran, Cobol) unaligned accesses must still be legal for
      programs, so the operating system includes an unaligned trap handler
      that mimics the request action; however, this makes the program run
      much slower

  big-endian versus little-endian
    how are bytes within a multiple byte structure numbered?
    byte 0 is most significant = big-endian = left-to-right
    byte 0 is least significant = little-endian = positional representation


Stack

  storage for automatic variables, that is, for local variables that are
    allocated at the beginning of a procedure call and that are discarded at
    the end of the procedure call (called stack frames)
  stack matches the LIFO behavior of nested procedure calls (incl. recursion)

  %o6 = %sp  -  stack pointer  -  must be doubleword aligned
  %i6 = %fp  -  frame pointer

  the stack is located in high addresses in memory and grows toward small
    addresses, so pushing something decrements the stack pointer
  save instruction makes room on the stack for local variables

  load - different sizes, signed/unsigned distinction for smaller than word
  store - different sizes

  addressing modes
    [reg + reg]   -  one can be %g0 to have form [reg]
    [reg + disp]  -  displacement is sign-extended 13-bit constant

  typical form for stack variable access is:  ld [%fp-4],%o1


Array Access



One-dimensional array
 
  storage allocation                         address arithmetic
 
    low addresses  +----+<-- base address    address of a[i] =
                   | a0 |  ^                   base address +
                   +----+  |                   (i * element_size)
                   | a1 |  |
                   +----+  | index i           for 0-origin indexing
                   |    |  |
                    ...    |
                   |    |  V
                   +----+<--                 address of a(i) =
                   | ai |                      base address +
                   +----+                      ((i-origin) * element_size)
                   |    |
                    ...                        for nonzero-origin indexing
                   |    |
                   +----+
                   | an |
    high addresses +----+
 
 
  access to stack-allocated array, 0-origin
 
  sll  %i_r, 2,   %o0      ! i in i_r, multiply by 4
  add  %fp,  %o0, %o0      ! add offset of element i to %fp
  ld   [%o0 + ary_s], %o0  ! access by adding offset of array start from %fp