CMPT 295
- Unit - Machine-Level Programming
- Lecture 15
- Assembly language
- Program Control
- Function Call and Stack
- Passing Control – cont’d
Happy Lunar New Year!
- 新年快乐 / 新年快樂 – Xīnnián kuàile
- Cung Chúc Tân Xuân
- Chúc Mừng Năm Mới
- 새해 복 많이 받으세요 – saehae bog manh-i bad-euseyo
- 过年好 / 過年好 – Guò nián hǎo
Homework
Memory Allocation Example
Where does everything go?
Code:
#include ... //shared libraries
char hugeArray[1 << 31]; /* 231 = 2GB */// data
int global = 0; // data
int useless(){ return 0; } // text
int main ()
{
void *ptr1, *ptr2; //stack
int local = 0; //stack
ptr1 = malloc(1 << 28); /* 228 = 256 MB*/ //heap
ptr2 = malloc(1 << 8); /* 28 = 256 B*///heap
/* Some print statements ... */
}
Stack in list form:
- Stack (down arrow)
- …
- Shared libraries
- …
- heap (up arrow)
- data
- Text
- …
Why 8?
- pushq src
- Fetch value of operand src
- Decrement %rsp by 8
- Write value at address given by %rsp
- popq dest
- Read value at %rsp (address) and store it in operand dest (must be register)
- Increment %rsp by 8
1) %rsp contains the memory address 0x0018
Register | Memory Address |
---|---|
%rsp | 0x0018 |
0x0010 | |
0x0008 |
2) %rsp contains the memory address ____
Register | Memory Address |
---|---|
0x0018 | |
%rsp | 0x0010 |
0x0008 |
Last Lecture
- Function call mechanisms: 1) passing control, 2) passing data, 3)
managing local data on memory (stack)
- Memory layout
- Stack (local variables …)
- Heap (dynamically allocated data)
- Data (statically allocated data)
- Text / Shared Libraries (program code)
- A “stack” is the right data structure for function call / return
- If multstore calls mult2, then mult2 returns before multstore returns
- x86-64 stack register and instructions: stack pointer %rsp, push and pop
(unless I’m missing something, this contains exactly the same slides as Lecture 14 here. Everything in the “example 1 steps X and Y” portion, read that for this section)
Summary
- Function call mechanisms: 1) passing control, 2) passing data, 3) managing local data on memory (stack)
- Memory layout
- Stack (local variables …)
- Heap (dynamically allocated data)
- Data (statically allocated data)
- Text / Shared Libraries (program code)
- A “stack” is the right data structure for function call / return
- If multstore calls mult2, then mult2 returns before multstore returns
- x86-64 stack register and instructions: stack pointer %rsp, push and pop
- x86-64 function call instructions: call and ret
Next Lecture
- Introduction
- C program -> assembly code -> machine level code
- Assembly language basics: data, move operation
- Memory addressing modes
- Operation leaq and Arithmetic & logical operations
- Conditional Statement – Condition Code + cmovX
- Loops
- Function call – Stack – Recursion
- Overview of Function Call
- Memory Layout and Stack - x86-64 instructions and registers
- Passing control
- (highlighted) Passing data – Calling Conventions
- Managing local data
- Recursion
- Array
- Buffer Overflow
- Floating-point operations