Assembly Language for Beginners

(Jeff_L) #1
1.27. POINTERS TO FUNCTIONS
The comparison function can be defined as:

int (*compare)(const void *, const void *)

Let’s use the following example:

1 / ex3 Sorting ints with qsort /
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int comp(const void _a, const void _b)
7 {
8 const int a=(const int )_a;
9 const int b=(const int )_b;
10
11 if (a==b)
12 return 0;
13 else
14 if (a < b)
15 return -1;
16 else
17 return 1;
18 }
19
20 int main(int argc, char argv[])
21 {
22 int numbers[10]={1892,45,200,-98,4087,5,-12345,1087,88,-100000};
23 int i;
24
25 /
Sort the array */
26 qsort(numbers,10,sizeof(int),comp) ;
27 for (i=0;i<9;i++)
28 printf("Number = %d\n",numbers[ i ]) ;
29 return 0;
30 }


1.27.1 MSVC


Let’s compile it in MSVC 2010 (some parts were omitted for the sake of brevity) with/Oxoption:

Listing 1.363: Optimizing MSVC 2010: /GS- /MD
__a$ = 8 ; size = 4
__b$ = 12 ; size = 4
_comp PROC
mov eax, DWORD PTR __a$[esp-4]
mov ecx, DWORD PTR __b$[esp-4]
mov eax, DWORD PTR [eax]
mov ecx, DWORD PTR [ecx]
cmp eax, ecx
jne SHORT $LN4@comp
xor eax, eax
ret 0
$LN4@comp:
xor edx, edx
cmp eax, ecx
setge dl
lea eax, DWORD PTR [edx+edx-1]
ret 0
_comp ENDP

_numbers$ = -40 ; size = 40
_argc$ = 8 ; size = 4
_argv$ = 12 ; size = 4
_main PROC
sub esp, 40 ; 00000028H
Free download pdf