CMPT 295

Demo: alternative way of implementing if/else in assembly language

Last Lecture

While loop – Question from last lecture “coding the false condition first”

in C:

while(x<y){
  //stmts
}

in assembly: # x in %edi, y in %esi

loop:
  cmpl %edi, %esi
  jl endloop
  # stmts
  jmp loop
endloop:
  ret

Loop Pattern 1

loop:
  if cond false
    goto done:
  stmts
  goto loop:
done:

Would this assembly code be the equivalent of our C code?

For loop - Homework

In C:

format: for(initialization;condition testing;increment){
for(i=0;i<;i++){
  // stmts
}
return;

Becomes:

i=0; //initialization
while(i<n){//condition
  //stmts
  i++; //increment
}
return;

In Assembly:

  xorl %ecx, %ecx #initialization: %ecx (i) <- 0
loop:
  cmpl %edi, %ecx #i-n?0 testing
  jge endloop #i-n>=0 false condition
  #stmts
  incl %ecx #i++ increment
  jmp loop #loop again
endloop:
  ret

Today’s Menu

What happens when a function (caller) calls another function (callee)?

  1. Control is passed (PC is set) …
    • To the beginning of the code in callee function
    • Back to where callee function was called in caller function
  2. Data is passed …
    • To callee function via function parameter(s)
    • Back to caller function via return value
  3. Memory is …
    • Allocated during callee function execution
    • Deallocated upon return to caller function

Code example 1:

void who(...) {
  int sum = 0;
  ...
  y = amI(x);
  sum = x + y;
  return;
}

Code example 2:

int amI(int i)
{
  int t = 3*i;
  int v[10];
  ...
  return v[t];
}

Above mechanisms implemented with machine code instructions and described as a set of conventions (ISA)

Remember from Lecture 2: Closer look at memory

Compressed view of memory w/ addresses in cells:

Address M[]
size-8
... ... ... ... ... ... ... ... ...
0x0018
0x0010
0x0008
0x0000 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008

Memory Layout

Segments:

Address M[]
0x00007FFFFFFFFFFF Stack (down arrow)
  Shared Libraries
  Heap (up arrow)
  Data
0x0000000000400000 Text
0x0000000000000000  

Memory Allocation Example

Where does everything go?

#include ...
char hugeArray[1 << 31]; /* 231 = 2GB */
int global = 0;
int useless(){ return 0; }
int main ()
{
  void *ptr1, *ptr2;
  int local = 0;
  ptr1 = malloc(1 << 28); /* 228 = 256 MB*/
  ptr2 = malloc(1 << 8); /* 28 = 256 B*/

  /* Some print statements ... */
}
M[]
Stack (down arrow)
...
Shared Libraries
...
Heap (up arrow)
Data
Text

Closer look at function call pattern

A function may call a function, which may call a function, which may call a function, …

Code example 1:

who(...) {
  ...
  ...
  are();
  ...
  ...
}

Code example 2:

are(...) {
  ...
  you();
  ...
  you();
  ...
}

Example 3:

you(...) {
  ...
  ...
  ...
  ...
  ...
}

Stack

Definition: A stack is a last-in-first-out (LIFO) data structure with two characteristic operations:

Do not have access to anything except what is on (at) top.

Image of stack of dinner plates.

Closer look at stack

Diagram of stack: at the top of the stack is %rsp; the stack grows down. Memory addresses decreese as they go down the stack.

x86-64 stack instruction: push

Diagram of stack after 3 pushes (transcriber’s note: these diagrams are probably wrong, but I need to describe what is there):

Register M[] Stack Name
%rsp   Top

x86-64 stack instruction: pop

… we pop once.

After we pop once:

Register M[] stack Name
  data  
  data  
%rsp data Top
 

Passing control mechanism x86-64 instruction: call and ret

Effect: return address, i.e., the address of the instruction after call func (held in PC) is pushed onto the stack

Register M[] Stack Name
%rsp   Top

Passing control mechanism x86-64 instruction: call and ret

Effect: return address, i.e., the address of instruction after call func, is pop’ed from the stack and stored in PC

After returning from the call …

Register M[] Stack Name
  data  
%rsp   Top

Example

Example pt. 1, in C:

void multstore(long x, long y, long *dest) {
  long t = mult2(x, y);
  *dest = t;
  return;
}

Example pt. 1, in Assembly:

0000000000400540 <multistore>:
  400540: push %rbx #Save %rbx
  400541: mov %rdx,%rbx #save Dest
  400544: callq 400550 <mult2> #mult2(x,y)
  400549: mov %rax,(%rbx) #save as dest
  40054c: pop %rbx #restore %rbx
  40054d: retq #return

Example pt. 2, in C:

long mult2(long a, long b) {
  long s = a * b;
  return s;
}

Example pt. 2, in Assembly:

0000000000400550 <mult2>:
  400550: mov %rdi,%rax #a
  400553: imul %rsi,%rax #a*b
  400557: retq #return

Example – Steps 1 and 2

Stack:

Register M[] Stack Name
  ret address  
%rsp   Top

Registers:

Register Value
%rbx  
%rsp 0x120
%rax  
%rip 0x400540
%rdi  
%rsi  
%rdx  

Example – Steps 3 and 4

Stack:

Register M[] Stack Name
  ret address  
  %rbx  
%rsp   Top

Registers:

Register Value
%rbx  
%rsp 0x118
%rax  
%rip 400544
%rdi  
%rsi  
%rdx  

Example – Steps 5 and 6

Stack:

Register M[] Stack Name
  ret address  
  %rbx  
  0x400549  
%rsp   Top

Registers:

Register Value
%rbx  
%rsp 0x110
%rax  
%rip 0x400553
%rdi  
%rsi  
%rdx  

Example – Steps 7, 8 and 9

Stack:

Register M[] Stack Name
  ret address  
  %rbx  
%rsp   Top

Registers:

Register Value
%rbx  
%rsp 0x118
%rax  
%rip 0x400549
%rdi  
%rsi  
%rdx  

Summary

Next Lecture