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

Machine-Level Programming Ii: Arithmetic & Control

The document discusses machine-level programming II and covers arithmetic and control operations. It introduces complete addressing modes for memory, including base, index, scale and displacement. It also covers the leal instruction for address computation, arithmetic instructions like add, sub, imul, and control flow instructions. An example arithmetic expression is translated to assembly instructions to demonstrate the concepts.

Uploaded by

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

Machine-Level Programming Ii: Arithmetic & Control

The document discusses machine-level programming II and covers arithmetic and control operations. It introduces complete addressing modes for memory, including base, index, scale and displacement. It also covers the leal instruction for address computation, arithmetic instructions like add, sub, imul, and control flow instructions. An example arithmetic expression is translated to assembly instructions to demonstrate the concepts.

Uploaded by

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

MACHINE-LEVEL

PROGRAMMING II:
ARITHMETIC & CONTROL
University of Texas at Austin

Today
• Complete addressing mode, address computation
(leal)
• Arithmetic operations
• Control: Condition codes
• Conditional branches
• While loops

2
University of Texas at Austin

Complete Memory Addressing Modes

• Most General Form


• D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D]
• D: Constant “displacement” 1, 2, or 4 bytes
• Rb: Base register: Any of 8 integer registers
• Ri: Index register: Any, except for %esp
• Unlikely you’d use %ebp, either
• S: Scale: 1, 2, 4, or 8 (why these numbers?)

• Special Cases
• (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]
• D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D]
• (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]

3
University of Texas at Austin

Address Computation Examples


%edx 0xf000
%ecx 0x0100

Expression Address Computation Address


0x8(%edx) 0xf000 + 0x8 0xf008
(%edx,%ecx) 0xf000 + 0x100 0xf100
(%edx,%ecx,4) 0xf000 + 4*0x100 0xf400
0x80(,%edx,2) 2*0xf000 + 0x80 0x1e080

4
University of Texas at Austin

Address Computation Instruction


• leal Src,Dest
• Src is address mode expression
• Set Dest to address denoted by expression

• Uses
• Computing addresses without a memory reference
• E.g., translation of p = &x[i];
• Computing arithmetic expressions of the form x + k*y
• k = 1, 2, 4, or 8
•int
Example
int mul12(int
mul12(int x)
x)
Converted to ASM by compiler:
{
{ leal
leal (%eax,%eax,2),
(%eax,%eax,2), %eax
%eax ;t
;t <-
<- x+x*2
x+x*2
return
return x*12;
x*12; sall
sall $2,
$2, %eax
%eax ;return
;return t<<2
t<<2
}
}
5
University of Texas at Austin

Today
• Complete addressing mode, address computation
(leal)
• Arithmetic operations
• Control: Condition codes
• Conditional branches
• While loops

6
University of Texas at Austin

Some Arithmetic Operations


• Two Operand Instructions:
Format Computation
addl Src,Dest Dest = Dest + Src
subl Src,Dest Dest = Dest  Src
imull Src,Dest Dest = Dest * Src
sall Src,Dest Dest = Dest << Src Also called shll
sarl Src,Dest Dest = Dest >> Src Arithmetic
shrl Src,Dest Dest = Dest >> Src Logical
xorl Src,Dest Dest = Dest ^ Src
andl Src,Dest Dest = Dest & Src
orl Src,Dest Dest = Dest | Src
• Watch out for argument order!
• No distinction between signed and unsigned int (why?)

7
University of Texas at Austin

Some Arithmetic Operations


• One Operand Instructions
incl Dest Dest = Dest + 1
decl Dest Dest = Dest  1
negl Dest Dest =  Dest
notl Dest Dest = ~Dest

• See book for more instructions

8
University of Texas at Austin

Arithmetic Expression Example


arith:
pushl %ebp Set
int
int arith(int
arith(int x,
x, int
int y,
y, int z) movl
int z) %esp, %ebp Up
{
{
int
int t1
t1 =
= x+y;
x+y; movl 8(%ebp), %ecx
int
int t2
t2 =
= z+t1;
z+t1; movl 12(%ebp), %edx
int
int t3
t3 =
= x+4;
x+4; leal (%edx,%edx,2), %eax
int
int t4
t4 =
= y
y ** 48;
48; sall $4, %eax
int
int t5
t5 =
= t3
t3 ++ t4;
t4; leal 4(%ecx,%eax), %eax Body
int
int rval
rval =
= t2
t2 *
* t5;
t5; addl %ecx, %edx
return
return rval;
rval; addl 16(%ebp), %edx
}
} imull %edx, %eax

popl %ebp
ret Finish

9
University of Texas at Austin

Understanding arith •

int
int arith(int
arith(int x,
x, int
int y,
y, int
int z)
z) Offset •
{
{
int
int t1
t1 =
= x+y;
x+y; 16 z
int
int t2
t2 =
= z+t1;
z+t1; 12 y
int
int t3
t3 =
= x+4;
x+4;
int
int t4
t4 =
= y
y ** 48;
48; 8 x
int
int t5
t5 =
= t3
t3 ++ t4;
t4;
int 4 Rtn Addr
int rval
rval =
= t2
t2 *
* t5;
t5;
return
return rval;
rval; 0 Old %ebp %ebp
}
}

movl 8(%ebp), %ecx


movl 12(%ebp), %edx
leal (%edx,%edx,2), %eax
sall $4, %eax
leal 4(%ecx,%eax), %eax
addl %ecx, %edx
addl 16(%ebp), %edx
imull %edx, %eax
10
University of Texas at Austin

Understanding arith • Stack


int
int arith(int
arith(int x,
x, int
int y,
y, int
int z)
z) Offset •
{
{
int
int t1
t1 =
= x+y;
x+y; 16 z
int
int t2
t2 =
= z+t1;
z+t1; 12 y
int
int t3
t3 =
= x+4;
x+4;
int
int t4
t4 =
= y
y ** 48;
48; 8 x
int
int t5
t5 =
= t3
t3 ++ t4;
t4;
int 4 Rtn Addr
int rval
rval =
= t2
t2 *
* t5;
t5;
return
return rval;
rval; 0 Old %ebp %ebp
}
}

movl 8(%ebp), %ecx # ecx = x


movl 12(%ebp), %edx # edx = y
leal (%edx,%edx,2), %eax # eax = y*3
sall $4, %eax # eax *= 16 (t4)
leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)
addl %ecx, %edx # edx = x+y (t1)
addl 16(%ebp), %edx # edx += z (t2)
imull %edx, %eax # eax = t2 * t5 (rval)
11
University of Texas at Austin

Observations about arith


• Instructions in different order
int
int arith(int
arith(int x,
x, int
int y,
y, int
int z)
z)
{
from C code
{
int
int t1
t1 =
= x+y;
x+y; • Some expressions require
int
int t2
t2 =
= z+t1;
z+t1; multiple instructions
int
int t3
t3 =
= x+4;
x+4; • Some instructions cover
int
int t4
t4 =
= y
y ** 48;
48; multiple expressions
int
int t5
t5 =
= t3
t3 ++ t4;
t4;
int • Get exact same code when
int rval
rval =
= t2
t2 *
* t5;
t5;
return
return rval;
rval; compile:
}
} • (x+y+z)*(x+4+48*y)

movl 8(%ebp), %ecx # ecx = x


movl 12(%ebp), %edx # edx = y
leal (%edx,%edx,2), %eax # eax = y*3
sall $4, %eax # eax *= 16 (t4)
leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)
addl %ecx, %edx # edx = x+y (t1)
addl 16(%ebp), %edx # edx += z (t2)
imull %edx, %eax # eax = t2 * t5 (rval)
12
University of Texas at Austin

Another Example
logical:
pushl %ebp Set
int
int logical(int
logical(int x,
x, int
int y)
y) movl %esp,%ebp Up
{
{
int
int t1
t1 =
= x^y;
x^y; movl 12(%ebp),%eax
int
int t2
t2 =
= t1
t1 >>
>> 17;
17; xorl 8(%ebp),%eax
int
int mask
mask =
= (1<<13)
(1<<13) -- 7;
7; sarl $17,%eax
int
int rval
rval =
= t2
t2 &
& mask;
mask; Body
andl $8185,%eax
return
return rval;
rval;
}
} popl %ebp
ret Finish

movl 12(%ebp),%eax # eax = y


xorl 8(%ebp),%eax # eax = x^y (t1)
sarl $17,%eax # eax = t1>>17 (t2)
andl $8185,%eax # eax = t2 & mask (rval)

13
University of Texas at Austin

Another Example
logical:
int
int logical(int
logical(int x,
x, int
int y)
y) pushl %ebp Set
{
{ movl %esp,%ebp Up
int
int t1
t1 =
= x^y;
x^y;
int
int t2
t2 =
= t1
t1 >>
>> 17;
17; movl 12(%ebp),%eax
int
int mask
mask =
= (1<<13)
(1<<13) -- 7;
7; xorl 8(%ebp),%eax
int
int rval
rval =
= t2
t2 &
& mask;
mask; sarl $17,%eax Body
return
return rval;
rval; andl $8185,%eax
}
}
popl %ebp
ret Finish

movl 12(%ebp),%eax # eax = y


xorl 8(%ebp),%eax # eax = x^y (t1)
sarl $17,%eax # eax = t1>>17 (t2)
andl $8185,%eax # eax = t2 & mask (rval)

14
University of Texas at Austin

Another Example
logical:
int
int logical(int
logical(int x,
x, int
int y)
y) pushl %ebp
{ Set
{ movl %esp,%ebp
int Up
int t1
t1 =
= x^y;
x^y;
int
int t2
t2 =
= t1
t1 >>
>> 17;
17; movl 12(%ebp),%eax
int
int mask
mask =
= (1<<13)
(1<<13) -- 7;
7; xorl 8(%ebp),%eax
int
int rval
rval =
= t2
t2 &
& mask;
mask; sarl $17,%eax
return
return rval;
rval; Body
andl $8185,%eax
}
}
popl %ebp
ret Finish

movl 12(%ebp),%eax # eax = y


xorl 8(%ebp),%eax # eax = x^y (t1)
sarl $17,%eax # eax = t1>>17 (t2)
andl $8185,%eax # eax = t2 & mask (rval)

15
University of Texas at Austin

Another Example
int logical:
int logical(int
logical(int x,
x, int
int y)
y)
{ pushl %ebp Set
{
int movl %esp,%ebp
int t1
t1 =
= x^y;
x^y; Up
int
int t2
t2 =
= t1
t1 >>
>> 17;
17;
int movl 12(%ebp),%eax
int mask
mask =
= (1<<13)
(1<<13) -- 7;
7;
int xorl 8(%ebp),%eax
int rval
rval =
= t2
t2 &
& mask;
mask;
return sarl $17,%eax
return rval;
rval; Body
} andl $8185,%eax
}
popl %ebp
ret Finish
2
213 == 8192,
13
8192, 2
213 –– 7
13
7 == 8185
8185

movl 12(%ebp),%eax # eax = y


xorl 8(%ebp),%eax # eax = x^y (t1)
sarl $17,%eax # eax = t1>>17 (t2)
andl $8185,%eax # eax = t2 & mask (rval)

16
University of Texas at Austin

Today
• Complete addressing mode, address computation
(leal)
• Arithmetic operations
• Control: Condition codes
• Conditional branches
• Loops

17
University of Texas at Austin

Processor State (IA32, Partial)


• Information about %eax
currently executing %ecx
program %edx General purpose
• Temporary data %ebx registers
( %eax, … ) %esi
• Location of runtime %edi
stack
%esp Current stack top
( %ebp,%esp )
%ebp Current stack frame
• Location of current
code control point
%eip Instruction pointer
( %eip, … )
• Status of recent tests
CF ZF SF OF Condition codes
( CF, ZF, SF, OF )
18
University of Texas at Austin

Condition Codes (Implicit Setting)


• 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 arithmetic operations
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 instruction
• Full documentation (IA32), link on course website
19
University of Texas at Austin

Condition Codes (Explicit Setting: Compare)

• Explicit Setting by Compare Instruction


•cmpl/cmpq Src2, Src1
•cmpl b,a like computing a-b without setting destination

•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)

20
University of Texas at Austin

Condition Codes (Explicit Setting: Test)

• Explicit Setting by Test instruction


•testl/testq Src2, Src1
testl b,a like computing a&b without setting destination

•Sets condition codes based on value of Src1 & Src2


•Useful to have one of the operands be a mask

•ZF set when a&b == 0


•SF set when a&b < 0

21
University of Texas at Austin

Reading Condition Codes


• SetX Instructions
• Set single byte based on combinations of condition
codes
SetX Condition Description
sete ZF Equal / Zero
setne ~ZF Not Equal / Not Zero
sets SF Negative
setns ~SF Nonnegative
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)
22
University of Texas at Austin

Reading Condition Codes (Cont.)


%eax %ah %al
• SetX Instructions:
• Set single byte based on combination %ecx %ch %cl
of condition codes
• 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
int gt
gt (int
(int x,
x, int
int y)
y)
{
{
return
return x
x >
> y; %edi
Body }
y;
}
movl 12(%ebp),%eax # eax = y %esp
cmpl %eax,8(%ebp) # Compare x : y
setg %al # al = x > y
movzbl %al,%eax # Zero rest of %eax %ebp
23
University of Texas at Austin

Today
• Complete addressing mode, address computation
(leal)
• Arithmetic operations
• x86-64
• Control: Condition codes
• Conditional branches & Moves
• Loops

25
University of Texas at Austin

Jumping
• jX Instructions
• Jump to different part of code depending on condition codes

jX Condition Description
jmp 1 Unconditional
je ZF Equal / Zero
jne ~ZF Not Equal / Not Zero
js SF Negative
jns ~SF Nonnegative
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)

26
University of Texas at Austin

Conditional Branch Example


int
int absdiff(int
absdiff(int x, x, int
int y)
y) absdiff:
{
{ pushl %ebp
Setup
int
int result;
result; movl %esp, %ebp
if
if (x
(x >
> y)y) {
{ movl 8(%ebp), %edx
result
result = = x-y;
x-y; movl 12(%ebp), %eax
}
} else
else {{ cmpl %eax, %edx
result Body1
result = = y-x;
y-x; jle .L6
}
} subl %eax, %edx
return
return result;
result; movl %edx, %eax Body2a
}
} jmp .L7
.L6:
subl %edx, %eax Body2b
.L7:
popl %ebp
ret Finish

27
University of Texas at Austin

Conditional Branch Example


(Cont.)
int
int goto_ad(int
goto_ad(int x,
x, int
int y)
y)
{
{ absdiff:
int
int result;
result; pushl %ebp
Setup
if
if (x
(x <=
<= y)
y) goto
goto Else;
Else; movl %esp, %ebp
result
result == x-y;
x-y; movl 8(%ebp), %edx
goto
goto Exit;
Exit; movl 12(%ebp), %eax
Else:
Else: cmpl %eax, %edx
jle .L6 Body1
result
result == y-x;
y-x;
Exit:
Exit: subl %eax, %edx
return movl %edx, %eax Body2a
return result;
result;
}
} jmp .L7
.L6:
• C allows “goto” as means of subl %edx, %eax Body2b
transferring control .L7:
popl %ebp
• Closer to machine-level ret Finish
programming style
• Generally considered bad
coding style
28
University of Texas at Austin

Conditional Branch Example


(Cont.)
int
int goto_ad(int
goto_ad(int x,
x, int
int y)
y)
{
{ absdiff:
int
int result;
result; pushl %ebp
Setup
if
if (x
(x <=
<= y)
y) goto
goto Else;
Else; movl %esp, %ebp
result
result == x-y;
x-y; movl 8(%ebp), %edx
goto
goto Exit;
Exit; movl 12(%ebp), %eax
Else:
Else: cmpl %eax, %edx
jle .L6 Body1
result
result == y-x;
y-x;
Exit:
Exit: subl %eax, %edx
return movl %edx, %eax Body2a
return result;
result;
}
} jmp .L7
.L6:
subl %edx, %eax Body2b
.L7:
popl %ebp
ret Finish

29
University of Texas at Austin

Conditional Branch Example


(Cont.)
int
int goto_ad(int
goto_ad(int x,
x, int
int y)
y)
{
{ absdiff:
int
int result;
result; pushl %ebp
Setup
if
if (x
(x <=
<= y)
y) goto
goto Else;
Else; movl %esp, %ebp
result
result == x-y;
x-y; movl 8(%ebp), %edx
goto
goto Exit;
Exit; movl 12(%ebp), %eax
Else:
Else: cmpl %eax, %edx
jle .L6 Body1
result
result == y-x;
y-x;
Exit:
Exit: subl %eax, %edx
return movl %edx, %eax Body2a
return result;
result;
}
} jmp .L7
.L6:
subl %edx, %eax Body2b
.L7:
popl %ebp
ret Finish

30
University of Texas at Austin

Conditional Branch Example


(Cont.)
int
int goto_ad(int
goto_ad(int x,
x, int
int y)
y)
{
{ absdiff:
int
int result;
result; pushl %ebp
Setup
if
if (x
(x <=
<= y)
y) goto
goto Else;
Else; movl %esp, %ebp
result
result == x-y;
x-y; movl 8(%ebp), %edx
goto
goto Exit;
Exit; movl 12(%ebp), %eax
Else:
Else: cmpl %eax, %edx
jle .L6 Body1
result
result == y-x;
y-x;
Exit:
Exit: subl %eax, %edx
return movl %edx, %eax Body2a
return result;
result;
}
} jmp .L7
.L6:
subl %edx, %eax Body2b
.L7:
popl %ebp
ret Finish

31
University of Texas at Austin

General Conditional Expression Translation

C Code
val = Test ? Then_Expr : Else_Expr;

val
val =
= x>y
x>y ?
? x-y
x-y :
: y-x;
y-x;
• Test is expression returning
integer
Goto Version
nt • = 0 interpreted as false
nt == !Test;
!Test;
if
if (nt)
(nt) goto
goto Else;
Else; • ≠ 0 interpreted as true
val
val == Then_Expr;
Then_Expr;
goto
goto Done;
Done;
• Create separate code
Else:
Else: regions for then & else
val
val == Else_Expr;
Else_Expr;
Done:
Done:
expressions
.
. .. .
. • Execute appropriate one

32
University of Texas at Austin

Using Conditional Moves


• Conditional Move Instructions
• Instruction supports:
if (Test) Dest  Src
C Code
• Supported in post-1995 x86 processors
val = Test
• GCC does not always use them
? Then_Expr
• Wants to preserve compatibility with :
: Else_Expr;
Else_Expr;
ancient processors
• Enabled for x86-64
Goto Version
• Use switch –march=686 for IA32
• Why? tval
tval = Then_Expr;
= Then_Expr
result
result == Else_Expr;
Else_Expr;
• Branches are very disruptive to t
t == Test;
Test;
instruction flow through pipelines if
if (t)
(t) result
result == tval;
tval;
• Conditional move do not require control return
return result;
result;
transfer
33
University of Texas at Austin

Conditional Move Example: x86-


64
int
int absdiff(int
absdiff(int x, x, int
int y)
y) {
{
int
int result;
result;
if
if (x
(x >
> y)y) {
{
result
result = = x-y;
x-y;
}
} else
else {{
result
result = = y-x;
y-x;
}
}
return
return result;
result;
}
}

absdiff:
x in %edi movl %edi, %edx
subl %esi, %edx # tval = x-y
y in %esi
movl %esi, %eax
subl %edi, %eax # result = y-x
cmpl %esi, %edi # Compare x:y
cmovg %edx, %eax # If >, result = tval
ret

34
University of Texas at Austin

Bad Cases for Conditional Move


Expensive Computations
val
val =
= Test(x)
Test(x) ?
? Hard1(x)
Hard1(x) :
: Hard2(x);
Hard2(x);

• Both values get computed


• Only makes sense when computations are very simple

Risky Computations
val
val =
= p
p ?
? *p
*p :
: 0;
0;
• Both values get computed
• May have undesirable effects

Computations with side effects


val
val =
= x
x >
> 0
0 ?
? x*=7
x*=7 :
: x+=3;
x+=3;
• Both values get computed
• Must be side-effect free
35
University of Texas at Austin

Control transfer and basic blocks


• jmp, jxx, and call pushl %ebp
movl %esp, %ebp
instructions transfer movl 8(%ebp), %edx
processor control movl 12(%ebp), %eax
L1:
• Basic block: region of cmpl %eax, %edx
subl %eax, %edx
uninterrupted control movl %edx, %eax
• No transfers in subl %edx, %eax
L2:
• No transfers out

36
University of Texas at Austin

Control transfer and basic blocks


• jmp, jxx, and call pushl %ebp
movl %esp, %ebp
instructions transfer movl 8(%ebp), %edx
processor control movl 12(%ebp), %eax
L1:
• Basic block: region of cmpl %eax, %edx
subl %eax, %edx
uninterrupted control movl %edx, %eax
• No transfers in subl %edx, %eax
L2:
• No transfers out

37
University of Texas at Austin

Control transfer and basic blocks


• jmp, jxx, and call pushl %ebp
movl %esp, %ebp
instructions transfer movl 8(%ebp), %edx
processor control movl 12(%ebp), %eax
L1:
• Basic block: region of cmpl %eax, %edx
subl %eax, %edx
uninterrupted control movl %edx, %eax
• No transfers in subl %edx, %eax
jne L1
• No transfers out L2:

38
University of Texas at Austin

Control transfer and basic blocks


• jmp, jxx, and call pushl %ebp
movl %esp, %ebp
instructions transfer movl 8(%ebp), %edx
processor control movl 12(%ebp), %eax
L1:
• Basic block: region of cmpl %eax, %edx
subl %eax, %edx
uninterrupted control movl %edx, %eax
• No transfers in subl %edx, %eax
jne L1
• No transfers out L2:

39
University of Texas at Austin

Control transfer and basic blocks


• jmp, jxx, and call pushl %ebp
movl %esp, %ebp
instructions transfer movl 8(%ebp), %edx
processor control movl 12(%ebp), %eax
L1:
• Basic block: region of cmpl %eax, %edx
subl %eax, %edx
uninterrupted control jne L2
• No transfers in movl %edx, %eax
subl %edx, %eax
• No transfers out jne L1
L2:

40
University of Texas at Austin

Control transfer and basic blocks


• jmp, jxx, and call pushl %ebp
movl %esp, %ebp
instructions transfer movl 8(%ebp), %edx
processor control movl 12(%ebp), %eax
L1:
• Basic block: region of cmpl %eax, %edx
subl %eax, %edx
uninterrupted control jne L2
• No transfers in movl %edx, %eax
subl %edx, %eax
• No transfers out jne L1
L2:

41
University of Texas at Austin

Control transfer and basic blocks


• Basic blocks form a int x = 1;
int y = 1;
graph while (y < 1000) {
• Nodes: basic blocks y = x + y;
}
• Edges: control transfers printf(“%d\n”, y);
• BB graph often reflects
high-level programming
constructs

42
University of Texas at Austin

Control transfer and basic blocks


• Basic blocks form a int x = 1;
int y = 1;
graph
• Nodes: basic blocks
while (y < 1000) {
• Edges: control transfers y = x + y;
}
• BB graph often reflects
high-level programming
printf(“%d\n”, y);
constructs

43
University of Texas at Austin

Today
• Complete addressing mode, address computation
(leal)
• Arithmetic operations
• x86-64
• Control: Condition codes
• Conditional branches and moves
• Loops

44
University of Texas at Austin

“Do-While” Loop Example


C Code Goto Version
int
int pcount_do(unsigned
pcount_do(unsigned x)x) int
int pcount_do(unsigned
pcount_do(unsigned x)
x)
{
{ {
{
int
int result
result =
= 0;
0; int
int result
result = = 0;
0;
do
do {{ loop:
loop:
result
result +=
+= xx &
& 0x1;
0x1; result
result +=+= x
x && 0x1;
0x1;
x
x >>=
>>= 1;
1; x
x >>=
>>= 1;
1;
}
} while
while (x);
(x); if
if (x)
(x)
return
return result;
result; goto
goto loop;
loop;
}
} return
return result;
result;
}
}

• Count number of 1’s in argument x (“popcount”)


• Use conditional branch to either continue looping or to exit
loop

45
University of Texas at Austin

“Do-While” Loop Compilation


Goto Version
int
int pcount_do(unsigned
pcount_do(unsigned x)
x) {
{
int
int result
result = = 0;
0;
loop:
loop:
result
result +=+= x
x && 0x1;
0x1;
x
x >>=
>>= 1;
1;
if
if (x)
(x)
goto
goto loop;
loop;
return
return result;
result;
}
}

movl $0, %ecx # result = 0


• Registers: .L2: # loop:
%edx x movl %edx, %eax
%ecx result andl $1, %eax # t = x & 1
addl %eax, %ecx # result += t
shrl %edx # x >>= 1
jne .L2 # If !0, goto loop
46
University of Texas at Austin

General “Do-While” Translation


C Code Goto Version
do loop:
Body Body
while (Test); if (Test)
goto loop
• Body: {
Statement1;
Statement2;

Statementn;
}

• Test returns integer


• = 0 interpreted as false
• ≠ 0 interpreted as true 47
University of Texas at Austin

“While” Loop Example


C Code Goto Version
int
int pcount_while(unsigned
pcount_while(unsigned x)
x) {
{ int
int pcount_do(unsigned
pcount_do(unsigned x)
x) {
{
int
int result
result = = 0;
0; int
int result
result = = 0;
0;
while
while (x)
(x) {{ if
if (!x)
(!x) goto
goto done;
done;
result
result +=
+= x x &
& 0x1;
0x1; loop:
loop:
x
x >>=
>>= 1;
1; result
result +=+= xx &
& 0x1;
0x1;
}
} x
x >>=
>>= 1;
1;
return
return result;
result; if
if (x)
(x)
}
} goto
goto loop;
loop;
done:
done:
return
return result;
result;
}
}

• Is this code equivalent to the do-while version?


• Must jump out of loop if test fails
48
University of Texas at Austin

General “While” Translation


While version
while (Test)
Body

Goto Version
Do-While Version
if (!Test)
if (!Test) goto done;
goto done; loop:
do Body
Body if (Test)
while(Test); goto loop;
done: done:
49
University of Texas at Austin

“For” Loop Example


C Code
#define
#define WSIZE
WSIZE 8*sizeof(int)
8*sizeof(int)
int
int pcount_for(unsigned
pcount_for(unsigned x) x) {{
int
int i;
i;
int
int result
result = = 0;
0;
for
for (i
(i == 0;
0; ii <
< WSIZE;
WSIZE; i++)
i++) { {
unsigned
unsigned mask
mask = = 1
1 <<
<< i;
i;
result
result +=
+= (x
(x &
& mask)
mask) !=
!= 0;
0;
}
}
return
return result;
result;
}
}

• Is this code equivalent to


other versions?

50
University of Texas at Austin

“For” Loop Form


General Form Init
for (Init; Test; Update ) i
i =
= 0
0

Body Test
i
i <
< WSIZE
WSIZE

for
for (i
(i =
= 0;
0; ii <
< WSIZE;
WSIZE; i++)
i++) {
{ Update
unsigned
unsigned mask
mask == 1
1 <<
<< i;
i; i++
i++
result
result +=
+= (x
(x && mask)
mask) !=
!= 0;
0;
}
}
Body
{
{
unsigned
unsigned mask
mask == 1
1 <<
<< i;
i;
result
result +=
+= (x
(x &
& mask)
mask) !=
!= 0;
0;
}
}

51
University of Texas at Austin

“For” Loop  While Loop


For Version
for (Init; Test; Update )
Body

While Version
Init;
while (Test ) {
Body
Update;
}
52
University of Texas at Austin

“For” Loop  …  Goto Init;


Init;
if (!Test)
For Version goto done;
for (Init; Test; Update ) loop:
loop:
Body
Body Update
if (Test)
goto loop;
done:
While Version
Init;
Init;
Init;
while (Test ) { if (!Test)
Body goto done;
do
do
Update; Body
} Update
while(Test);
done:
53
University of Texas at Austin

“For” Loop Conversion Example


C Code Goto Version
int
int pcount_for_gt(unsigned
pcount_for_gt(unsigned x) x) {{
#define
#define WSIZE
WSIZE 8*sizeof(int)
8*sizeof(int) int
int i;
i;
int
int pcount_for(unsigned
pcount_for(unsigned x) x) {{
int
int
int result
result = 0; Init
= 0;
int i;
i; i
int i == 0;
0;
int result
result = = 0;
0; if !Test
for if (!(i
(!(i < < WSIZE))
WSIZE))
for (i
(i == 0;
0; ii <
< WSIZE;
WSIZE; i++)
i++) { { goto
unsigned goto done;
done;
unsigned mask
mask == 1
1 <<
<< i;
i; loop:
result loop: Body
result +=
+= (x
(x && mask)
mask) !=
!= 0;0; {
} {
} unsigned
return unsigned mask
mask = = 11 <<
<< i;
i;
return result;
result; result
} result +=+= (x
(x &
& mask)
mask) !=!= 0;
0;
} }
} Update
i++;
i++; Test
if (i < WSIZE)
if (i < WSIZE)
• Initial test can be optimized goto
goto loop;
loop;
done:
done:
away return
return result;
result;
}
}
54
University of Texas at Austin

Summary
• Today
• Complete addressing mode, address computation (leal)
• Arithmetic operations
• Control: Condition codes
• Conditional branches & conditional moves
• Loops
• Next Time
• Switch statements
• Stack
• Call / return
• Procedure call discipline

55

You might also like