**************************************************** * Example 1: * **************************************************** Dump of assembler code for function example_1: 0x80483e4 : push %ebp 0x80483e5 : mov %esp,%ebp 0x80483e7 : mov 0xc(%ebp),%eax 0x80483ea : add 0x8(%ebp),%eax 0x80483ed : mov %ebp,%esp 0x80483ef : pop %ebp 0x80483f0 : ret Dump of assembler code for function main: 0x80483f4
: push %ebp 0x80483f5 : mov %esp,%ebp 0x80483f7 : sub $0x8,%esp 0x80483fa : add $0xfffffff8,%esp 0x80483fd : push $0x2 0x80483ff : push $0x1 0x8048401 : call 0x80483e4 0x8048406 : add $0xfffffff8,%esp 0x8048409 : push %eax 0x804840a : push $0x8048478 0x804840f : call 0x8048308 0x8048414 : xor %eax,%eax 0x8048416 : mov %ebp,%esp 0x8048418 : pop %ebp 0x8048419 : ret #include int example_1 (int x, int y) { return x+y; } int main () { int result; result = example_1 (1, 2); printf ("%d\n", result); return 0; } **************************************************** * Example 2: * **************************************************** Dump of assembler code for function example_2: 0x8048420 : push %ebp 0x8048421 : mov %esp,%ebp 0x8048423 : sub $0x10,%esp 0x8048426 : push %esi 0x8048427 : push %ebx 0x8048428 : mov 0x8(%ebp),%ebx 0x804842b : cmp $0x2,%ebx 0x804842e : jg 0x8048437 0x8048430 : mov $0x1,%eax 0x8048435 : jmp 0x8048453 0x8048437 : add $0xfffffff4,%esp 0x804843a : lea 0xfffffffe(%ebx),%eax 0x804843d : push %eax 0x804843e : call 0x8048420 0x8048443 : mov %eax,%esi 0x8048445 : add $0xfffffff4,%esp 0x8048448 : lea 0xffffffff(%ebx),%eax 0x804844b : push %eax 0x804844c : call 0x8048420 0x8048451 : add %esi,%eax 0x8048453 : lea 0xffffffe8(%ebp),%esp 0x8048456 : pop %ebx 0x8048457 : pop %esi 0x8048458 : mov %ebp,%esp 0x804845a : pop %ebp 0x804845b : ret #include int example_2 (int n) {int result; if (n <= 2) result = 1; else result = example_2(n-2) + example_2(n-1); return result; } int main () { int n, result; printf ("n="); scanf ("%d", &n); result = example_2(n); printf ("example_2(%d) = %d\n", n, result); return 0; } **************************************************** * Example 3: * **************************************************** Dump of assembler code for function example_3: 0x80483f0 : push %ebp 0x80483f1 : mov %esp,%ebp 0x80483f3 : push %ebx 0x80483f4 : mov 0x8(%ebp),%ebx 0x80483f7 : mov 0xc(%ebp),%ecx 0x80483fa : xor %eax,%eax 0x80483fc : xor %edx,%edx 0x80483fe : cmp %ecx,%eax 0x8048400 : jge 0x804840a 0x8048402 : add (%ebx,%edx,4),%eax 0x8048405 : inc %edx 0x8048406 : cmp %ecx,%edx 0x8048408 : jl 0x8048402 0x804840a : pop %ebx 0x804840b : mov %ebp,%esp 0x804840d : pop %ebp 0x804840e : ret #include int example_3 (int x[], int num) { int ii, sum; sum = 0; for (ii=0; ii: push %ebp 0x8048435 : mov %esp,%ebp 0x8048437 : mov 0x8(%ebp),%edx 0x804843a : xor %eax,%eax 0x804843c : test %edx,%edx 0x804843e : je 0x8048449 0x8048440 : add 0x4(%edx),%eax 0x8048443 : mov (%edx),%edx 0x8048445 : test %edx,%edx 0x8048447 : jne 0x8048440 0x8048449 : mov %ebp,%esp 0x804844b : pop %ebp 0x804844c : ret #include typedef struct linked_list { struct linked_list *next; int data; } linked_list; linked_list a_list[5]; void init_a_list () { int ii; for (ii=0; ii<4; ii++) { a_list[ii].next = &(a_list[ii+1]); a_list[ii].data = ii+1; } a_list[4].next = NULL; a_list[4].data = 5; } int example_4 (linked_list *head) { int sum; sum = 0; while (head != NULL) { sum += head->data; head = head->next; } return sum; } int main () { init_a_list (); printf ("%d\n", example_4 (a_list)); return 0;