CS485G Spring 2015 20
absDiff:
pushl %ebp # save base pointer
movl %esp,%ebp # new base pointer
movl 8(%ebp), %edx # d = x
movl 12(%ebp), %eax # a = y
cmpl %eax, %edx # x <> y?
jle .L6 # jump if x <= y
subl %eax, %edx # d = x - y
movl %edx, %eax # a = x - y
jmp .L7 # goto exitpoint
.L6: # elsepoint
subl %edx, %eax # a = y - x
.L7: # exitpoint
popl %ebp # restore %ebp
ret # return
11. C can sometimes use a single conditional expression to han-
dle such cases:
1 val = x>y ? x-y : y-x;
21 do while loops
1. C code to count how many 1’s in a parameter
1 int countOnes(unsigned x) {
2 int result = 0;
3 do {
4 result += x & 0x1;
5 x >>= 1;
6 } while (x);
7 return result;
8 }
2. Same thing, represented with goto
Kommentare zu diesen Handbüchern