ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalDC-221Array Allocation Acces…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    DC-221

    Computer Organization and Assembly Language

    Progress0 / 35 done
    Topics35
    1Introduction to Computer Systems2Information is Bits + Context3Programs are Translated by Other Programs4Understanding Compilation Systems5Processors Read and Interpret Instructions6Caches Matter7Storage Devices Form a Hierarchy8The Operating System Manages the Hardware9Systems Communicate Using Networks10Representing and Manipulating Information11Information Storage12Integer Representations13Integer Arithmetic14Floating Point15Machine-Level Representation of Programs16A Historical Perspective17Program Encodings18Data Formats19Accessing Information20Arithmetic and Logical Operations21Control22Procedures23Array Allocation and Access24Heterogeneous Data Structures25Understanding Pointers26Using the GDB Debugger27Out-of-Bounds Memory References and Buffer Overflow
    28Extending IA-32 to 64 Bits29Machine-Level Representations of Floating-Point Programs30Processor Architecture31The Y86 Instruction Set Architecture32Logic Design and the Hardware Control Language (HCL)33Sequential Y86 Implementations34General Principles of Pipelining35Pipelined Y86 Implementations
    7 min read
    Topic 23 of 35
    Topic Study Note Illustration
    23

    Array Allocation and Access

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language