calling-convention

Calling C function from x64 assembly with registers instead of stack

筅森魡賤 提交于 2019-12-07 22:12:25
问题 This answer puzzled me. According to the standard C calling conventions, the standard way to call C functions is to push arguments to the stack and to call the subroutine. That is clearly different from syscalls, where you set different registers with appropriate arguments and then syscall . However, the answer mentioned above gives this GAS code: .global main .section .data hello: .asciz "Hello\n" .section .text main: movq $hello, %rdi movq $0, %rax call printf movq $0, %rax ret which works

__cdecl results in larger executable than __stdcall?

青春壹個敷衍的年華 提交于 2019-12-07 07:28:53
问题 I found this: Because the stack is cleaned by the called function, the __stdcall calling convention creates smaller executables than __cdecl, in which the code for stack cleanup must be generated for each function call . Suppose I got 2 functions: void __cdecl func1(int x) { //do some stuff using x } void __stdcall func2(int x, int y) { //do some stuff using x, y } and here in the main() : int main() { func1(5); func2(5, 6); } IMO, it is main() 's responsibility to clean up the stack of the

Why did Microsoft choose stdcall as their API convention?

Deadly 提交于 2019-12-07 05:00:44
问题 Is there a good reason? Are their internal functions (not exported) also stdcall convention? 回答1: It was an adaptation to the pascal calling convention for 32-bit code. Pascal was the calling convention for 16-bit operating systems like OS/2 and Windows 3. Why pascal was chosen is a bit of a guess, even I was a small pup back then, but it is slightly more efficient. Which mattered back when 640 KB was all you had to work with. Most Win32 functions aren't true stdcall as it also prescribes how

x64 calling convention (stack) and varargs

橙三吉。 提交于 2019-12-07 04:47:00
问题 I've read Microsoft's documentation, but the scheme is so awkward, I thought I'd double-check to make sure I'm understanding it correctly... My understanding is the generic method by which parameters are passed is this: --- bottom of stack --- (return address) [shadow space for arg 1] [shadow space for arg 2] [shadow space for arg 3] [shadow space for arg 4] arg N arg N - 1 arg N - 2 ... arg 6 arg 5 ---- top of stack ----- It seems so awkward when implementing va_arg and such... is this

When to use calling conventions

孤人 提交于 2019-12-07 03:59:38
问题 What are the key factors on using different calling conventions? When does someone know to use specific calling conventions such as __cdecl or __stdcall or __fastcall in different occasions. Examples would be really apprciated. 回答1: Most of the time you don't need to worry about it. Usually you'll use __cdecl , but only because that's the default in Visual C++. C++ member functions, however, use the __thiscall convention by default in Visual C++ A (rather common) situation where you really

How to (cross-)compile to both ARM hard- and soft-float (softfp) with a single GCC (cross-)compiler?

跟風遠走 提交于 2019-12-07 02:49:09
问题 I'd like to use a single (cross-)compiler to compile code for different ARM calling conventions: since I always want to use floating point and NEON instructions, I just want to select the hard-float calling convention or the soft-float (softfp) calling convention. My compiler defaults to hard-float, but it supports both architectures that I need: $ arm-linux-gnueabihf-gcc -print-multi-lib .; arm-linux-gnueabi;@marm@march=armv4t@mfloat-abi=soft $ When I compile with the default parameters: $

Function parameter passing in a Linux kernel interrupt handler (from asm to C)

只愿长相守 提交于 2019-12-07 00:39:28
问题 When I read the Linux kernel source, I came across this piece of code: __visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs) { struct pt_regs *old_regs = set_irq_regs(regs); entering_ack_irq(); local_apic_timer_interrupt(); exiting_irq(); set_irq_regs(old_regs); } The function smp_apic_timer_interrupt() takes one parameter. The calling of this function is by a piece of assembly language code: ENTRY(apic_timer_interrupt) RING0_INT_FRAME; ASM_CLAC; pushl_cfi $~(0xef); SAVE

where is amd64 psABI? [closed]

不打扰是莪最后的温柔 提交于 2019-12-06 17:11:16
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . The AMD64 psABI used to be hosted at x86-64.org. I have a copy of pdf file and it says explicitly: The architecture specification is available on the web at http://www.x86-64.org/documentation. but http://www.x86-64.org is down for a long time already. Several months at least. Does anyone know where the latest

Can stdcall have a variable arguments?

落花浮王杯 提交于 2019-12-06 08:20:55
问题 As far as I know, only the caller-clean-stack convention can use variable arguments. By the way, the WinApi StringCchPrintfW is declared like this.(I removed the SAL) __inline HRESULT __stdcall StringCchPrintfW( STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszFormat, ... ); Can stdcall have a variable arguments either? 回答1: No. The stdcall calling convention has the callee clean the stack. Since the callee is cleaning the stack there is no way for it to know at compile time how

Writing naked functions with custom prolog and epilog code in Visual Studio

£可爱£侵袭症+ 提交于 2019-12-06 05:15:04
问题 I'm writing some plugin code in a dll that is called by a host over which I have no control. The host assumes that the plugins are exported as __stdcall functions. The host is told the name of the function and the details of the arguments that it expects and dynamically crufts up a call to it via LoadLibrary, GetProcAddress and manually pushing the arguments onto the stack. Usually plugin dlls expose a constant interface. My plugin exposes an interface that is configured at dll load time. To