
CS485G Spring 2015 21
1 int countOnes(unsigned x)
2 {
3 int result = 0;
4 loop:
5 result += x & 0x1;
6 x >>= 1;
7 if (x)
8 goto loop;
9 return result;
10 }
3. Partial assembler listing
movl 8(%esp),%edx # d = x
movl $0, %ecx # result = 0
.L2: # loop:
movl %edx, %eax # a = x
andl $1, %eax # a = x & 1
addl %eax, %ecx # result += x & 1
shrl $1, %edx # x >>= 1
jne .L2 # If !0, goto loop
movl %ecx, %eax # a = result
4. for loops are very similar
5. The compiler can generate better code by replacing the uncondi-
tional 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
Kommentare zu diesen Handbüchern