Ts Lecture10
Ts Lecture10
Ts Lecture10
Modern processors are pretty much all RISC. Even CISC instruction sets (x86-
64) are translated to RISC microcode on chip prior to execution.
32-Bit General Purpose
Registers
• EAX – Accumulator
• EBX – Base
• ECX – Counter
• EDX – Data
AH (8 bits) AL (8 bits)
AX (16 bits)
AT&T Syntax
• gcc and gas use AT&T syntax:
– Opcode appended by type
•b – byte (1 byte)
•w – word (2 bytes)
•l – long (4 bytes)
•q – quad (8 bytes)
Data (Heap)
Data (Heap)
Globals
Text (Code)
0
Linux Address Space
Stack
• Calling Convention
– An agreement, usually created by a system's designers, on how
function calls should be implemented
• Stack
– A portion of memory managed in a last-in, first-out (LIFO)
fashion
• Function Call
– A control transfer to a segment of code that ends with a return to
the point in code immediately after where the call was made (the
return address)
Activation Records
• An object containing all the necessary
data for a function stored on the stack
– Storage for Function parameters
– Storage for Return address
– Storage for Return value
– Storage for Local variables
– Storage for Temporaries (spilled registers)
• Caller-Saved
– A piece of data (e.g., a register) that must be explicitly saved if it
needs to be preserved across a function call
• Callee-Saved
– A piece of data (e.g., a register) that must be saved by a called
function before it is modified, and restored to its original value
before the function returns
MIPS Calling Convention
• First 4 arguments $a0-$a3
– Remainder put on stack
...
Parameter 3
Parameter 2
EBP + 12
Parameter 1
Return Address
Saved EBP
EBP
Local 1
Local 2
EBP - 8
Local 3
Saved EDI
Saved ESI
ESP
...
Lower addresses
Remember this from Scoping?
#include <stdio.h>
>> gcc ./main.c
int* foo() {
./main.c: In function ‘foo’:
int x = 5;
./main.c:4: warning: function returns
return &x; address of local variable
} >> ./a.out
void bar() { int y = 10; } *p=5
int main() *p=10
{
int *p = foo();
printf("*p=%d\n", *p);
bar();
printf("*p=%d\n", *p);
return 0;
}
• The activation records for foo() and bar() landed on the same stack space
19