Machine-Level Programming Ii: Arithmetic & Control
Machine-Level Programming Ii: Arithmetic & Control
Machine-Level Programming Ii: Arithmetic & Control
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
• 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
4
University of Texas at Austin
• 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
7
University of Texas at Austin
8
University of Texas at Austin
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
}
}
•
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
}
}
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
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
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
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
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
•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
21
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
27
University of Texas at Austin
29
University of Texas at Austin
30
University of Texas at Austin
31
University of Texas at Austin
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
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
Risky Computations
val
val =
= p
p ?
? *p
*p :
: 0;
0;
• Both values get computed
• May have undesirable effects
36
University of Texas at Austin
37
University of Texas at Austin
38
University of Texas at Austin
39
University of Texas at Austin
40
University of Texas at Austin
41
University of Texas at Austin
42
University of Texas at Austin
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
45
University of Texas at Austin
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
50
University of Texas at Austin
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
While Version
Init;
while (Test ) {
Body
Update;
}
52
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