| Integer power example |
|
In the first lecture, I showed a small C program (that should also make sense to Java programmers), computing integer powers of 2, and its compilation to x86 machine code. Here is the example in full.
#include < stdio.h >
int testfun( int n ){
int res = 1;
while( n > 0 ){
n--;
res *= 2;
}
return res;
}
int main( int argc, char** argv ) {
for ( int i = 0; i < 10; ++i ) {
int res = testfun ( i );
printf ( "2^%i = %i\n", i, res );
}
return 0;
}
I compiled this program as follows. clang -S -Wall -O3 -std=c99 test.c
The clang compiler is free, open
source software and available for most platforms. The The resulting x86 machine code can be found here. Quite a blow-up in terms of size from the original program. |