Download Here - Computer Science Department

Transcript
CS485G Spring 2015
1
2
3
4
5
6
7
8
9
10
21
int countOnes(unsigned x)
{
int result = 0;
loop:
result += x & 0x1;
x >>= 1;
if (x)
goto loop;
return result;
}
3. Partial assembler listing
movl
movl
.L2:
movl
andl
addl
shrl
jne
movl
8(%esp),%edx
$0, %ecx
%edx, %eax
$1, %eax
%eax, %ecx
$1, %edx
.L2
%ecx, %eax
#
#
#
#
#
#
#
#
#
d = x
result = 0
loop:
a = x
a = x & 1
result += x & 1
x >>= 1
If !0, goto loop
a = result
4. for loops are very similar
5. The compiler can generate better code by replacing the unconditional jump at the end of the loop with a conditional jump.
22
Procedures
1. Lecture 12, 2/11/2015
2. In order to handle recursion, languages are compiled to use a stack.
3. Each invocation of a procedure pushes a new frame on the stack.
4. When the procedure returns, the frame is popped and the space it
occupied is available for another procedure.
5. The frame contains storage private to this instance of the procedure.
(a) return address