I tried to find anything that will show code but I wasn't able to find anything expect for an answer on stackoverflow. I would find a lot of theory but no practical code that works. What I want to do is allocate memory (with execution mapping), add the machine instructions and then allocate another memory block for the data and finally, execute the block of memory that contains the code. So something like what the OS loader does when reading an executable. I have come with the following code:

Code:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "sys/mman.h"

#define null (void*)0

int main() {
  char* data = mmap(null, 15, PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  memset(data, 0x0, 15); // Default value

  *data = 'H';
  data[1] = 'e';
  data[2] = 'l';
  data[3] = 'l';
  data[4] = 'o';
  data[5] = ' ';

  data[6] = 'w';
  data[7] = 'o';
  data[8] = 'r';
  data[9] = 'l';
  data[10] = 'd';
  data[11] = '!';

  void* code = mmap(null, 500, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0);
  char* code_byte = (char*)code;
  long* code_long = (long*)code;
  memset(code, 0xc3, 500); // Default value

  /* Call the "write" and "exit" system calls*/
  // mov rax, 0x04
  *code_byte = 0x48;
  code_byte[1] = 0xC7;
  code_byte[2] = 0xC0;
  code_byte[3] = 0x04;
  code_byte[4] = 0x00;
  code_byte[5] = 0x00;
  code_byte[6] = 0x00;

  // mov rbx, 0x01
  code_byte[7]  = 0x48;
  code_byte[8]  = 0xC7;
  code_byte[9]  = 0xC3;
  code_byte[10] = 0x01;
  code_byte[11] = 0x00;
  code_byte[12] = 0x00;
  code_byte[13] = 0x00;

  // mov rdx, 12 (hardcoded)
  code_byte[14] = 0x48;
  code_byte[15] = 0xC7;
  code_byte[16] = 0xC2;
  code_byte[17] = 12;
  code_byte[18] = 0x00;
  code_byte[19] = 0x00;
  code_byte[20] = 0x00;

  // mov rdx, 
  code_byte[21] = 0x48;
  code_byte[22] = 0xC7;
  code_byte[23] = 0xC1;
  code_long[24] = (long)data;
  code_byte[32] = 0x00;

  // int 0x80 (which calls the "write" system call on linux)
  code_byte[33] = 0xcd;
  code_byte[34] = 0x80;

  /* Execute the code */
  ((void(*)(void))code)();
}
When I compile and execute the program, nothing happens! I'm 100% sure that the instructions work as I have tested them with another example that creates an ELF executable file and it was able to execute correctly. So unless I copy-pasted them wrong, the instructions are not the problem. The only thing that may be wrong is when I'm getting the location of the "data" "segment". In my eyes, this uses 8 bytes for the memory address (I'm in a 64bit machine) and it takes the memory address the "data" variable holds so I would expect it to work....

Any ideas?