Review: life time of variables
 

Variables in a computer program have different life times:

  • Long term storage variables:

    • The life time of these variables is the entire duration of the program execution

      Example:

      • Java's   class variables
      • C's   global variables

  • Short term storage variables:

    • The life time of these variables is the duration of a method invocation (call)

      Examples:

        • Parameter variables and local variables    

The storage location of the long term variables

  • The long term storage variables are stored in the .data section of a computer program:


  • Note:

    • Later in the course, we will study how to store short term variables

What happens when you define an uninitialized global variable in a computer program

    int x;  // global variable

Effect:

  1. Reserved some unused memory cells to store the value (= a binary number) in the variable:

  2. Equate the variable name to the memory location of the first allocated memory cell (so we can use the variable name to find the variable)

Assembler directive to define uninitialized global variables
 

ARM assembler directives to reserve uninitialized memory cells:

 .skip  n   // Reserve n bytes of memory    
   

Examples:

  x: .skip  4   // Reserve n bytes of memory    
  y: .skip  2   // Reserve n bytes of memory       
   

Important:  

  • You must use a (unique) label to mark the reserved memory cells

  • You need the memory address in order to read/write the variable !

How C's global variable definitions are translated into ARM assembler
 

Examples of C global variable definitions:

   int a;
   short b;
   byte c; 

The equivalent construct in ARM assembler:

 .data   // Variables are reserved in the .data section

 a: .skip 4  // Reserve 4 bytes and id this mem addr with "a"
 b: .skip 2  // Reserve 2 bytes and id this mem addr with "b"
 c: .skip 1  // Reserve 1 bytes and id this mem addr with "c"

Comment:

  • Uninitialized global variables will have the value 0 (ZERO)

How C's global variable definitions are translated into ARM assembler
 

Example global variables definitions:

  .data

  a:      .skip 4    // Reserve 4 bytes
  b:      .skip 2    // Reserve 2 bytes
  c:      .skip 1    // Reserve 1 byte
 

Effect: the variables are reserved inside the .data section:

DEMO: /home/cs255001/demo/asm/1-directives/skip01.s

What happens when you define an initialized global variable in a computer program

   int x = 4;  

Effect:

  • Reserved some unused memory cells and store the initial value (= a binary number) in the memory cells:

  • Equate the variable name to the memory location of the first allocated memory cell (so we can use the variable name to find the variable)

ARM assembler directives used to define initialized variables

 

  .byte Value    - reserves 1 byte of memory and 
                   store the 8 bits 2's complementary 
                   code for Value in the memory cell

  .2byte Value   - reserves 2 bytes of memory and 
                   store the 16 bits 2's complementary 
                   code for Value in the memory cells

  .4byte Value   - reserves 4 bytes of memory and 
                   store the 32 bits 2's complementary 
                   code for Value in the memory cells

  .float Value   - reserves 4 bytes of memory and 
                   store the 32 bits IEEE 754 
                   code for Value in the memory cells

  .asciz "..."   - reserves strlen bytes of memory and 
                   store the ASCII codes for all the 
                   characters in the memory cells
   

How initialized variable definitions are translated into ARM assembler

Examples of initialized variable definitions:

   byte   a = 15;
   short  b = 15;
   int    c = 15;
   float  d = 5.0;
   char[] e = "ABC"; // C style array 

Their ARM assembler equivalent (= how the C compiler will translate the variable definitions)

   a:      .byte  15
   b:      .2byte 15
   c:      .4byte 15
   d:      .float  5
   e:      .asciz "ABC"
   

How initialized variable definitions are translated into ARM assembler

The effect of the reservation made by the .byte, .2byte, .4byte, .float and .asciz directives:

   .data

   a:      .byte  15
   b:      .2byte 15
   c:      .4byte 15
   d:      .float  5
   e:      .asciz "ABC"

Results:   the following memory values are allocated inside the .data section:

DEMO: /home/cs255001/demo/asm/1-directives/init-var.s

Emphasis: a label is a symbolic name for a memory address

Notice how the variables are stored starting at memory address 0x1878c(16) (Hex)

Notice that the label (symbolic name)  a  is equal to the binary number 0x1878c(16) (Hex):