CMPT 295

Last Lecture

Today’s Menu

Homework: int max(int x, int y)

In C:

int max(int x,int y){
  int result = x;
  if(y>x)
    result=y;
  return result;
}

version 1 – with jX instruction

In Assembly: # x in %edi, y in %esi, result in %eax

max:
  movl %edi,%eax #result=x
  cmpl %edi,%esi #if y<=x then
  jle endif #return
  movl %esi,%eax #result=y
endif:
  ret

We branch (jump) when the condition (y > x) is false, i.e., when (y <= x) -> This technique is called “coding the false condition first” or ”taking care of …”

Conditional move instruction cmovX

What C code looks like when using conditional operator:

result=test?val2:val1;
return result;

What logic of assembly code looks like when using cmovX (expressed in C):

result=val1;
if (test) result = val2;
return result;

Example: cmovle Src,Dest

Alternative: int abs(int x)

In C:

int abs(int x){
  if(x<0)
    x=-x;
  return x;
}

in assembly: # x in %edi, result in %eax

abs:
  movl %edi,%eax # result = x
  negl %edi # x = -x
  cmpl $0,%eax # if x < 0 then
  cmovl %edi,%eax # result = -x
  ret

Advantage of conditional move cmovX

Note about branching:

What do we mean by “safe”?

Homework: Example: alternate int max(int x, int y)

version 2 – with cmovX instruction

In C:

int max(int x, int y){
  int result = x;
  if(y>x)
    result = y;
  return result;
}

In Assembly: # x in %edi, y in %esi, result in %eax

max:
  movl %edi,%eax #result=x
  cmpl %edi,%esi #if y>x then
  cmovg %esi,%eax #result=y
  ret

While loop – “coding the false condition first”

int x and int y are arguments to function

in C:

while(x<y){
  //stmts
}
return;

in assembly:

loop:
  _____
  _____
  _____
  _____ 
  _____
  _____
  _____
endloop:
  ret

Loop Pattern 1

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

While loop – “jump-to-middle”

int x and int y are arguments to function

in C:

while(x<y){
  //stmts
}
return;

in assembly:

loop:
  # stmts
test:
  ____
  ____
  ____
  ____
  ret

Loop Pattern 2

  goto test:
loop:
  # stmts
test:
  if cond true
    goto loop:
done:

Do While loop – “jump-to-middle”

int x and int y are arguments to function

in C:

do{
  stmts
} while(x<y);
return;

in assembly:

loop:
  #stmts
test:
  ____
  ____
  ____
  ____
  ____
  ret

Loop Pattern 2:

(deleted) goto test:
loop:
  stmts
test:
  if cond true
    goto loop:
done:

For loop

In C:

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

Becomes:

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

Which becomes, in Assembly:

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

Summary

Compiler can produce different instruction combinations when assembling the same C code.

cmp* and test* instructions set condition codes

Next Lecture