inline-assembly

Access array defined in inline assembler from C

耗尽温柔 提交于 2020-01-24 17:09:06
问题 I have an integer declared in Assembler, and I use it in C in the following way: asm( "number: \n" ".long 0xFFFFFFFF \n ); extern int number; int main(){ //do something with number } Now I want to declare a 32 byte array in Assembler. I tried the following: asm( "number: \n" ".long 0xFFFFFFFF \n" ".long 0xFFFFFFFF \n" ".long 0xFFFFFFFF \n" ".long 0xFFFFFFFF \n" ".long 0xFFFFFFFF \n" ".long 0xFFFFFFFF \n" ".long 0xFFFFFFFF \n" ".long 0xFFFFFFFF \n" ); extern unsigned char* number; int main() {

One thread counting, other thread does a job and measurement

别说谁变了你拦得住时间么 提交于 2020-01-24 14:10:32
问题 I would like to implement a 2 thread model where 1 is counting (infinitely increment a value) and the other one is recording the first counter, do the job, record the second recording and measure the time elapsed between. Here is what I have done so far: // global counter register unsigned long counter asm("r13"); // unsigned long counter; void* counter_thread(){ // affinity is set to some isolated CPU so the noise will be minimal while(1){ //counter++; // Line 1* asm volatile("add $1, %0" :

One thread counting, other thread does a job and measurement

て烟熏妆下的殇ゞ 提交于 2020-01-24 14:10:11
问题 I would like to implement a 2 thread model where 1 is counting (infinitely increment a value) and the other one is recording the first counter, do the job, record the second recording and measure the time elapsed between. Here is what I have done so far: // global counter register unsigned long counter asm("r13"); // unsigned long counter; void* counter_thread(){ // affinity is set to some isolated CPU so the noise will be minimal while(1){ //counter++; // Line 1* asm volatile("add $1, %0" :

what is the equivalent of _emit in MASM

只谈情不闲聊 提交于 2020-01-24 09:49:26
问题 I'm trying to port some inline assembly code written in Visual Studio into MASM64. The original code uses _emit which is a pseudo instruction that defines one byte at the current location in the current text segment. How would I do the same in x64 assembly MASM? 回答1: You can just use db , as in: db 10h You do this most often in a data segment, unless things have changed in the 64-bit version of MASM, it should work in the code segment as well. 来源: https://stackoverflow.com/questions/6916050

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

空扰寡人 提交于 2020-01-19 18:08:05
问题 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

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

烂漫一生 提交于 2020-01-19 18:07:37
问题 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

get string length in inline GNU Assembler

风流意气都作罢 提交于 2020-01-18 17:55:06
问题 I am re-learning assembler which I used on very old MS-DOS machines!!! This is my understanding of what that function should look like. It compiles but crashes with a SIGSEGV when trying to put 0xffffffff in ecx . The code is run in a VM with 32-bit Debian 9. Any help would be appreciated. int getStringLength(const char *pStr){ int len = 0; char *Ptr = pStr; __asm__ ( "movl %1, %%edi\n\t" "xor %%al, %%al\n\t" "movl 0xffffffff, %%ecx\n\t" "repne scasb\n\t" "subl %%ecx,%%eax\n\t" "movl %%eax,%0

Read the value of a cpu control register from admin privilege app (Windows)

徘徊边缘 提交于 2020-01-15 04:59:09
问题 I am trying to read data from a cpu control register using inline assembly. Im initially targeting x86-64. I'm not that familiar with c or assembly but ive managed to put together a very simple attempt as follows: #include <stdio.h> #include <stdint.h> int main() { uint64_t result; asm ("movq %%cr4, %0;" : "=r" (result) :: ); printf("result: %d \n", result); return 0; } This compiles but throws a runtime error in gdb: Thread 1 received signal SIGILL, Illegal instruction. main () at main.c:6 6

Why ret disappear with optimization?

亡梦爱人 提交于 2020-01-14 10:36:08
问题 int suma(int* array, int len) { asm(" xor %eax, %eax # resultado = 0 \n" " xor %edx, %edx # i = 0 \n" "1: add (%rdi,%rdx,4), %eax # res += array[i] \n" " inc %edx # ++i \n" " cmp %edx,%esi # i < len? \n" " jne 1b # repetir \n" // " ret \n" ); } int main() { int v[100]; return suma(v, 100); } Why is it that gcc inserts ret at the end of suma() on -O0 , but I have to add it myself on -O3 ? From gcc -v : gcc version 8.2.1 20181011 (Red Hat 8.2.1-4) (GCC) 回答1: I assume 64bit..., array in rdi, len

What does __asm volatile (“pause” ::: “memory”); do?

怎甘沉沦 提交于 2020-01-12 09:15:54
问题 I am looking at an open source C++ project which has the following code structure: while(true) { // Do something work if(some_condition_becomes_true) break; __asm volatile ("pause" ::: "memory"); } What does the last statement do? I understand that __asm means that it is an assembly instruction and I found some posts about pause instruction which say that the thread effectively hints the core to release resources and give other thread more resources (in context of hyper-threading). But what