Q:

Inline ASM jmp to new memory

So I would like to load a set of assembly instructions from a file, and execute those instructions. 
OR
I would like to load already compiled machine code again from a file (non-exe) 
My Thoughts:
So far I've learned enough inline asm to work with c/c++ variables easily. 
  asm volatile (
    "mov     %1, %%ecx;" // yes unnecessary, I know
    "add     %2, %%ecx;" // I know they're already loaded in a register
    "mov     %%ecx ,%0 ;"// just demonstrating what I've learned
    :"=r"(address)
    :"r"(address),"r"(offset)
    :"%ecx"
  );

I've begun to learn about op-codes, and I've gotten some x86 manuals. I understand (somewhat) how the hardware works at a basic level.
I know that I can load the file into memory with c++ with fstream, and I'm wondering if there's a way to execute memory from that space, or if it's in a non-executable portion of memory or something..
Reason:
Currently there are several reasons I'd like to do this. I have a desire to create a basic encyption for my program, for a simple key to run the program. And while I can easily encrypt and decrypt the actual code, I want the program to be unencrypted every run and never stored on the harddrive. I am aware of several problems, however I'm very interested in doing so. 
Final Questions:
Can I execute machine code from memory space in a program from c++ in asm?
Is asm necessary?
Are there other ways to accomplish this task more efficiently, if so, what are those methods?
I'm also reading up on some DLL injection for a mouse sharing program that I'm making (two computers next to each other, both have monitors, however you only have one mouse/keyboard. And I'm wondering where I can find some good resources on the topic? Google has been helpful, however I'm interested in perhaps some IRC channels or something similar. Anyways, thanks for reading!

A:

This sounds like you kind of want some JIT compiling of some x86 assembly in a separate file?
I might have misunderstood, when you say 'memory space', your encapsulating a very broad term.  I assume you don't mean that you want to compile it with a specific set of assembly but be able to interchange the assembly instructions at runtime?
Have a read over the following Dynamically generating and executing x86 code
You'll see 
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

 int main(int argc, char *argv[]) {
     uint8_t *buf = mmap(NULL, 1000, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

     buf[0] = 0xb8;
     uint32_t u32 = 42;
     memcpy(buf + 1, &u32, 4);
     buf[5] = 0xc3;

     if (mprotect(buf, 1000, PROT_EXEC | PROT_READ) < 0) {
         fprintf(stderr, "mprotect failed: %s\n", strerror(errno));
         return 1;
     }
     int (*ptr)(void) = (int (*)(void)) buf;

     printf("return is %d\n", ptr());

     return 0; }

Here you are allocating memory for a buffer.  Putting the instructions into the buffer, then creating a function pointer from the allocated memory.  Notice the memory is being protected also.
Read over the article for a better understanding, it also gives you some Windows equivalents.

