inline-assembly

Writing a Linux int 80h system-call wrapper in GNU C inline assembly [duplicate]

喜欢而已 提交于 2019-11-27 08:28:49
This question already has an answer here: How to invoke a system call via sysenter in inline assembly? 2 answers I'm trying to use inline assembly... I read this page http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx but I can't understand the parameters passing to my function. I'm writing a C write example.. this is my function header: write2(char *str, int len){ } And this is my assembly code: global write2 write2: push ebp mov ebp, esp mov eax, 4 ;sys_write mov ebx, 1 ;stdout mov ecx, [ebp+8] ;string pointer mov edx, [ebp+12] ;string size int 0x80 ;syscall leave ret What do I have

Address of labels (MSVC)

痴心易碎 提交于 2019-11-27 07:26:50
We are writing a byte-code for a high-level compiled language, and after a bit of profiling and optimization, it became clear that the current largest performance overhead is the switch statement we're using to jump to the byte-code cases. We investigated pulling out the address of each case label and storing it in the stream of byte-code itself, rather than the instruction ID that we usually switch on. If we do that, we can skip the jump table, and directly jump to the location of code of the currently executing instruction. This works fantastically in GCC, however, MSVC doesn't seem to

Unexpected GCC inline ASM behaviour (clobbered variable overwritten)

自闭症网瘾萝莉.ら 提交于 2019-11-27 07:10:30
问题 On my computer, the compiled executable omits executing "mov %2, %%ax" at the top of the loop when "add %1, %%ax" uncommented. Anyone to doublecheck or comment ? #include <stdio.h> int main() { short unsigned result, low ,high; low = 0; high = 1; __asm__ ( "movl $10, %%ecx \n\t" "loop: mov %2, %%ax \n\t" // "add %1, %%ax \n\t" // uncomment and result = 10 "mov %%ax, %0 \n\t" "subl $1, %%ecx \n\t" "jnz loop" : "=r" (result) : "r" (low) , "r" (high) : "%ecx" ,"%eax" ); printf("%d\n", result);

How do I specify immediate floating point numbers with inline assembly?

偶尔善良 提交于 2019-11-27 06:09:36
问题 When I try to compile this code: #include <stdio.h> main(int argc, char *argv[]) { double y = 0; __asm__ ("fldl $150;" "fsqrt;" "fstl %0;" : : "g" (y) ); printf("%f\n", y); return 0; } I get this error: sqrt.c: Assembler messages: sqrt.c:6: Error: suffix or operands invalid for `fld' Why doesn't this work? Why can't I push the number "150" onto the stack for floating point operations? 回答1: I do not know of an assembly language which supports literal floating point constants for immediate use.

Is there a way to insert assembly code into C?

↘锁芯ラ 提交于 2019-11-27 05:12:19
问题 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. 回答1: Using GCC __asm__("movl %edx, %eax\n\t" "addl $2, %eax\n\t"); Using VC++ __asm { mov eax, edx add eax, 2 } 回答2: In GCC, there's more to it than that. In the instruction, you have to tell

How to set a variable in GCC with Intel syntax inline assembly?

风格不统一 提交于 2019-11-27 04:51:34
Why doesn't this code set temp to 1? How do I actually do that? int temp; __asm__( ".intel_syntax;" "mov %0, eax;" "mov eax, %1;" ".att_syntax;" : : "r"(1), "r"(temp) : "eax"); printf("%d\n", temp); You want temp to be an output, not an input, I think. Try: __asm__( ".intel_syntax;" "mov eax, %1;" "mov %0, eax;" ".att_syntax;" : "=r"(temp) : "r"(1) : "eax"); This code does what you are trying to achieve. I hope this helps you: #include <stdio.h> int main(void) { /* Compile with C99 */ int temp=0; asm ( ".intel_syntax;" "mov %0, 1;" ".att_syntax;" : "=r"(temp) : /* no input*/ ); printf("temp=%d

calling assembly function from c

☆樱花仙子☆ 提交于 2019-11-27 04:43:50
问题 I'm trying to call an assembly function from c,but i keep getting errors. .text .globl integrate .type integrate, @function integrate: push %ebp mov %esp, %ebp mov $0,%edi start_loop: cmp %edi,1024 je loop_exit mov 8(%ebp),%eax mov 12(%ebp),%ecx sub %eax,%ecx add %edi,%ecx incl %edi jmp start_loop loop_exit: movl %ebp, %esp popl %ebp ret This is my assembly function,file called integrate.s. #include <stdio.h> extern int integrate(int from,int to); void main() { printf("%d",integrate(1,10)); }

How to access the control registers cr0,cr2,cr3 from a program? Getting segmentation fault

家住魔仙堡 提交于 2019-11-27 04:31:00
问题 I have written a program which tries to read from and write to the control registers. The program compiles fine, but when the inline assembly is about to be executed, it produces a segmentation fault. Code: void instructions(int val) { int i; int value; for(i = 0; i < val; i++) __asm__("mov %cr0, %eax"); } I used GDB and stepped through each assembly line and it is on the mov %cr0,%eax that the segmentation fault is occurring. Anyone who knows what is wrong? 回答1: Quoting from Intel® 64 and IA

Negative clock cycle measurements with back-to-back rdtsc?

…衆ロ難τιáo~ 提交于 2019-11-27 03:57:16
问题 I am writing a C code for measuring the number of clock cycles needed to acquire a semaphore. I am using rdtsc, and before doing the measurement on the semaphore, I call rdtsc two consecutive times, to measure the overhead. I repeat this many times, in a for-loop, and then I use the average value as rdtsc overhead. Is this correct, to use the average value, first of all? Nonetheless, the big problem here is that sometimes I get negative values for the overhead (not necessarily the averaged

How Do I Use Labels In GCC Inline Assembly?

耗尽温柔 提交于 2019-11-27 03:41:25
问题 I'm trying to learn x86-64 inline assembly and decided to implement this very simple swap method that simply orders a and b in ascending order: #include <stdio.h> void swap(int* a, int* b) { asm(".intel_syntax noprefix"); asm("mov eax, DWORD PTR [rdi]"); asm("mov ebx, DWORD PTR [rsi]"); asm("cmp eax, ebx"); asm("jle .L1"); asm("mov DWORD PTR [rdi], ebx"); asm("mov DWORD PTR [rsi], eax"); asm(".L1:"); asm(".att_syntax noprefix"); } int main() { int input[3]; scanf("%d%d%d", &input[0], &input[1