calling-convention

How do vararg functions find out the number of arguments in machine code?

心已入冬 提交于 2019-12-28 06:02:25
问题 How can variadic functions like printf find out the number of arguments they got? The amount of arguments obviously isn't passed as a (hidden) parameter (see a call to printf in asm example here). What's the trick? 回答1: The trick is that you tell them somehow else. For printf you have to supply a format string which even contains type information (which might be incorrect though). The way to supply this information is mainly user-contract and often error-prone. As for calling conventions:

Printing a number in assembly NASM using printf

非 Y 不嫁゛ 提交于 2019-12-25 00:39:16
问题 I've been trying to get this to print 12345 for a while now. Can anyone provide a hint as to what I should do? It will print the three lines of text, then on the fourth line prints "age", which I'm guessing is a remnant in the stack from line 2. bits 64 global main extern printf section .text main: ; function setup push rbp mov rbp, rsp sub rsp, 32 ; lea rdi, [rel message] mov al, 0 call printf ;above code correctly prints message ;where the issue lies push rbp mov rbp, rsp ;sub rsp, 32 mov

function calling convention with boost::function_types

岁酱吖の 提交于 2019-12-24 15:51:00
问题 I've just been experimenting with the boost::function_types library recently, and I've come across a bit of a snag. I want to find out the calling convention of a given function, however I'm not quite sure how to do this. Here's what I have so far: This produces an error about how it cannot find the *_cc tag values inside each if statement. I suspect it may have something to do with the way I'm defining the macros; the documentation isn't very clear about how to setup extra calling

stdcall calling convention and using pinvoke in C#

本秂侑毒 提交于 2019-12-24 12:44:20
问题 I created a DLL file which includes two empty functions below. extern "C" __declspec(dllexport) void __stdcall myFunc1() { // just empty function } extern "C" __declspec(dllexport) void __cdecl myFunc2() { // just empty function } In C#, I could call the functions using DLLImport attribute like below. [DllImport("myDLL", CallingConvention=CallingConvention.StdCall)] private extern static void myFunc1(); [DllImport("myDLL", CallingConvention=CallingConvention.Cdecl)] private extern static void

Do C compilers optimize away functions in assembly so they minimize use of the stack?

故事扮演 提交于 2019-12-23 01:54:34
问题 I am starting to learn assembly (x86-64 in NASM on OSX), and am now exploring how functions look in it. Most resources explaining how "calling conventions" work show examples along the lines of this: // c code MyFunction1(a, b); // assembly code main: push a push b push rbp ; save frame pointer on stack mov rsp, rbp ; save stack pointer in frame pointer xor rax, rax ; set function return value to 0. call _MyFunction mov rbp, rsp ; restore stack pointer pop rbp ; restore frame pointer ret ;

Calling Win32's Sleep function from assembly creates access violation error

ぃ、小莉子 提交于 2019-12-23 01:44:14
问题 I'm using MASM and Visual C++, and I'm compiling in x64. This is my C++ code: // include directive #include "stdafx.h" // external functions extern "C" int Asm(); // main function int main() { // call asm Asm(); // get char, return success _getch(); return EXIT_SUCCESS; } and my assembly code: extern Sleep : proc ; code segment .code ; assembly procedure Asm proc ; sleep for 1 second mov ecx, 1000 ; ecx = sleep time sub rsp, 8 ; 8 bytes of shadow space call Sleep ; call sleep add rsp, 8 ; get

Is `extern “C”` a part of the type of a function?

自作多情 提交于 2019-12-21 03:38:07
问题 I don't see any comment in the standard except linkage related things. Though the standard doesn't say anything about calling convention, the calling conventions might be different between C and C++ in the real world, so I expected that the types of a C function and a C++ function are different. But it seems not, especially in GCC. #include <type_traits> extern "C" { int c_func(int); } int cpp_func(int); static_assert(!std::is_same<decltype(c_func), decltype(cpp_func)>::value, "It should not

Question about Objective C calling convention and argument passing on ARM

烂漫一生 提交于 2019-12-20 23:29:31
问题 I want to know how objective C runtime handle arguments when I call a objective C method like [NSString stringWithFomat:@"%@, %@", @"Hello", @"World"] There are three arguments for this objective C call, how does it work compared to typical way on a ARM system. I have known register r0, r1, r2, r3 will hold first 4 arguments, how about there are additional arguments? How does it put them on a stack and pop them later? 回答1: For functions that returns a simple type: r0 = self (NSString) r1 =

What's the advantage of having nonvolatile registers in a calling convention?

泪湿孤枕 提交于 2019-12-19 10:06:58
问题 I'm programming a JIT compiler and I've been surprised to discover that so many of the x86-64 registers are nonvolatile (callee-preserved) in the Win64 calling convention. It seems to me that nonvolatile registers just amount to more work in all functions that could use these registers. This seems especially true in the case of numeric computations where you'd want to use many registers in a leaf function, say some kind of highly optimized matrix multiplication. However, only 6 of the 16 SSE

Why does GObject method still get called even if callback arguments don't match those in XML?

走远了吗. 提交于 2019-12-19 09:48:05
问题 Suppose I have a method like this <interface name="org.Test.ChildTest"> <!-- set_age(guint32 new_age): sets new age --> <method name="set_age"> <arg type="u" name="new_age" direction="in"/> </method> In my table of methods I have: { (GCallback) child_test_set_age, dbus_glib_marshal_child_test_BOOLEAN__UINT_POINTER, 0 } and the right GObject method signature is: gboolean child_test_set_age (ChildTest *childTest, guint ageIn, GError** error) Why does my method, child_test_set_age() , still get