How are parameters accepted by standard c functions in assembly? [duplicate]












2
















This question already has an answer here:




  • What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

    3 answers



  • Where is the x86-64 System V ABI documented?

    1 answer




I have 64-bit MacOS assembly code which performs binary search on an array. Binary search in standard C library is:



void* bsearch (const void* key, const void* base,
size_t num, size_t size,
int (*compar)(const void*,const void*));


And my assembly looks like



mov    %edi,0x2(%rsp)
mov $0x2010,%r8d
mov $0x4,%ecx
lea 0x2(%rsp),%rdi
callq <bsearch@plt>


I am wondering if there is any definitive order of parameters that the bsearch takes, i.e. is there any way of knowing what rdi, ecx, r8d correspond to here? Is it key, base, compar?










share|improve this question















marked as duplicate by Peter Cordes assembly
Users with the  assembly badge can single-handedly close assembly questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 5:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • It depends on the calling convention for the platform you are on. For example the Calling Convention for 64-bit Windows is different than 64-bit Linux (or OSX) code. Looking at the code you provided I can infer you are not targeting Windows and you appear to be using a system that uses the AMD64 System V ABI. You can find a summary of the calling convention and the registers used here .A complete copy can be found here

    – Michael Petch
    Nov 24 '18 at 4:57













  • Thanks. I am using OSX.

    – dipankar
    Nov 24 '18 at 4:59











  • Yep, OSX uses the AMD64 System V ABI so the links I gave should apply.

    – Michael Petch
    Nov 24 '18 at 4:59











  • the linked duplicate covers the normal function calling convention as well as the system-call calling convention.

    – Peter Cordes
    Nov 24 '18 at 5:13
















2
















This question already has an answer here:




  • What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

    3 answers



  • Where is the x86-64 System V ABI documented?

    1 answer




I have 64-bit MacOS assembly code which performs binary search on an array. Binary search in standard C library is:



void* bsearch (const void* key, const void* base,
size_t num, size_t size,
int (*compar)(const void*,const void*));


And my assembly looks like



mov    %edi,0x2(%rsp)
mov $0x2010,%r8d
mov $0x4,%ecx
lea 0x2(%rsp),%rdi
callq <bsearch@plt>


I am wondering if there is any definitive order of parameters that the bsearch takes, i.e. is there any way of knowing what rdi, ecx, r8d correspond to here? Is it key, base, compar?










share|improve this question















marked as duplicate by Peter Cordes assembly
Users with the  assembly badge can single-handedly close assembly questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 5:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • It depends on the calling convention for the platform you are on. For example the Calling Convention for 64-bit Windows is different than 64-bit Linux (or OSX) code. Looking at the code you provided I can infer you are not targeting Windows and you appear to be using a system that uses the AMD64 System V ABI. You can find a summary of the calling convention and the registers used here .A complete copy can be found here

    – Michael Petch
    Nov 24 '18 at 4:57













  • Thanks. I am using OSX.

    – dipankar
    Nov 24 '18 at 4:59











  • Yep, OSX uses the AMD64 System V ABI so the links I gave should apply.

    – Michael Petch
    Nov 24 '18 at 4:59











  • the linked duplicate covers the normal function calling convention as well as the system-call calling convention.

    – Peter Cordes
    Nov 24 '18 at 5:13














2












2








2









This question already has an answer here:




  • What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

    3 answers



  • Where is the x86-64 System V ABI documented?

    1 answer




I have 64-bit MacOS assembly code which performs binary search on an array. Binary search in standard C library is:



void* bsearch (const void* key, const void* base,
size_t num, size_t size,
int (*compar)(const void*,const void*));


And my assembly looks like



mov    %edi,0x2(%rsp)
mov $0x2010,%r8d
mov $0x4,%ecx
lea 0x2(%rsp),%rdi
callq <bsearch@plt>


I am wondering if there is any definitive order of parameters that the bsearch takes, i.e. is there any way of knowing what rdi, ecx, r8d correspond to here? Is it key, base, compar?










share|improve this question

















This question already has an answer here:




  • What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

    3 answers



  • Where is the x86-64 System V ABI documented?

    1 answer




I have 64-bit MacOS assembly code which performs binary search on an array. Binary search in standard C library is:



void* bsearch (const void* key, const void* base,
size_t num, size_t size,
int (*compar)(const void*,const void*));


And my assembly looks like



mov    %edi,0x2(%rsp)
mov $0x2010,%r8d
mov $0x4,%ecx
lea 0x2(%rsp),%rdi
callq <bsearch@plt>


I am wondering if there is any definitive order of parameters that the bsearch takes, i.e. is there any way of knowing what rdi, ecx, r8d correspond to here? Is it key, base, compar?





This question already has an answer here:




  • What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

    3 answers



  • Where is the x86-64 System V ABI documented?

    1 answer








c assembly x86-64






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 5:10









Michael Petch

25.3k556101




25.3k556101










asked Nov 24 '18 at 4:42









dipankardipankar

294




294




marked as duplicate by Peter Cordes assembly
Users with the  assembly badge can single-handedly close assembly questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 5:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Peter Cordes assembly
Users with the  assembly badge can single-handedly close assembly questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 5:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • It depends on the calling convention for the platform you are on. For example the Calling Convention for 64-bit Windows is different than 64-bit Linux (or OSX) code. Looking at the code you provided I can infer you are not targeting Windows and you appear to be using a system that uses the AMD64 System V ABI. You can find a summary of the calling convention and the registers used here .A complete copy can be found here

    – Michael Petch
    Nov 24 '18 at 4:57













  • Thanks. I am using OSX.

    – dipankar
    Nov 24 '18 at 4:59











  • Yep, OSX uses the AMD64 System V ABI so the links I gave should apply.

    – Michael Petch
    Nov 24 '18 at 4:59











  • the linked duplicate covers the normal function calling convention as well as the system-call calling convention.

    – Peter Cordes
    Nov 24 '18 at 5:13



















  • It depends on the calling convention for the platform you are on. For example the Calling Convention for 64-bit Windows is different than 64-bit Linux (or OSX) code. Looking at the code you provided I can infer you are not targeting Windows and you appear to be using a system that uses the AMD64 System V ABI. You can find a summary of the calling convention and the registers used here .A complete copy can be found here

    – Michael Petch
    Nov 24 '18 at 4:57













  • Thanks. I am using OSX.

    – dipankar
    Nov 24 '18 at 4:59











  • Yep, OSX uses the AMD64 System V ABI so the links I gave should apply.

    – Michael Petch
    Nov 24 '18 at 4:59











  • the linked duplicate covers the normal function calling convention as well as the system-call calling convention.

    – Peter Cordes
    Nov 24 '18 at 5:13

















It depends on the calling convention for the platform you are on. For example the Calling Convention for 64-bit Windows is different than 64-bit Linux (or OSX) code. Looking at the code you provided I can infer you are not targeting Windows and you appear to be using a system that uses the AMD64 System V ABI. You can find a summary of the calling convention and the registers used here .A complete copy can be found here

– Michael Petch
Nov 24 '18 at 4:57







It depends on the calling convention for the platform you are on. For example the Calling Convention for 64-bit Windows is different than 64-bit Linux (or OSX) code. Looking at the code you provided I can infer you are not targeting Windows and you appear to be using a system that uses the AMD64 System V ABI. You can find a summary of the calling convention and the registers used here .A complete copy can be found here

– Michael Petch
Nov 24 '18 at 4:57















Thanks. I am using OSX.

– dipankar
Nov 24 '18 at 4:59





Thanks. I am using OSX.

– dipankar
Nov 24 '18 at 4:59













Yep, OSX uses the AMD64 System V ABI so the links I gave should apply.

– Michael Petch
Nov 24 '18 at 4:59





Yep, OSX uses the AMD64 System V ABI so the links I gave should apply.

– Michael Petch
Nov 24 '18 at 4:59













the linked duplicate covers the normal function calling convention as well as the system-call calling convention.

– Peter Cordes
Nov 24 '18 at 5:13





the linked duplicate covers the normal function calling convention as well as the system-call calling convention.

– Peter Cordes
Nov 24 '18 at 5:13












1 Answer
1






active

oldest

votes


















2














There are two possible calling conventions, depending on your OS (see here for more details). On Microsoft, the order is RCX, RDX, R8, R9. On Unix, the order is RDI, RSI, RDX, RCX, R8, R9. Note that the "r" or "e" at the beginning just indicates whether you are using all 64 bits of the register (r) or only the lower 32 bits (e). So in your case, I would guess that you are using Unix and the correspondence is rdi=key, ecx=size, r8d=compar.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    There are two possible calling conventions, depending on your OS (see here for more details). On Microsoft, the order is RCX, RDX, R8, R9. On Unix, the order is RDI, RSI, RDX, RCX, R8, R9. Note that the "r" or "e" at the beginning just indicates whether you are using all 64 bits of the register (r) or only the lower 32 bits (e). So in your case, I would guess that you are using Unix and the correspondence is rdi=key, ecx=size, r8d=compar.






    share|improve this answer




























      2














      There are two possible calling conventions, depending on your OS (see here for more details). On Microsoft, the order is RCX, RDX, R8, R9. On Unix, the order is RDI, RSI, RDX, RCX, R8, R9. Note that the "r" or "e" at the beginning just indicates whether you are using all 64 bits of the register (r) or only the lower 32 bits (e). So in your case, I would guess that you are using Unix and the correspondence is rdi=key, ecx=size, r8d=compar.






      share|improve this answer


























        2












        2








        2







        There are two possible calling conventions, depending on your OS (see here for more details). On Microsoft, the order is RCX, RDX, R8, R9. On Unix, the order is RDI, RSI, RDX, RCX, R8, R9. Note that the "r" or "e" at the beginning just indicates whether you are using all 64 bits of the register (r) or only the lower 32 bits (e). So in your case, I would guess that you are using Unix and the correspondence is rdi=key, ecx=size, r8d=compar.






        share|improve this answer













        There are two possible calling conventions, depending on your OS (see here for more details). On Microsoft, the order is RCX, RDX, R8, R9. On Unix, the order is RDI, RSI, RDX, RCX, R8, R9. Note that the "r" or "e" at the beginning just indicates whether you are using all 64 bits of the register (r) or only the lower 32 bits (e). So in your case, I would guess that you are using Unix and the correspondence is rdi=key, ecx=size, r8d=compar.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 4:57









        Flight OdysseyFlight Odyssey

        1,7231218




        1,7231218















            Popular posts from this blog

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

            Berounka

            I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...