If the function prototype is:

void qsort ( void * first, size_t number, size_t size, int ( * comparator ) ( const void *, const void * ) ); 

So how to call it in assembler? I tried something like that

 section .data a dd 1, 3, 2, 2 c dd 10 b dd 4 section .text CMAIN: ... push dword[comp] push dword[b] push dword[c] push dword[a] call qsort;программа вылетает на этом месте, дальше не идет add esp, 16 PRINT_DEC 4, [a + 4] xor eax, eax ret comp: push ebp mov ebp, esp mov edx, dword[ebp + 4] mov eax, dword[ebp + 8] sub eax, edx leave ret 

but in the end nothing works

  • why are the elements in array 4, and you pass the value of 10? Also note that the first parameter is a pointer, and the next 2 are values. however, you pass all 3 parameters in the same way ... In general, run under the debugger and see where you put it. distinguish values ​​from pointers (i.e. addresses where a variable / function is located) - Mike

1 answer 1

Working Example: (POASM, MASM compatible)

 .code public c main main: push comp push dword ptr [b] push dword ptr [c] push a call qsort add esp, 16 push a push f call vprintf add esp, 8 xor eax, eax ret comp: mov eax, dword ptr [esp+4] mov edx, dword ptr [esp+8] mov eax, dword ptr [eax] sub eax, dword ptr [edx] ret .data public a a: dd 1, 3, 2, 2 c: dd 4 b: dd 4 f: db "%i,%i,%i,%i",13,10,0 
  • Just correct the ads section for themselves. Compilers are a little different. - kisssko