CMPT 295: Unit - Machine-Level Programming

Lecture 19:

Last lecture

First 6 arguments:

Name Register
argument 1 %rdi
argument 2 %rsi
argument 3 %rdx
argument 4 %rcx
argument 5 %r8
argument 6 %r9
return value %rax

Stack:

Register Stack Note
   
  argument n Stored onto the stack in reverse order
  Stored onto the stack in reverse order
  argument 8 Stored onto the stack in reverse order
%rsp argument 7 Stored onto the stack in reverse order

Today’s Menu

To recap …

3. Managing local data

Assembly 1 (x86-64 function call convention):

who:
  ...
  movq $15213, %rbx
  call amI
  addq %rbx, %rax
  ...
  ret

Assembly 2:

amI:
  ...
  subq $18213, %rbx
  ...
  ret

Register Table:

Register Value
%rbx  

3. Managing local data - “register saving” convention => callee saved registers

“register saving” conventions:

  1. calle saved registers

When we need space for our local data …

  1. Registers
    • A function can utilise unused registers (only when needed)
    • Some registers are referred to as callee saved registers:
    • %rbx, %rbp, %r12 to %r15 (and %ebx, %bx, %bl, …) * Callee saved registers means that … * the callee function must preserve the values of these registers before using them, * then restore their values before the control is returned (through the execution of ret instruction) to the caller function

3. Managing local data - “register saving” convention => callee saved registers

Callee saved registers:

Upon return from callee, caller can always assume that these registers still contain the values caller stored in them before calling callee!

3. Managing local data - “register saving” convention => caller saved registers

Register saving conventions:

  1. Callee saved registers
  2. Caller saved registers

  3. Registers (cont’d)
    • Some registers are referred to as caller saved registers:
    • %r10, %r11, %rax and all 6 registers used for passing data as arguments to callee (and %r10d, %r10w, %r10b, …) * Caller saved registers means that … * the caller function must preserve the values of these registers before …
    • setting up the callee‘s argument(s) into the appropriate “data passing as argument” register(s) and
    • calling the callee * then once the control is returned to the caller, the caller must restore their values before using them.

Managing local data - “register saving” convention => caller saved registers

caller saved registers:

Callee can always assume that caller has saved the content of these registers, so it is “safe” for callee to use them!

x86-64 “register saving” convention

Solution 1:

who:
  ...
  movq $15213, %rbx
  call amI
  addq %rbx, %rax
  ...
  ret

amI:
  subq $18213, %rbx
  ret

Solution 2:

who:
  ...
  movq $15213, %r10

  call amI

  addq %r10, %rax
  ...
  ret
amI:
  ...
  subq $18213, %r10
  ...
  ret
base + displacement Stack Variable Purpose
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

Register Table:

Register Value
   
   
   
   
   
   
   
   
   
   
   
   
   
   

3. Managing local data => spilling

Must remember to clean-up the stack before returning to caller.

Local variables on Stack – Example

long incr(long *p, long val)
{
  long x = *p;
  long y = x + val;
  *p = y;
  return x;
}
long call_incr() {
  long v1 = 15213;
  long v2 = incr(&v1, 3000);
  return v1+v2;
}

Assembly:

call_incr:
  subq $16, %rsp # highlighted
  movq $15213, 8(%rsp) # highlighted
  movl $3000, %esi
  leaq 8(%rsp), %rdi
  call incr
  addq 8(%rsp), %rax
  addq $16, %rsp
  ret
Register M[] Stack
%rsp  
base + displacement Stack Variable Purpose
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

Register Table:

Register Value
   
   
   
   
   
   
   
   
   
   
   
   
   
   

Summary - x86-64 “register saving” convention

callee saved registers:

Register Value Note
%rbx    
%r12    
%r13    
%r14    
%r15    
%rbp   Parameters/arguments
%rsp   return value

caller saved registers:

Register Value Note
%rax   Return value
%rax   Parameters/arguments
%rdi   Parameters/arguments
%rsi   Parameters/arguments
%rdx   Parameters/arguments
%rcx   Parameters/arguments
%r8   Parameters/arguments
%r9   Parameters/arguments
%r10    
%r11    

Summary - x86-64 conventions and stack frame

M[] Stack Note
caller frame
caller saved regs caller frame
args 7 … n caller frame
return address caller frame
callee saved regs callee frame
%rsp/Top/local vars callee frame

Next lecture