inline-assembly

Writing MIPS machine instructions and executing them from C

空扰寡人 提交于 2019-12-24 04:40:35
问题 I'm trying to write some self modifying code in C and MIPS. Since I want to modify the code later on, I'm trying to write actual machine instructions (as opposed to inline assembly) and am trying to execute those instructions. Someone told me that it would be possible to just malloc some memory, write the instructions there, point a C function pointer to it and then jump to it. (I include the example below) I've tried this with my cross compiler (sourcery codebench toolchain) and it doesn't

Executing generated assembler inline

江枫思渺然 提交于 2019-12-24 01:23:52
问题 I was reading the following presentation: http://wingolog.org/pub/qc-2012-js-slides.pdf which talks about (4,10,19) inline ASM generation as a technique used in Javascript optimisation. In the following paper: https://sites.google.com/site/juliangamble/Home/Compilers%20Tutorial%202006-09-16.pdf?attredirects=0&d=1 at page 30 and 31 they talk about using scheme to generate ASM that is subsequently linked and executed in a subsequent OS process. What about the scenario where you want to generate

Convert AT&T syntax to Intel Syntax (ASM)

你。 提交于 2019-12-24 00:57:07
问题 I've been trying to access the peb information of an executable as seen here: Access x64 TEB C++ & Assembly The code works only in AT&T syntax for some odd reason but when I try to use Intel syntax, it fails to give the same value. There's of course an error on my part. So I'm asking.. How can I convert: int main() { void* ptr = 0; //0x7fff5c4ff3c0 asm volatile ( "movq %%gs:0x30, %%rax\n\t" "movq 0x60(%%rax), %%rax\n\t" "movq 0x18(%%rax), %%rax\n\t" "movq %%rax, %0\n" : "=r" (ptr) :: ); } to

How do you call Win32 API functions from inline assembler?

我只是一个虾纸丫 提交于 2019-12-24 00:45:00
问题 Would somebody please tell me whats wrong with this code I am just calling a Sleep function from the kernel32.dll What's wrong? I am using Visual Studio 2008. Any help would be grateful. Thank you very much. __asm { mov eax, 77e2ef66h push 9999 call eax } 回答1: Where did you get that magic number, 77e2ef66h ? Usually if you're calling Win32 API functions from inline assembler, you'd do something like: __asm { push 9999 call Sleep } Functions in Win32 do not have a fixed address (regardless of

ARMv8 floating point output inline assembly

烂漫一生 提交于 2019-12-24 00:25:58
问题 For adding two integers, I write: int sum; asm volatile("add %0, x3, x4" : "=r"(sum) : :); How can I do this with two floats? I tried: float sum; asm volatile("fadd %0, s3, s4" : "=r"(sum) : :); But it gives me an error: Error: operand 1 should be a SIMD vector register -- `fadd x0,s3,s4' Any ideas? 回答1: ARMv7 double: %P modifier GCC devs informed me the correct undocumented modifier for ARMv7 doubles at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89482#c4 Maybe I should stop being lazy and

How to find the return address of a function in C?

▼魔方 西西 提交于 2019-12-23 22:49:23
问题 I'm trying to use a small amount of AT&T style inline assembly in C and GCC by reading an article on CodeProject here. The main reason I wish to do this is to find the old value of the EIP register to be able to have a reliable address of instructions in my code. I have written a simple example program to demonstrate my understanding of this concept thus far : #include <stdio.h> #include <stdlib.h> int mainReturnAddress = 0; int main() { asm volatile ( "popl %%eax;" "pushl %%eax;" "movl %%eax

GCC error: Cannot apply offsetof to member function MyClass::MyFunction

人走茶凉 提交于 2019-12-23 22:25:29
问题 After trying to replace the offset keyword with __offsetof while trying to compile with Apple GCC 4.2.1 using the -fasm-blocks argument (which enables Intel style assembly syntax) inline assembly code which worked in MSVC, I get an error: Cannot apply offsetof to member function MyClass::MyFunction class MyClass { void MyFunction(void* pData) { } }; void test() { _asm { //mov eax, offset MyClass::MyFunction - this works in MSVC mov eax, offsetof(class MyClass, MyFunction) //error: Cannot

GCC Inline Assembly 'Nd' constraint

风格不统一 提交于 2019-12-23 12:59:47
问题 I'm developing a small toy kernel in C. I'm at the point where I need to get user input from the keyboard. So far, I have implemented inb using the following code: static inline uint8_t inb(uint16_t port) { uint8_t ret; asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port)); return ret; } I know that the "=a" constraint means that al/ax/eax will be copied to ret as output, but I'm still confused about the "Nd" constraint. Can anyone provide some insight on why this constraint is necessary? Or

Work around lack of Yz machine constraint under Clang?

旧巷老猫 提交于 2019-12-23 10:10:52
问题 We use inline assembly to make SHA instructions available if __SHA__ is not defined. Under GCC we use: GCC_INLINE __m128i GCC_INLINE_ATTRIB MM_SHA256RNDS2_EPU32(__m128i a, const __m128i b, const __m128i c) { asm ("sha256rnds2 %2, %1, %0" : "+x"(a) : "xm"(b), "Yz" (c)); return a; } Clang does not consume GCC's Yz constraint (see Clang 3.2 Issue 13199 and Clang 3.9 Issue 32727), which is required by the sha256rnds2 instruction: Yz First SSE register (%xmm0). We added a mov for Clang: asm ("mov

Why is the value of EDX overwritten when making call to printf?

早过忘川 提交于 2019-12-22 05:26:23
问题 I've written a simple assembly program: section .data str_out db "%d ",10,0 section .text extern printf extern exit global main main: MOV EDX, ESP MOV EAX, EDX PUSH EAX PUSH str_out CALL printf SUB ESP, 8 ; cleanup stack MOV EAX, EDX PUSH EAX PUSH str_out CALL printf SUB ESP, 8 ; cleanup stack CALL exit I am the NASM assembler and the GCC to link the object file to an executable on linux. Essentially, this program is first putting the value of the stack pointer into register EDX, it is then