Data Structures in Assembly: Understanding Array Basics and Examples

Data Structures in Assembly: Understanding Array Basics and Examples
Slide Note
Embed
Share

This content covers the fundamental principles of array allocation, access, and examples in assembly language. It delves into topics such as array structure, memory allocation, accessing array elements, and practical examples using zip codes. Explore how arrays are utilized in assembly programming to store and manipulate data efficiently.

  • Assembly Language
  • Data Structures
  • Array Allocation
  • Array Access
  • Zip Codes

Uploaded on Apr 03, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Lecture 8: Data Structures in Assembly CS 105 February 17, 2020

  2. 2 From Last Time int proc(int *p); example1: subq $16, %rsp movl $10, 12(%rsp) movq %rsp, %rdi call 0x400546 <proc> addq $16, %rsp ret int example1(int x) { int a[4]; a[3] = 10; return proc(a); }

  3. Array Allocation Basic Principle TA[L]; Array of data type T and length L Contiguously allocated region of L * sizeof(T) bytes in memory char string[12]; x x + 12 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 double a[3]; x x + 8 x + 16 x + 24 char *p[3]; x x + 8 x + 16 x + 24

  4. Array Access Basic Principle TA[L]; Array of data type T and length L Identifier A can be used as a pointer to array element 0: Type T* int val[5]; 1 5 2 1 3 x x + 4 x + 8 x + 12 x + 16 x + 20 Reference val[4] val val+1 &val[2] val[5] *(val+1) val + i Type int int * int * int * int int int * Value 3 x x + 4 x + 8 ?? 5 x + 4 i

  5. Array Example #define ZLEN 5 typedef int zip_code[ZLEN]; zip_code pomona = { 9, 1, 7, 1, 1 }; zip_code cornell = { 1, 4, 8, 5, 3 }; Declaration zip_code pomona equivalent to int pomona[5] zip_code pomona; 9 1 7 1 1 36 40 44 48 52 56 zip_code cornell; 1 4 8 5 3 16 20 24 28 32 36

  6. Array Accessing Example zip_code pomona; 9 1 7 1 1 16 20 24 28 32 36 int get_digit(zip_code z, int digit){ return z[digit]; } # %rdi = z # %rsi = digit movl (%rdi,%rsi,4), %eax # z[digit] Register %rdi contains starting address of array Register %rsi contains array index Desired digit at %rdi + 4*%rsi Use memory reference (%rdi,%rsi,4)

  7. Array Loop Exercise int array_loop(zip_code z) { int sum = ______; int i; for(i = ___; i < ___; ___ ) sum = _______; } array_loop: xorl %esi, %esi xorl %eax, %eax L1: addl (%rdi,%rsi,4), %eax incq %rsi cmpq $5, %rsi jne L1 retq return _______; } int array_loop(zip_code z){ int sum = ______; int *p; for(p = ___; p < ___; ___ ) sum = _______; } return _______; }

  8. Array Loop Exercise array_loop: xorl %esi, %esi xorl %eax, %eax L1: addl (%rdi,%rsi,4), %eax incq %rsi cmpq $5, %rsi jne L1 retq array_r: xorl %eax, %eax cmpl $5, %esi je L2 pushq %rbx movl (%rdi,%rsi,4), %ebx incq %rsi callq array_r addl %ebx, %eax popq %rbx L2: retq

  9. Structure Representation r struct rec { zip_code z; struct rec *next; }; z 0 next 24 32 20 Structure represented as block of memory Big enough to hold all of the fields Fields ordered according to declaration Even if another ordering could yield a more compact representation Compiler determines overall size + positions of fields Machine-level program has no understanding of the structures in the source code

  10. Generating Pointer to Structure Member r struct rec { zip_code z; struct rec *next; }; r+4*idx z 0 next 24 32 20 Generating Pointer to Array Element Offset of each structure member determined at compile time Compute as r + 4*idx int *get_digit_ptr(struct rec *r, int idx){ return &(r->z[idx]); } # r in %rdi, idx in %rsi leaq (%rdi,%rsi,4), %rax ret

  11. Register %rdi Value p Following Linked List r typedef struct rec { C Code zip_code z; struct rec *next; } zip_node; z next 24 32 20 0 Element i zip_node*get_tail_ptr(zip_node *p){ if(p == NULL){ return NULL; } get_tail_ptr: testq jne xorl retq L1: movq movq testq jne retq %rdi, %rdi L1 %eax, %eax while(p->next != NULL){ p = p->next; } %rdi, %rax 24(%rax), %rdi %rdi, %rdi L1 return p; }

  12. Register %rdi Value p Linked List Exercise r typedef struct rec { C Code zip_code z; struct rec *next; } zip_node; z next 24 32 20 0 Element i ll_exercise: movq testq je movq testq je pushq callq addq L2: retq L1: xorl retq %rdi, %rax %rax, %rax L1 24(%rax), %rdi %rdi, %rdi L2 %rax ll_exercise $8, %rsp zip_node *exercise(zip_node *p){ %eax, %eax }

Related


More Related Content