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” (updated)

int x and int y are arguments to function

in C:

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

Steps:

  1. Analyze the condition: x<y
  2. write cmpl %esi,%edi
  3. List all outcomes:
    • xy>0    x>yx-y > 0 \implies x>y: exits loop
    • xy<0    x<yx-y < 0 \implies x< y: loops
    • xy=0    x=yx-y = 0 \implies x=y: exits loop
  4. Which outcomes “exit loop”?
  5. Write for false condition i.e. g & e (jge)

in assembly:

loop: # x in %edi, y in %esi
  cmpl %esi,%edi # cmpl y,x
  jge endloop
  #stmts in loop
  jmp loop
endloop:
  ret

Loop Pattern 1

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

While loop – “jump-to-middle” (updated)

int x and int y are arguments to function

in C:

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

Steps:

  1. (shown in C code)
  2. (shwon in assembly)
  3. All possible values
    • xy>0    x>yx-y >0 \implies x>y: exits loop, g
    • xy<0    x<yx-y < 0 \implies x< y: loops, l
    • xy=0    x=yx-y = 0 \implies x=y: exits loops, e
  4. Consider outcome ???(can’t read) “loops”

in assembly:

  jmp test
loop:
  # stmts
test:
  cmpl %esi,%edi #cmpl y,x (step 2)
  jl loop (step 5)
  ret

Loop Pattern 2

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

Do While loop – “jump-to-middle” (updated)

int x and int y are arguments to function

in C:

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

in assembly:

loop:
  #stmts
test:
  jmpl %esi,%edi
  jl loop
  ret

Loop Pattern 2:

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

For loop (updated)

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;

Questions:

  1. Here are we coding the false condition first?
  2. Which loop patterns are we using?

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); In this situation "je" would also work. Do you see why?
  #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