ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Computer Organization and Assembly Language
    DC-221
    Progress0 / 35 topics
    Topics
    1. Introduction to Computer Systems2. Information is Bits + Context3. Programs are Translated by Other Programs4. Understanding Compilation Systems5. Processors Read and Interpret Instructions6. Caches Matter7. Storage Devices Form a Hierarchy8. The Operating System Manages the Hardware9. Systems Communicate Using Networks10. Representing and Manipulating Information11. Information Storage12. Integer Representations13. Integer Arithmetic14. Floating Point15. Machine-Level Representation of Programs16. A Historical Perspective17. Program Encodings18. Data Formats19. Accessing Information20. Arithmetic and Logical Operations21. Control22. Procedures23. Array Allocation and Access24. Heterogeneous Data Structures25. Understanding Pointers26. Using the GDB Debugger27. Out-of-Bounds Memory References and Buffer Overflow28. x86-64: Extending IA-32 to 64 Bits29. Machine-Level Representations of Floating-Point Programs30. Processor Architecture31. The Y86 Instruction Set Architecture32. Logic Design and the Hardware Control Language (HCL)33. Sequential Y86 Implementations34. General Principles of Pipelining35. Pipelined Y86 Implementations
    DC-221›Array Allocation and Access
    Computer Organization and Assembly LanguageTopic 23 of 35

    Array Allocation and Access

    7 minread
    1,270words
    Intermediatelevel

    Array Allocation and Access in Computer Organization and Assembly Language

    In Computer Organization and Assembly Language, arrays are collections of elements, typically of the same data type, stored in contiguous memory locations. These arrays can be accessed and manipulated through their indices. Understanding how arrays are allocated and accessed is crucial for working efficiently with data structures in assembly language.

    1. Array Allocation

    In assembly language, array allocation refers to reserving space in memory for storing multiple data elements. Since assembly works directly with memory addresses, arrays are typically created by allocating a block of memory large enough to hold all the elements of the array.

    a) Contiguous Memory Allocation

    Arrays in most low-level programming languages, including assembly, are stored in contiguous memory locations. This means that the elements of the array are laid out sequentially in memory, one after another. The starting memory address is often referred to as the base address of the array.

    For example, an array of integers will occupy a series of consecutive memory locations, where each integer occupies the same number of bytes (commonly 4 bytes for 32-bit integers).

    b) Allocating Space for Arrays

    In assembly, the size of the array depends on the number of elements and the size of each element. For example, if we are working with an array of 10 integers (each 4 bytes in size), we would allocate 40 bytes of space.

    Example (Allocating Space in Data Segment)
    section .data
        myArray times 10 dd 0  ; Declare an array of 10 integers (each 4 bytes) initialized to 0
    

    In this example:

    • times 10 indicates we want 10 elements.
    • dd (define doubleword) allocates 4 bytes for each integer (32-bit integer).

    2. Array Access

    Array elements in assembly language are accessed using their index and the base address of the array. Since arrays are stored in contiguous memory locations, each element can be accessed using a formula:

    • Address of the nth element = Base Address + (n * Size of each element)

    In assembly, the base address can be accessed directly by referring to the label representing the array, and each element can be accessed by adjusting the address using the index.

    a) Accessing Array Elements

    Let’s consider an array myArray where we want to access the n-th element. In assembly, we can calculate the address of the element using the index and then load or store data into it.

    Example (Accessing Array Elements)
    section .data
        myArray times 10 dd 0  ; Declare an array of 10 integers (each 4 bytes) initialized to 0
    
    section .text
        global _start
    
    _start:
        MOV ESI, 4       ; Set index to 4 (for 5th element)
        MOV EBX, myArray ; Load the base address of the array into EBX
        MOV ECX, 4       ; Size of each element (4 bytes for 32-bit integers)
        MUL ECX          ; Multiply index by size of element (index * size)
        ADD EBX, EAX     ; Add the offset to the base address to get the address of the 5th element
        MOV EAX, [EBX]   ; Load the value of the 5th element into EAX
    
        ; Continue with other operations...
        ; Exit program
        MOV EAX, 1       ; SYS_exit system call
        MOV EBX, 0       ; Exit status
        INT 0x80         ; Exit the program
    

    In this example:

    • The array myArray is declared with 10 elements, each 4 bytes.
    • ESI is set to 4, which corresponds to the 5th element (array indices start at 0).
    • The MUL ECX instruction multiplies the index by the size of each element (4 bytes), calculating the byte offset.
    • The result is added to the base address (EBX) to get the address of the 5th element in the array.
    • Finally, MOV EAX, [EBX] loads the value of the 5th element into the EAX register.

    b) Modifying Array Elements

    To modify an element in the array, we first calculate the address of the element, and then use the MOV instruction to store a value at that address.

    Example (Modifying Array Elements)
    section .data
        myArray times 10 dd 0  ; Declare an array of 10 integers (each 4 bytes) initialized to 0
    
    section .text
        global _start
    
    _start:
        MOV ESI, 3       ; Set index to 3 (for 4th element)
        MOV EBX, myArray ; Load the base address of the array into EBX
        MOV ECX, 4       ; Size of each element (4 bytes for 32-bit integers)
        MUL ECX          ; Multiply index by size of element (index * size)
        ADD EBX, EAX     ; Add the offset to the base address to get the address of the 4th element
        MOV DWORD [EBX], 42  ; Store the value 42 in the 4th element
    
        ; Continue with other operations...
        ; Exit program
        MOV EAX, 1       ; SYS_exit system call
        MOV EBX, 0       ; Exit status
        INT 0x80         ; Exit the program
    

    In this example:

    • The value 42 is stored in the 4th element of the array (myArray[3]).
    • We first calculate the address of the 4th element using the index (ESI = 3), multiply by the size of the element (4 bytes), and then add the result to the base address of the array.
    • The MOV DWORD [EBX], 42 instruction stores the value 42 at the calculated memory location.

    3. Multi-Dimensional Arrays

    Multi-dimensional arrays (such as 2D arrays) are often represented as 1D arrays in memory, with each row stored consecutively in memory. For example, a 2D array with m rows and n columns can be represented as a 1D array with m * n elements.

    The address calculation for a 2D array typically requires two indices: row and column. The address of the element at row i and column j in a 2D array of integers can be calculated as:

    • Address of element at (i, j) = Base Address + ((i * Number of columns) + j) * Size of element

    Example (2D Array Access)

    section .data
        my2DArray times 3*4 dd 0  ; 3x4 array (3 rows, 4 columns)
    
    section .text
        global _start
    
    _start:
        MOV ESI, 1       ; Set row index to 1 (second row)
        MOV EDI, 2       ; Set column index to 2 (third column)
        MOV EBX, my2DArray ; Load base address of array into EBX
        MOV ECX, 4       ; Number of columns (4 in this case)
        MOV EDX, 4       ; Size of each element (4 bytes for 32-bit integers)
    
        ; Calculate address of element (1, 2) in 2D array
        MUL ECX          ; Multiply row index by number of columns
        ADD ESI, EDI     ; Add column index to row calculation
        MUL EDX          ; Multiply by size of element (4 bytes)
        ADD EBX, EAX     ; Add the offset to the base address to get the address of (1, 2)
    
        MOV EAX, [EBX]   ; Load the value at (1, 2) into EAX
    
        ; Continue with other operations...
        ; Exit program
        MOV EAX, 1       ; SYS_exit system call
        MOV EBX, 0       ; Exit status
        INT 0x80         ; Exit the program
    

    In this example:

    • We calculate the address of the element located in the second row and third column of a 3x4 array, using the formula for calculating the address of a 2D array element.
    • The base address of the array is in EBX, and we use ESI and EDI to calculate the row and column offsets.

    4. Conclusion

    In Computer Organization and Assembly Language, array allocation and access involve directly manipulating memory addresses using indexes and calculating offsets based on the element size. Arrays are typically allocated as contiguous blocks of memory, and elements can be accessed or modified by calculating their memory addresses. This gives assembly language programmers complete control over memory management, but requires careful handling of memory and array bounds.

    Previous topic 22
    Procedures
    Next topic 24
    Heterogeneous Data Structures

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,270
      Code examples0
      DifficultyIntermediate