Generating machine code from C

0

Sorry if these are naive questions - I have very little understanding of how C really works at the low level.

So I'm generating machine code to write to some mmap'd memory for execution. I'm confused about the use of hexadecimal literals for generating machine code.

Consider the assembly instruction (AT&T syntax): cmove %edx, %ecx. This has the machine code representation 0x0F44CA.

So, would doing something like:

char opcode[3] { 0x0F, 0x44, 0xCA };

represent the correct binary string under when 'under the hood'? I suspect it might not, since apparently hexadecimal literals in C are stored as integers. My concern is that, since integers are 32-bit, the actual values getting stored are

0x0000000F 0x00000044 0x000000CA

Which is something completely different from what I need.

Another concern I have is, does the type I give to the array affect the value actually being stored? So would

uint8_t opcode[3] { 0x0F, 0x44, 0xCA };

or

int opcode[3] { 0x0F, 0x44, 0xCA };

be any different from

char opcode[3] { 0x0F, 0x44, 0xCA };

under the hood?

c
compiler-construction
hex
jit
machine-code
asked on Stack Overflow Aug 20, 2013 by AlexJ136

2 Answers

1

I did not get your actual problem but I think these two points may help you for better understanding of machine code.

  1. Use objdump and you will get machine code and assembly code together to understand what is happening.

    objdump -d prog.o
    
  2. Read this article http://csapp.cs.cmu.edu/public/ch3-preview.pdf

I hope this will help you somewhat.

answered on Stack Overflow Aug 20, 2013 by someone
1

uint8_t opcode[3] = { 0x0F, 0x44, 0xCA };

will store your values as 8-bit values 'bytes' in the order you gave them.

It is the same as

unsigned char opcode[3] = { 0x0F, 0x44, 0xCA };

But using an 'int' type is as you said 0000000F00000044000000CA or 0F00000044000000CA000000 depending on the endianess of your system.

answered on Stack Overflow Aug 20, 2013 by rockdaboot

User contributions licensed under CC BY-SA 3.0