inline-assembly

How to write multiple assembly statements within asm() without “\\t\\n” separating each line using GCC?

懵懂的女人 提交于 2019-12-06 16:11:14
How to write multiple assembly statements within asm() without "\t\n" separating each line using GCC? I've seen some textbooks write multiple assembly statements within asm() as: asm(" movl $4, %eax movl $2, %ebx addl %eax, %ebx ... "); However, my compiler (GCC) doesn't recognize this syntax. Instead, I must rely on "\t\n" separating each line or using multiple asm() : asm( "movl $4, %eax\t\n" "movl $2, %ebx\t\n" "addl %eax, %ebx\t\n" ...); or asm("movl $4, %eax"); asm("movl $2, %ebx"); asm("addl %eax, %ebx"); ... How do I enable the "clean" syntax with no "\t\n" or repeated asm() ? GCC Your

Why isn't my inline assembly in C++ working?

我的梦境 提交于 2019-12-06 13:01:11
问题 Strangest error output: #include <iostream> int main(int arg, char **LOC[]) { asm ( "mov eax, 0CF;" "pusha;" ); return 0; } It complains, and here is the error from GCC: t.s: Assembler messages: t.s:31: Error: too many memory references for `mov' 回答1: You get this error because your assembly is malformatted. Register accesses are done like %eax , $ is used for immediate operands. Furthermore, GCC, by default (see DanielKO's comment), uses the AT&T syntax, which has the destination on the

Is there any simple way to improve performance of this spinlock function?

本小妞迷上赌 提交于 2019-12-06 10:13:07
问题 I'm trying to implement a spinlock in my code but the spinlock that I implemented based on Wikipedia results in extremely slow performance. int lockValue = 0; void lock() { __asm__("loop: \n\t" "movl $1, %eax \n\t" "xchg %eax, lockValue \n\t" "test %eax, %eax \n\t" "jnz loop"); } Is there any way of improving this to make it faster? Thanks. 回答1: How about something like this (I understand this is the KeAcquireSpinLock implementation). My at&t assembly is weak unfortunately. spin_lock: rep;

Gcc inline assembly: what's wrong with the dynamic allocated register `r` in input operand?

泪湿孤枕 提交于 2019-12-06 09:19:17
问题 When I test the GCC inline-assembly, I use the test function to display a character on the screen with the BOCHS emulator. This code is running in 32-bit protected mode. The code is as follows: test() { char ch = 'B'; __asm__ ("mov $0x10, %%ax\n\t" "mov %%ax, %%es\n\t" "movl $0xb8000, %%ebx\n\t" "mov $0x04, %%ah\n\t" "mov %0, %%al\n\t" "mov %%ax, %%es: ((80 * 3 + 40) * 2)(%%ebx)\n\t" ::"r"(ch):); } The result I'm getting is: The red character on the screen isn't displaying B correctly.

32bit to 64bit inline assembly porting

允我心安 提交于 2019-12-06 09:12:47
I have a piece of C++ code (compiled with g++ under a GNU/Linux environment) that load a function pointer (how it does that doesn't matter), pushes some arguments onto the stack with some inline assembly and then calls that function, the code is like : unsigned long stack[] = { 1, 23, 33, 43 }; /* save all the registers and the stack pointer */ unsigned long esp; asm __volatile__ ( "pusha" ); asm __volatile__ ( "mov %%esp, %0" :"=m" (esp)); for( i = 0; i < sizeof(stack); i++ ){ unsigned long val = stack[i]; asm __volatile__ ( "push %0" :: "m"(val) ); } unsigned long ret = function_pointer(); /

Help in building an 16 bit os

落爺英雄遲暮 提交于 2019-12-06 06:27:26
问题 I am trying to build an old 16 bit DOS like OS. My example kernel code: asm(".code16\n"); void putchar(char); int main() { putchar('A'); return 0; } void putchar(char val) { asm("movb %0, %%al\n" "movb $0x0E, %%ah\n" "int $0x10\n" : :"r"(val) ) ; } This is how I compile it : nasm -f bin -o ./bin/boot.bin ./source/boot.asm gcc -nostdinc -fno-builtin -I./include -c -o ./bin/kernel.o ./source/kernel.c ld -Ttext=0x9000 -o ./bin/kernel.bin ./bin/kernel.o -e 0x0 dd if=/dev/zero of=./bin/empty.bin

arm Inline assembly in gcc

我是研究僧i 提交于 2019-12-06 06:04:42
问题 I have some trouble with some inline assembly code. I know what should be done but I miss the "how" ! I have this checksum function that is "almost" working : static unsigned long cksum_unroll( unsigned short **w, int *mlen) { int len; unsigned short *w0; unsigned long sum=0; len = *mlen; w0 = *w; while( len >= 8) { asm volatile ( "ldmia %[w0]!, {v1, v2}\n\t" "adds %[sum], %[sum], v1\n\t" "adcs %[sum], %[sum], v2\n\t" "adcs %[sum], %[sum], #0" : [sum] "+r" (sum) : [w0] "r" (w0) ); len -= 8; }

Why is this simple c program with gcc (clang) inline assembly exhibiting undefined behaviour?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 05:47:59
I'm trying to do a very simple thing with gcc assembler extension: load an unsigned int variable into a register add 1 to it output the result While compiling my solution: #include <stdio.h> #define inf_int volatile unsigned long long int main(int argc, char *argv[]){ inf_int zero = 0; inf_int one = 1; inf_int infinity = ~0; printf("value of zero, one, infinity = %llu, %llu, %llu\n", zero, one, infinity); __asm__ volatile ( "addq $1, %0" : "=r" (infinity) ); __asm__ volatile ( "addq $1, %0" : "=r" (zero) ); __asm__ volatile ( "addq $1, %0" : "=r" (one) ); printf("value of zero, one, infinity =

GCC INLINE ASSEMBLY Won't Let Me Overwrite $esp

旧时模样 提交于 2019-12-06 03:19:36
问题 I'm writing code to temporarily use my own stack for experimentation. This worked when I used literal inline assembly. I was hardcoding the variable locations as offsets off of ebp. However, I wanted my code to work without haivng to hard code memory addresses into it, so I've been looking into GCC's EXTENDED INLINE ASSEMBLY. What I have is the following: volatile intptr_t new_stack_ptr = (intptr_t) MY_STACK_POINTER; volatile intptr_t old_stack_ptr = 0; asm __volatile__("movl %%esp, %0\n\t"

ARM inline asm: exit system call with value read from memory

℡╲_俬逩灬. 提交于 2019-12-06 00:42:44
Problem I want to execute the exit system call in ARM using inline assembly on a Linux Android device, and I want the exit value to be read from a location in memory. Example Without giving this extra argument, a macro for the call looks like: #define ASM_EXIT() __asm__("mov %r0, #1\n\t" \ "mov %r7, #1\n\t" \ "swi #0") This works well. To accept an argument, I adjust it to: #define ASM_EXIT(var) __asm__("mov %r0, %0\n\t" \ "mov %r7, #1\n\t" \ "swi #0" \ : \ : "r"(var)) and I call it using: #define GET_STATUS() (*(int*)(some_address)) //gets an integer from an address ASM_EXIT(GET_STATUS());