0% found this document useful (0 votes)
3 views

Lecture Slides 04 044-x86-Conditionals

The document discusses conditionals and control flow in programming, highlighting how conditional branches can implement various control flow constructs in higher-level languages. It details x86 jump instructions, processor state, condition codes, and their implicit and explicit settings through arithmetic and comparison operations. Additionally, it explains how to read condition codes using SetX instructions to set a single byte based on the condition codes.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture Slides 04 044-x86-Conditionals

The document discusses conditionals and control flow in programming, highlighting how conditional branches can implement various control flow constructs in higher-level languages. It details x86 jump instructions, processor state, condition codes, and their implicit and explicit settings through arithmetic and comparison operations. Additionally, it explains how to read condition codes using SetX instructions to set a single byte based on the condition codes.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

University

 of  Washington  

Condi3onals  and  Control  Flow  


l A  condi3onal  branch  is  sufficient  to  implement  most  control  
flow  constructs  offered  in  higher  level  languages  
l if  (condi)on)  then  {...}  else  {…}  
l while  (condi)on)  {…}  
l do  {…}  while  (condi)on)  
l for  (ini)aliza)on;  condi)on;  itera)ve)  {...}  
 
l Uncondi3onal  branches  implement  some  related  control  flow  
constructs  
l break,  con)nue  

l In  x86,  we’ll  refer  to  branches  as  “jumps”  (either  condi3onal  


or  uncondi3onal)  

x86  
University  of  Washington  

Jumping  
¢ jX  Instruc3ons  
§ Jump  to  different  part  of  code  depending  on  condi)on  codes  

jX   Condi3on   Descrip3on  

jmp 1 Uncondi3onal  
je ZF Equal  /  Zero  
jne ~ZF Not  Equal  /  Not  Zero  
js SF Nega3ve  
jns ~SF Nonnega3ve  
jg ~(SF^OF)&~ZF Greater  (Signed)  
jge ~(SF^OF) Greater  or  Equal  (Signed)  
jl (SF^OF) Less  (Signed)  
jle (SF^OF)|ZF Less  or  Equal  (Signed)  
ja ~CF&~ZF Above  (unsigned)  
jb CF Below  (unsigned)  

x86  
University  of  Washington  

Processor  State  (IA32,  Par3al)  


¢ Informa3on  about   %eax
currently  execu3ng   %ecx
program  
%edx General  purpose  
§ Temporary  data   registers  
(  %eax,  …)   %ebx
§ Loca)on  of  run)me   %esi
stack   %edi
(  %ebp,%esp)  
%esp Current  stack  top  
§ Loca)on  of  current  
code  control  point   %ebp Current  stack  frame  
(  %eip  )  
§ Status  of  recent  tests   %eip Instruc3on  pointer  
(  CF,ZF,SF,OF  )  
CF ZF SF OF Condi3on  codes  
x86  
University  of  Washington  

Condi3on  Codes  (Implicit  Se[ng)  


¢ Single-­‐bit  registers  
CF    Carry  Flag  (for  unsigned)  SF    Sign  Flag  (for  signed)  
ZF    Zero  Flag    OF    Overflow  Flag  (for  signed)  
¢ Implicitly  set  (think  of  it  as  side  effect)  by  arithme3c  opera3ons
 Example: addl/addq Src,Dest    ↔ t = a+b
§ CF  set  if  carry  out  from  most  significant  bit  (unsigned  overflow)  
§ ZF  set  if  t == 0  
§ SF  set  if  t < 0 (as  signed)  
§ OF  set  if  two’s  complement  (signed)  overflow  
(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)  
¢ Not  set  by  lea  instruc3on  (beware!)  
¢ Full  documenta3on  (IA32):  h`p://www.jegerlehner.ch/intel/IntelCodeTable.pdf  

x86  
University  of  Washington  

Condi3on  Codes  (Explicit  Se[ng:  Compare)  


¢ Single-­‐bit  registers  
CF    Carry  Flag  (for  unsigned)  SF    Sign  Flag  (for  signed)  
ZF    Zero  Flag    OF    Overflow  Flag  (for  signed)  
¢ Explicit  Se[ng  by  Compare  Instruc3on  
cmpl/cmpq Src2,Src1
   cmpl b,a like  compu)ng a-b  without  seRng  des)na)on  
§ CF  set  if  carry  out  from  most  significant  bit  (used  for  unsigned  comparisons)  
§ ZF  set  if  a == b  
§ SF  set  if  (a-b) < 0 (as  signed)  
§ OF  set  if  two’s  complement  (signed)  overflow  
(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

x86  
University  of  Washington  

Condi3on  Codes  (Explicit  Se[ng:  Test)  


¢ Single-­‐bit  registers  
CF    Carry  Flag  (for  unsigned)  SF    Sign  Flag  (for  signed)  
ZF    Zero  Flag    OF    Overflow  Flag  (for  signed)  
¢ Explicit  Se[ng  by  Test  instruc3on  
testl/testq Src2,Src1  
testl b,a like  compu)ng a & b without  seRng  des)na)on    
§ Sets  condi)on  codes  based  on  value  of  Src1  &  Src2  
§ Useful  to  have  one  of  the  operands  be  a  mask  
§ ZF  set  if  a&b == 0
§ SF  set  if  a&b < 0

§ testl %eax, %eax


§ Sets  SF  and  ZF,  check  if  eax  is  +,0,-­‐  

x86  
University  of  Washington  

Reading  Condi3on  Codes  


¢ SetX  Instruc3ons  
§ Set  a  single  byte  to  0  or  1  based  on  combina)ons  of  condi)on  codes  

SetX Condi3on Descrip3on


sete ZF Equal  /  Zero
setne ~ZF Not  Equal  /  Not  Zero
sets SF Nega3ve
setns ~SF Nonnega3ve
setg ~(SF^OF)&~ZF Greater  (Signed)
setge ~(SF^OF) Greater  or  Equal  (Signed)
setl (SF^OF) Less  (Signed)
setle (SF^OF)|ZF Less  or  Equal  (Signed)
seta ~CF&~ZF Above  (unsigned)
setb CF Below  (unsigned)

x86  
University  of  Washington  

Reading  Condi3on  Codes  (Cont.)  


¢ SetX  Instruc3ons:     %eax %ah %al
Set  single  byte  to  0  or  1  based  on  combina)on  of  
condi)on  codes   %ecx %ch %cl

¢ One  of  8  addressable  byte  registers   %edx %dh %dl


§ Does  not  alter  remaining  3  bytes   %ebx %bh %bl
§ Typically  use  movzbl  to  finish  job   %esi
int gt (int x, int y) %edi
{
return x > y; %esp
} %ebp

Body:  y  at  12(%ebp),  x  at  8(%ebp)  


movl 12(%ebp),%eax # eax = y
cmpl %eax,8(%ebp) What  does  
# Compare x :each  
y of  
setg %al #these  
al = instruc3ons  
x > y do?  
movzbl %al,%eax # Zero rest of %eax
x86  
University  of  Washington  

Reading  Condi3on  Codes  (Cont.)  


¢ SetX  Instruc3ons:     %eax %ah %al
Set  single  byte  to  0  or  1  based  on  combina)on  of  
condi)on  codes   %ecx %ch %cl

¢ One  of  8  addressable  byte  registers   %edx %dh %dl


§ Does  not  alter  remaining  3  bytes   %ebx %bh %bl
§ Typically  use  movzbl  to  finish  job   %esi
int gt (int x, int y) %edi
{
return x > y; %esp
} %ebp

Body:  y  at  12(%ebp),  x  at  8(%ebp)  


movl 12(%ebp),%eax # eax = y
cmpl %eax,8(%ebp) # Compare x and y (x  –  y)  
setg %al # al = x > y
movzbl %al,%eax # Zero rest of %eax
x86  

You might also like