Structures and Linked Lists in C/C++ ICS 51. Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Link list/file stamps/clusters Odds and ends remaining for test 2.
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Accessing parameters from the stack and calling functions.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Runtime Environments Compiler Construction Chapter 7.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Today’s Material Aggregate Data Types: Structures and Unions
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Stack Usage with MS Visual Studio Without Stack Protection.
Compiler Construction Code Generation Activation Records
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Assembly Language: Function Calls Jennifer Rexford.
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Recitation 3: Procedures and the Stack
Stack Operations Dr. Hadi AL Saadi.
Assembly function call convention
STACKS & QUEUES for CLASS XII ( C++).
CS-401 Computer Architecture & Assembly Language Programming
Computer Architecture and Assembly Language
143A: Principles of Operating Systems Lecture 4: Calling conventions
Exploiting & Defense Day 2 Recap
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Aaron Miller David Cohen Spring 2011
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Pointers and Linked Lists
Discussion Section – 11/3/2012
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Heterogeneous aggregate datatypes
Stack Frames and Advanced Procedures
Practical Session 4.
Multi-modules programming
X86 Assembly Review.
Low-Level Thread Dispatching on the x86
Dynamic allocation (continued)
ICS51 Introductory Computer Organization
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Structures and Linked Lists in C/C++ ICS 51

Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a single object One or more members, possibly different types Example struct grade { intstudentID; char* lastName; char* firstName; intscore; } struct grade my_grade, my_friends_grade; Memory allocation Consecutive locations, may be 4-byte aligned or not (depends on compiler) In order to avoid alignment uncertainty, use all the fields of the same 4-byte size

Linked List In one of the assignments, we will use linked list typedef struct list_struc { struct list_struc * next_element; int value; } list; Example of linked list each element is of type list; head = &elem1 elem1 = {&elem2, -2} elem2 = {&elem3, 0} elem3 = {&elem4, 1} elem4 = {&elem5, 3} elem5 = {NULL, 5} ‘&’ means ‘address of’; NULL – reserved address with the value of 0 elem2 -2 elem3 0 pointer to head elem4 1 elem5 3 NULL 5 elem1 elem5

Sample program #include typedef struct linked_list { struct linked_list *next_element; int value; } list; int sum_c(struct linked_list *link_ptr) { int sum=0; while (link_ptr != NULL) ////// we go through the list till it reaches NULL { sum=sum+(*link_ptr).value; link_ptr=(*link_ptr).next_element; ////// we move the pointer to the next element } return sum; } __declspec(naked) int sum_asm(struct linked_list *link_ptr) { __asm { push ebx //////// we 'd better push the registers before using them push ecx //////////// same thing mov eax,0 mov ebx, dword ptr[esp+12] /////////// the parameter passed is in esp+12 MAIN_LOOP: cmp ebx, NULL je END mov ecx, dword ptr[ebx+4] /////////// moves the second 4 bytes of the strcuture (.value) into ecx add eax,ecx mov ebx, dword ptr[ebx] ///////// moves the first 4 bytes of the structure (.next_element) into ebx jmp MAIN_LOOP END: pop ecx ////////// pop the pushed registers before returning pop ebx //////////// same thing ret} See next page…

Sample program (cont.) void main() { struct linked_list first, second, third, fourth, fifth, sixth; ///// we define some elements struct linked_list *list_ptr; // we define a pointer to an element or a list, // the pointer initially points to a dummy location. first.value=10; /////////// intialization. first.next_element=&second; /////////// the pointer of the second obejct is copied to the first.next_element. second.value=2; second.next_element=&third; third.value=5; third.next_element=&fourth; fourth.value=100; fourth.next_element=&fifth; fifth.value=20; fifth.next_element=&sixth; sixth.value=17; sixth.next_element=NULL;//////////// NULL is defined in list_ptr=&third; printf("the returned value for a single element like sixth is %d\n",sum_c(&sixth)); printf("the returned value for the list starting from the third element is %d\n",sum_asm(list_ptr)); printf("the returned value for the whole list is %d\n",sum_asm(&first)); third.next_element=&sixth; ////////////// deleting elements fourth and fifth printf("the returned value after deleting elements fourth and fifth is %d\n",sum_asm(&first)); }