calling-convention

How to “goto” into different function in c?

旧城冷巷雨未停 提交于 2019-11-27 02:34:00
问题 Basically I am trying to simulate assembly code in C. Here is the C code: int main() { test(); main_next: printf("Hello, World!"); } void test() { goto main_next; } Trying to compile this code (Linux 32 bit, gcc 4.6.3), I got this error: error: label ‘main_randomtag_next’ used but not defined Does anyone know how to do this kind of inter-procedural goto in C? Thank you! 回答1: But what about the children? stack? goto between functions doesn't make any sense if you think about the stack. What

Pass function as a parameter

南楼画角 提交于 2019-11-27 01:55:56
I've written function 'A' that will call one of a number of other functions. To save re-writing function 'A', I'd like to pass the function to be called as a parameter of function 'A'. For example: function A{ Param($functionToCall) Write-Host "I'm calling : $functionToCall" } function B{ Write-Host "Function B" } Function C{ write-host "Function C" } A -functionToCall C Returns: I'm calling: C I am expecting it to return: I'm calling: Function C. I've tried various things such as: Param([scriptblock]$functionToCall) Cannot convert System.String to ScriptBlock A -functionToCall $function:C

How are variable arguments implemented in gcc?

你说的曾经没有我的故事 提交于 2019-11-27 01:01:21
int max(int n, ...) I am using cdecl calling convention where the caller cleans up the variable after the callee returns. I am interested in knowing how do the macros va_end , va_start and va_arg work? Does the caller pass in the address of the array of arguments as the second argument to max? If you look at the way the C language stores the parameters on the stack, the way the macros work should become clear:- Higher memory address Last parameter Penultimate parameter .... Second parameter Lower memory address First parameter StackPointer -> Return address (note, depending on the hardware the

Why does IA-32 have a non-intuitive caller and callee register saving convention?

好久不见. 提交于 2019-11-26 23:25:28
问题 The common calling conventions for IA-32 say: • Callee-save registers %ebx, %esi, %edi, %ebp, %esp Callee must not change these. (Or restore the caller's values before returning.) • Caller-save registers %eax, %edx, %ecx, condition flags Caller saves these if it wants to preserve them. Callee can freely clobber. Why does this strange convention exist? Why not save all the registers before calling another function? Or have the callee save and restore everything with pusha / popa ? 回答1: Why

Calling printf in extended inline ASM

☆樱花仙子☆ 提交于 2019-11-26 20:57:22
I'm trying to output the same string twice in extended inline ASM in GCC , on 64-bit Linux. int main() { const char* test = "test\n"; asm( "movq %[test], %%rdi\n" // Debugger shows rdi = *address of string* "movq $0, %%rax\n" "push %%rbp\n" "push %%rbx\n" "call printf\n" "pop %%rbx\n" "pop %%rbp\n" "movq %[test], %%rdi\n" // Debugger shows rdi = 0 "movq $0, %%rax\n" "push %%rbp\n" "push %%rbx\n" "call printf\n" "pop %%rbx\n" "pop %%rbp\n" : : [test] "g" (test) : "rax", "rbx","rcx", "rdx", "rdi", "rsi", "rsp" ); return 0; } Now, the string is outputted only once. I have tried many things, but I

Why does the Mac ABI require 16-byte stack alignment for x86-32?

本小妞迷上赌 提交于 2019-11-26 20:13:48
I can understand this requirement for the old PPC RISC systems and even for x86-64, but for the old tried-and-true x86? In this case, the stack needs to be aligned on 4 byte boundaries only. Yes, some of the MMX/SSE instructions require 16byte alignments, but if that is a requirement of the callee, then it should ensure the alignments are correct. Why burden every caller with this extra requirement? This can actually cause some drops in performance because every call-site must manage this requirement. Am I missing something? Update: After some more investigation into this and some consultation

What is the meaning and usage of __stdcall?

本秂侑毒 提交于 2019-11-26 18:50:14
问题 I've come across __stdcall a lot these days. MSDN doesn't explain very clearly what it really means, when and why should it be used, if at all. I would appreciate if someone would provide an explanation, preferably with an example or two. 回答1: All functions in C/C++ have a particular calling convention. The point of a calling convention is to establish how data is passed between the caller and callee and who is responsible for operations such as cleaning out the call stack. The most popular

Changing a C# delegate's calling convention to CDECL

◇◆丶佛笑我妖孽 提交于 2019-11-26 17:46:09
问题 I have had this problem with C# when I was using DotNet1.1 The problem is this. I have an unmanaged dll, which has a function which takes a function pointer (among other arguments). When I declare the DLLImport in C# code, I pass a delegate. But the delegates in C# have stdcall calling convention whereas the unmanaged function expects a cdecl function pointer. Thus my naive approach resulted in crashes. Then I found the following: http://www.codeproject.com/KB/cs/cdeclcallback.aspx Some guy

Does each PUSH instruction push a multiple of 8 bytes on x64?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 17:14:19
问题 On x64, does each PUSH instruction push a multiple of 8 bytes? If not, how much does it push? Also, how much stack space does each function parameter consume? 回答1: No, but in practice, one always pushes an 8 byte value onto the stack. Function parameters consuming varying amounts of stack space depending on the size of the function parameter and whether it is passed in the stack, in the registers, or passed by reference. If one passes a function parameter in the stack by pushing , then the

glibc scanf Segmentation faults when called from a function that doesn't align RSP

僤鯓⒐⒋嵵緔 提交于 2019-11-26 16:48:46
When compiling below code: global main extern printf, scanf section .data msg: db "Enter a number: ",10,0 format:db "%d",0 section .bss number resb 4 section .text main: mov rdi, msg mov al, 0 call printf mov rsi, number mov rdi, format mov al, 0 call scanf mov rdi,format mov rsi,[number] inc rsi mov rax,0 call printf ret using: nasm -f elf64 example.asm -o example.o gcc -no-pie -m64 example.o -o example and then run ./example it runs, print: enter a number: but then crashes and prints: Segmentation fault (core dumped) So printf works fine but scanf not. What am I doing wrong with scanf so?