inline-assembly

Syscall from inline asm in x86_64 Linux?

穿精又带淫゛_ 提交于 2019-11-29 07:47:35
问题 Why does this print garbage instead of exiting my program gracefully? I use system calls this way on BSD, and I wonder what would I need to make it work in Linux. int main(int argc, char **argv) { __asm ("movq $1,%rax; movq $0,%rdi; syscall"); /* exit(0) ? */ return 0; } Thanks. 回答1: Why does this print garbage instead of exiting my program gracefully? Per CESA-2009-001, "Syscall 1 is exit on i386 but write on x86_64". what would I need to make it work in Linux Use the syscall ordinals from

Calling a function in gcc inline assembly

走远了吗. 提交于 2019-11-29 07:23:58
Say, I want to call a function with the following signature in inline assembly of gcc. How can I do that? int some_function( void * arg ); Generally you'll want to do something like void *x; asm(".. code that writes to register %0" : "=r"(x) : ... int r = some_function(x); asm(".. code that uses the result..." : ... : "r"(r), ... That is, you don't want to do the function call in the inline asm at all. That way you don't have to worry about details of the calling conventions, or stack frame management. 来源: https://stackoverflow.com/questions/7984209/calling-a-function-in-gcc-inline-assembly

What does the declaration“extern struct cpu *cpu asm(“%gs:0”);” mean?

ぃ、小莉子 提交于 2019-11-29 07:12:43
When I'm reading the xv6 source code, I'm confused about the syntax of the declaration below. Can anyone explain it to me? extern struct cpu *cpu asm("%gs:0"); David Wohlferd I assume you understand what extern struct cpu *cpu means. The question you have is: What does the asm("%gs:0") part mean? This code is using a gcc extension called asm labels to say that the variable cpu is defined by the assembler string %gs:0 . This is NOT how this extension is intended to be used and is considered a hack . There's an excellent discussion of gs (and fs) here , but in short gs points to the current

Is inline asm part of the ANSI C standard?

浪尽此生 提交于 2019-11-29 04:22:40
I always thought it was but many IDEs and syntax highlighting tools do not highlight ASM in C, but they always do with C++. Is inline assembly part of the C standard (ANSII or ISO) or not? It's not in the ISO C standard (n1570 draft of C2011) as such, but mentioned in annex J (common extensions): J.5.10 The asm keyword 1 The asm keyword may be used to insert assembly language directly into the translator output (6.8). The most common implementation is via a statement of the form: asm ( character-string-literal ); Annex J is informative, not normative, so an implementation need not provide

64bit Applications and Inline Assembly

天大地大妈咪最大 提交于 2019-11-29 01:48:00
问题 I am using Visual C++ 2010 developing 32bit windows applications. There is something I really want to use inline assembly. But I just realized that visual C++ does not support inline assembly in 64bit applications. So porting to 64bit in the future is a big issue. I have no idea how 64bit applications are different from 32bit applications. Is there a chance that 32bit applications will ALL have to be upgraded to 64bit in the future? I heard that 64bit CPUs have more registers. Since

Is there a way to insert assembly code into C?

拈花ヽ惹草 提交于 2019-11-28 16:29:40
I remember back in the day with the old borland DOS compiler you could do something like this: asm { mov ax,ex etc etc... } Is there a semi-platform independent way to do this now? I have a need to make a BIOS call, so if there was a way to do this without asm code, that would be equally useful to me. Using GCC __asm__("movl %edx, %eax\n\t" "addl $2, %eax\n\t"); Using VC++ __asm { mov eax, edx add eax, 2 } In GCC, there's more to it than that. In the instruction, you have to tell the compiler what changed, so that its optimizer doesn't screw up. I'm no expert, but sometimes it looks something

C/C++ inline assembler with instructions in string variables

冷暖自知 提交于 2019-11-28 14:47:08
So as you know in C and C++ if using Visual-C you can have in line assembly instructions such as: int main() { printf("Hello\n"); __asm int 3 printf("this will not be printed.\n"); return 0; } Which will make a breakpoint inside of the executable. So my question is, is there somekind of function I can use to call __asm using a variable such as a char array. I was thinking something like this: char instruction[100] = "int 3"; __asm instruction But that doesn't seem to really work since it gives 'Invalid OP code'. So can you help with this or it isn't possible at all. Neither C nor C++ are

How to move double in %rax into particular qword position on %ymm or %zmm? (Kaby Lake or later)

送分小仙女□ 提交于 2019-11-28 14:36:33
The idea is that I'd like to collect returned values of double into a vector register for processing for machine imm width at a time without storing back into memory first. The particular processing is a vfma with other two operands that are all constexpr , so that they can simply be summoned by _mm256_setr_pd or aligned/unaligned memory load from constexpr array . Is there a way to store double in %ymm at particular position directly from value in %rax for collecting purpose? The target machine is Kaby Lake. More efficient of future vector instructions are welcome also. Inline-assembly is

How to access C struct/variables from inline asm?

寵の児 提交于 2019-11-28 14:16:44
Consider the following code: int bn_div(bn_t *bn1, bn_t *bn2, bn_t *bnr) { uint32 q, m; /* Division Result */ uint32 i; /* Loop Counter */ uint32 j; /* Loop Counter */ /* Check Input */ if (bn1 == NULL) return(EFAULT); if (bn1->dat == NULL) return(EFAULT); if (bn2 == NULL) return(EFAULT); if (bn2->dat == NULL) return(EFAULT); if (bnr == NULL) return(EFAULT); if (bnr->dat == NULL) return(EFAULT); #if defined(__i386__) || defined(__amd64__) __asm__ (".intel_syntax noprefix"); __asm__ ("pushl %eax"); __asm__ ("pushl %edx"); __asm__ ("pushf"); __asm__ ("movl %eax, (bn1->dat[i])"); __asm__ ("xorl

Merit of inline-ASM rounding via putting float into int variable

99封情书 提交于 2019-11-28 14:14:41
I have inherited a pretty interesting piece of code: inline int round(float a) { int i; __asm { fld a fistp i } return i; } My first impulse was to discard it and replace calls with (int)std::round (pre-C++11, would use std::lround if it happened today), but after a while I started to wonder if it might have some merit after all... The use case for this function are all values in [-100, 100] , so even int8_t would be wide enough to hold the result. fistp requires at least a 32 bit memory variable, however, so less than int32_t is just as wasted as more. Now, quite obviously casting the float