Translating the variable definition:   the head variable

  • In Java, we define the head variable as follows:

         public class Node 
         {
            int value;
            List next;
         }
      
         Node  head;     

  • In C, we define the head variable as follows:

         struct Node 
         {
            int value;
            struct Node *next;
         }
      
         struct Node  *head;     

  • The variable head is a reference variable that stores a memory address (= 4 bytes)

Translating the variable definition:   the head variable

  • The translation of the definition of the variable head into ARM assembler is as follows:

      .data
    
      head:  .skip 4    // The head variable      

  • Note:   the variable at label head is reference (= address) (and not a Node object):

How to fetch the address stored in the variable head
 

  • Facts about the variable head:

      1. The variable head is stored in memory       

      2. The variable head contains 4 bytes

     

     

     

     

     

 

How to fetch the address stored in the variable head
 

  • Facts about the variable head:

      1. The variable head is stored in memory       

      2. The variable head contains 4 bytes

  • Therefore, we must use the ldr instruction to fetch 4 bytes in the variable head:

         // Preparation:
         //    r1 = address of head
      
      
         ldr    r0, [r1]                             
      

We have (already) learned how to put the address of head in a register !

How to fetch the base address stored in the variable head
 

  • Facts about the variable head:

      1. The variable head is stored in memory       

      2. The variable head contains 4 bytes

  • How to fetch the address in the variable head into a register (e.g.: r0):

         // Preparation:
         movw    r1, #:lower16:head
         movt    r1, #:upper16:head
      
         ldr    r0, [r1]                             
      

Note:   exactly the same way as fetching an int typed variable from memory !!

Difference in fetching the base address of a static array and the first object in a linked list

Base address of a static array Base address of the first list object
The base address of a static array is a constant number The base address of the first list object is a variable

The same technique is used to implement the dynamic array

Base address of a static array Base address of a dynamic array
The base address of a static array is a constant number The base address of a dynamic array is a variable

I.e.:   you can use this method to fetch the base address of a dynamic array