inline-assembly

What happens to registers when you manipulate them using asm code in C++?

我是研究僧i 提交于 2021-02-20 03:00:07
问题 Some code: int x = 1; for(int i = 1; i < 10; i++) { x *= i; __asm { mov eax, x }; } If this program uses eax in order to increase the value of i , what will happen when I manipulate eax ? Will the compiler save registers from before the __asm call and use them after the asm code was executed or will it ignore that eax was manipulated and continue producing some sort of strange behavior? What happens to eax internally? EDIT: Even if my code only works with Visual C++ I want to know what

What happens to registers when you manipulate them using asm code in C++?

北城余情 提交于 2021-02-20 03:00:07
问题 Some code: int x = 1; for(int i = 1; i < 10; i++) { x *= i; __asm { mov eax, x }; } If this program uses eax in order to increase the value of i , what will happen when I manipulate eax ? Will the compiler save registers from before the __asm call and use them after the asm code was executed or will it ignore that eax was manipulated and continue producing some sort of strange behavior? What happens to eax internally? EDIT: Even if my code only works with Visual C++ I want to know what

gcc inline assembly behave strangely

主宰稳场 提交于 2021-02-19 08:25:11
问题 I am learning GCC's extended inline assembly currently. I wrote an A + B function and wants to detect the ZF flag, but things behave strangely. The compiler I use is gcc 7.3.1 on x86-64 Arch Linux. I started from the following code, this code will correctly print the a + b . int a, b, sum; scanf("%d%d", &a, &b); asm volatile ( "movl %1, %0\n" "addl %2, %0\n" : "=r"(sum) : "r"(a), "r"(b) : "cc" ); printf("%d\n", sum); Then I simply added a variable to check flags, it gives me wrong output. int

How to declare and initialize local variables in gcc inline assembly without extended inline asm?

孤人 提交于 2021-02-18 12:20:14
问题 I know this is a very basic question but I am really stuck on it. In fact I am absolutely newbie in GCC syntax. I want to have local variables (Stack addresses with labels) without using extended inline assembly. Something like the following code in Intel syntax: DATA1 DB 100 MOV AL, DATA1 This is the code I guess may substitute in GCC: int someFunction(int x) { __asm__ volatile( "function1:" ".data;" ".2byte $4 data1 ;" ".text;" "pushq %rbp;" "movq %rsp , %rbp ;" "movl var , %eax;" // this

Inline assembly multiplication “undefined reference” on inputs

风流意气都作罢 提交于 2021-02-17 07:15:08
问题 Trying to multiply 400 by 2 with inline assembly, using the fact imul implicity multiplies by eax . However, i'm getting "undefined reference" compile errors to $1 and $2 int c; int a = 400; int b = 2; __asm__( ".intel_syntax;" "mov eax, $1;" "mov ebx, $2;" "imul %0, ebx;" ".att_syntax;" : "=r"(c) : "r" (a), "r" (b) : "eax"); std::cout << c << std::endl; 回答1: Do not use fixed registers in inline asm, especially if you have not listed them as clobbers and have not made sure inputs or outputs

Inline assembly statements in C code and extended ASM for ARM Cortex architectures

你。 提交于 2021-02-17 05:18:30
问题 I am trying to compile the following two pieces of code with ARM Compiler 5 for a Cortex A microprocessor: Part 1 : static inline void cp15_write_sctlr(uint32_t value) { asm("mcr p15, 0, %0, c1, c0, 0" :: "r"(value)); } static inline uint32_t cp15_read_actlr(void) { uint32_t actlr; asm("mrc p15, 0, %0, c1, c0, 1" : "=r"(actlr)); return actlr; } Part 2 : static inline void dmb(void) { asm("dmb" ::: "memory"); } static inline void dsb(void) { asm("dsb" ::: "memory"); } static inline void isb

How do I call hex data stored in an array with inline assembly?

你。 提交于 2021-02-10 11:56:32
问题 I have an OS project that I am working on and I am trying to call data that I have read from the disk in C with inline assembly. I have already tried reading the code and executing it with the assembly call instruction, using inline assembly. void driveLoop() { uint16_t sectors = 31; uint16_t sector = 0; uint16_t basesector = 40000; uint32_t i = 40031; uint16_t code[sectors][256]; int x = 0; while(x==0) { read(i); for (int p=0; p < 256; p++) { if (readOut[p] == 0) { } else { x = 1; //kprint

How do I call hex data stored in an array with inline assembly?

[亡魂溺海] 提交于 2021-02-10 11:55:11
问题 I have an OS project that I am working on and I am trying to call data that I have read from the disk in C with inline assembly. I have already tried reading the code and executing it with the assembly call instruction, using inline assembly. void driveLoop() { uint16_t sectors = 31; uint16_t sector = 0; uint16_t basesector = 40000; uint32_t i = 40031; uint16_t code[sectors][256]; int x = 0; while(x==0) { read(i); for (int p=0; p < 256; p++) { if (readOut[p] == 0) { } else { x = 1; //kprint

How can I indicate that the memory *pointed* to by an inline ASM argument may be used?

房东的猫 提交于 2021-02-10 06:56:53
问题 Consider the following small function: void foo(int* iptr) { iptr[10] = 1; __asm__ volatile ("nop"::"r"(iptr):); iptr[10] = 2; } Using gcc, this compiles to: foo: nop mov DWORD PTR [rdi+40], 2 ret Note in particular, that the first write to iptr , iptr[10] = 1 doesn't occur at all: the inline asm nop is the first thing in the function, and only the final write of 2 appears (after the ASM call). Apparently the compiler decides that it only needs to provide an up-to-date version of the value of

How can I indicate that the memory *pointed* to by an inline ASM argument may be used?

百般思念 提交于 2021-02-10 06:56:16
问题 Consider the following small function: void foo(int* iptr) { iptr[10] = 1; __asm__ volatile ("nop"::"r"(iptr):); iptr[10] = 2; } Using gcc, this compiles to: foo: nop mov DWORD PTR [rdi+40], 2 ret Note in particular, that the first write to iptr , iptr[10] = 1 doesn't occur at all: the inline asm nop is the first thing in the function, and only the final write of 2 appears (after the ASM call). Apparently the compiler decides that it only needs to provide an up-to-date version of the value of