The task requires poking machine code directly into memory and executing it. The machine code is the architecture-specific opcodes which have the simple task of adding two unsigned bytes together and making the result available to the high-level language.

For example, the following assembly language program is given for x86 (32 bit) architectures:

mov EAX, [ESP+4]
add EAX, [ESP+8]
ret
This would translate into the following opcode bytes:

139 68 36 4 3 68 36 8 195
Or in hexadecimal:

8B 44 24 04 03 44 24 08 C3
Task

If different than 32-bit x86, specify the target architecture of the machine code for your example. It may be helpful to also include an assembly version of the machine code for others to reference and understand what is being executed. Then, implement the following in your favorite programming language:

 Poke the necessary opcodes into a memory location.
 Provide a means to pass two values to the machine code.
 Execute the machine code with the following arguments: unsigned-byte argument of value 7; unsigned-byte argument of value 12; The result would be 19.
 Perform any clean up actions that are appropriate for your chosen language (free the pointer or memory allocations, etc.)


 
Module Checkit {
      Buffer DataMem as Long*10
      Return DataMem, 1:=500    ' second Long
      Print Eval(DataMem, 1)+5100+5=5605
      \\ Now we do math executing machine code
      Buffer Code ExecMem as byte*1024
      Address=0
      EmbLong(0xb8, 5100) ' mov eax,5100
      EmbByteByte(0x83, 0xC0, 5) ' add  eax,0x5
      EmbByteLong(0x3,0x5, DataMem(1)) ' add eax, [DataMem(1)] 
      EmbLong(0xa3, DataMem(0)) ' mov [DataMem(0)], eax
      \\ split rem to execute xor eax eax (eax=0)
      Rem : EmbByte(0x31, 0xC0) ' xor eax, eax 
      Ret() ' Return
      \\  
      Try ok {
            Execute Code ExecMem, 0
      }
      \\If  Eax <>0 then we get error, so we read error as Uint()
      \\ Error read once then change to zero
      m=Uint(Error)
      \\ Hex is Print Hexadecimal for unsigned numbers
      Hex m
      Print m=5605
      Print Error=0, ok=False
 
      Print Eval(DataMem, 0)=5605,  Eval(DataMem, 0)
      \\ sub used as Exit here
      Sub Ret()
            Return ExecMem, Address:=0xC3
            Address++
      End Sub
      Sub EmbByteByte()
            Return ExecMem, Address:=Number, Address+1:=Number, Address+2:=Number
            Address+=3
      End Sub
      Sub EmbByte()
            Return ExecMem, Address:=Number, Address+1:=Number
            Address+=2
      End Sub
      Sub EmbLong()
            Return ExecMem, Address:=Number, Address+1:=Number as Long
            Address+=5
      End Sub
      Sub EmbByteLong()
            Return ExecMem, Address:=Number, Address+1:=Number, Address+2:=Number as Long
            Address+=6
      End Sub
 
}
CheckIt
 