Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c library x86/x64 assembler

Is there a C library for assembling a x86/x64 assembly string to opcodes?

Example code:

/* size_t assemble(char *string, int asm_flavor, char *out, size_t max_size); */

unsigned char bytes[32];
size_t size = assemble("xor eax, eax\n"
                       "inc eax\n"
                       "ret",
                       asm_x64, &bytes, 32);

for(int i = 0; i < size; i++) {
    printf("%02x ", bytes[i]);
}

/* Output: 31 C0 40 C3 */

I have looked at asmpure, however it needs modifications to run on non-windows machines.

I actually both need an assembler and a disassembler, is there a library which provides both?

like image 799
Tyilo Avatar asked Jun 08 '13 15:06

Tyilo


People also ask

What is x86 assembly used for?

x86 assembly language is the name for the family of assembly languages which provide some level of backward compatibility with CPUs back to the Intel 8008 microprocessor, which was launched in April 1972. It is used to produce object code for the x86 class of processors.

What is x64 assembly?

x64 is a generic name for the 64-bit extensions to Intel‟s and AMD‟s 32-bit x86 instruction set architecture (ISA). AMD introduced the first version of x64, initially called x86-64 and later renamed AMD64. Intel named their implementation IA-32e and then EMT64.

What are the two main parts of an x86 instruction called?

Arithmetic and Logic Instructions. The add instruction adds together its two operands, storing the result in its first operand. Note, whereas both operands may be registers, at most one operand may be a memory location.

What is echo in assembly language?

echo Assembly Code The assembly code for echo iterates over the argument pointers on the stack, printing the string corresponding to each argument. The iteration starts at the second element on the stack (past the first element, argument count), and stops when reaching a zero.


2 Answers

There is a library that is seemingly a ghost; its existance is widely unknown:

XED (X86 Encoder Decoder)

Intel wrote it: https://software.intel.com/sites/landingpage/pintool/docs/71313/Xed/html/

It can be downloaded with Pin: https://software.intel.com/en-us/articles/pintool-downloads

like image 153
user4872223 Avatar answered Sep 19 '22 23:09

user4872223


Sure - you can use llvm. Strictly speaking, it's C++, but there are C interfaces. It will handle both the assembling and disassembling you're trying to do, too.

like image 40
Carl Norum Avatar answered Sep 19 '22 23:09

Carl Norum