Recall:   combining adjacent memory cells
 

Recall:   restriction on combining 2 adjacent memory cells:

  • 2 adjacent memory bytes can be combined into one memory cell of 2 bytes if:

    • the address of the first byte is even (= divisible by 2)

    Examples:

            

Recall:   combining adjacent memory cells
 

Recall:   restriction on combining 4 adjacent memory cells:

  • 4 adjacent memory bytes can be combined into one memory cell of 4 bytes if:

    • the address of the first byte is divisible by 4

    Examples:

          

Alignment requirement
 

  • Due to these "bytes combination" limitation, many computers have placement constraints on variable locations in memory

  • These placement constraints are called:

      • memory alignment requirements        

Memory alignment requirements in computer programs
 

  1. int typed and float typed variables:

    • must be allocated at a memory address that is divisible by 4

  2. short typed variables:

    • must be allocated at a memory address that is divisible by 2

  3. byte typed variables:

    • There are no alignment requirements for byte typed variables.

      I.e.: Byte typed variables can be placed anywhere in memory.

 

DEMO: /home/cs255001/demo/asm/1-directives/align.c
(This C program will show the alignment requirements of some variables)

Memory alignment directive

Assembler directive used to satisfy the memory alignment requirements:

 .align  n  // If necessary, skip forward to an address divisible by 2n 

 Examples:

     .align  1   // Skip to an even address
     .align  2   // Skip to an address divisible by 4 
 

How to use the .align directive:

  • To define an int typed variable:

       int x = 55;
    

    We use:

        .align 2   // Skip forward to an address div by 22 = 4  
     i: .4byte 55  // Reserve 4 bytes and initial with 55 (coverted binary) 

Note:   You will not need to use the .align directive in CS255 (it's too low level)