CS 354 Machine Organization Programming Tuesday April 08 and Thursday April 10 2025 Midterm Exam 2 Results will be released by Friday April 11 This Week Project P5 DUE by Saturday Apr 19 Available without ontime points until Monday Homework Quiz HW6 DUE by Monday Apr 14 Available without ontime points until Apr 21 Tuesday Apr 15 Homework Quiz HW7 Released by the end of this week Activity A11 DUE by Saturday Apr 12 Learning Objectives able to trace function call and its stack frame able to access parameters and local variables based on location from ebp and esp able to trace recursive function calls through their stack frame identify and describe effects of ASM call ret leave instructions able to access 1D array element using ASM instructions and memory operand types able to access multidimensional array via ASM instructions and memory operand types describe compute and use alignment requirements of elements in structs and unions understand the difference and use of structs and unions in C This Week Tuesday Recursion Stack Allocated Arrays in C Stack Allocated Arrays in Assembly Stack Allocated Multidimensional Arrays Thursday Stack Allocated Structures Alignment Alignment Practice Unions Next Week Pointers in Assembly Stack Smashing and Exceptions B O 3 10 Putting it Together Understanding Pointers 3 12 Out of Bounds Memory References and Buffer Overflow 8 1 Exceptions 8 2 Processes 8 3 System Call Error Handling 8 4 Process Control through pg 719 Copyright 2016 2025 Jim Skrentny Dr Mahmood CS 354 S25 W11 L21 22 1 Recursion Use a stack trace to determine the result of the call fact 3 int fact int n int result if n 1 result 1 else return result result n fact n 1 direct recursion recursive case base case infinite recursion Assembly Trace fact pushl ebp movl esp ebp pushl ebx subl 4 esp movl 8 ebp ebx movl 1 eax cmpl 1 ebx jle L1 leal 1 ebx eax movl eax esp call fact imull ebx eax L1 addl 4 esp popl ebx popl ebp ret Stack bottom 1st fact s arg 3 main s return addr main 1st fact 2nd fact 3rd fact Infinite recursion causes When tracing functions in assembly code focus on the function s body in HLL Copyright 2016 2025 Jim Skrentny Dr Mahmood CS 354 S25 W11 L21 22 2 Stack Allocated Arrays in C Recall Array Basics T A N where T is the element datatype of size L bytes and N is the number of elements 1 a contiguous region of the stack 2 an identifier is associated with the The elements of A are accessed using Recall Array Indexing and Address Arithmetic A i 1 int I 11 2 char C 7 3 double D 11 4 short S 42 5 char C 13 6 int I 11 7 double D 7 For each array declarations below what is L element size the address arithmetic for the ith element and the total size of the array C code L Address of ith element Total array size Copyright 2016 2025 Jim Skrentny Dr Mahmood CS 354 S25 W11 L21 22 3 Stack Allocated Arrays in Assembly Arrays on the Stack How is an array laid out on the stack Option 1 or 2 The first element index 0 of an array Accessing 1D Arrays in Assembly higher addresses earlier frames 1 A 0 A 1 A N 1 2 A N 1 A 1 A 0 Stack Top Assume array s start address is in edx and index is in ecx movl edx ecx 4 eax Assume I is an int array and S is a short int array For both arrays start address is in edx and the index i is in ecx Determine the element type and instruction for each Assembly instruction to move C code s value into eax C code Type 1 I 2 I 0 3 I 4 I i 5 I 2 6 I i 1 7 I i 3 8 S 3 9 S 1 10 S i 11 S 4 i 1 12 S i 5 Copyright 2016 2025 Jim Skrentny Dr Mahmood CS 354 S25 W11 L21 22 4 Stack Allocated Multidimensional Arrays Recall 2D Array Basics T A R C where T is the element datatype of size L bytes R is the number of rows and C is the number of columns Recall that 2D arrays are stored on the stack in int A 5 3 typedef int row t 3 row t A 5 Accessing 2D Arrays in Assembly A i j Given an array A as declared above if xA is in eax I is in ecx j is in edx then A i j in assembly is leal ecx ecx 2 ecx sall 2 edx addl eax edx movl edx ecx 4 eax Compiler Optimizations If only accessing part of array the compiler makes a pointer to If taking a fixed stride through the array the compiler uses Copyright 2016 2025 Jim Skrentny Dr Mahmood CS 354 S25 W11 L21 22 5 higher addresses earlier frames 1 v c 2 c 1 c 0 y x Stack Top 2 x y c 0 c 1 c 2 v Stack Allocated Structures Structures on the Stack struct iCell int x int y int c 3 int v How is a structure laid out on the stack Option 1 or 2 The compiler associates data member names with their uses address arithmetic with The first data member of a structure is closest to Accessing Structures in Assembly Given struct iCell ic assume ic is initialized void function iCell ip Assume ic is at the top of the stack edx stores ip and esi stores i For each of the following determine the assembly instruction to move the C code s value into eax Assembly C code 1 ic v 2 ic c i 3 ip x 4 ip y 5 ip c i Assembly code to access a structure does not have the data member names or types Copyright 2016 2025 Jim Skrentny Dr Mahmood CS 354 S25 W11 L21 22 6 Alignment Example Assume CPU reads 8 byte words f is a misaligned float Restrictions IA 32 has Linux short int float pointer double address must be a multiple of address must be a multiple of Windows same as Linux except double address must be a multiple of Implications Padding must be inserted by compiler into structs What Why Structure Example struct s1 int i char c int j The total size of a structure is typically a multiple of its Copyright 2016 2025 Jim Skrentny Dr Mahmood CS 354 S25 W11 L21 22 7 Alignment Practice For each structure below complete the memory layout and determine the total bytes allocated 1 struct sA int i int j char c 2 struct sB char a char b char c 3 struct sC char c short s int i char d 4 struct sD short s int i char c 5 struct sE int i short s char c The order that a structure s data members are listed can affect memory utilization because of Copyright …
View Full Document
Unlocking...