user
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.)
assistant
I don't know how to translate this C line

```
c = ((int (*) (int, int))buf)(a, b);

```

so cannot solve the task with an idiomatic solution. I have also tried with Go's approach by adding a helper program but it also doesn't work out. Nonetheless I just present the attempt here so perhaps someone can fix that in 10 seconds.

```
use NativeCall;

constant PROT_READ   = 0x1;   #
constant PROT_WRITE  = 0x2;   #
constant PROT_EXEC   = 0x4;   # from local /usr/include/bits/mman.h
constant MAP_PRIVATE = 0x02;  #
constant MAP_ANON    = 0x20;  #

sub mmap(Pointer $addr, size_t $length, int32 $prot, int32 $flags,
   int32 $fd, size_t $offset --> Pointer) is native { * };
sub memcpy(Pointer $dest, Pointer $src, size_t $size --> Pointer) is native {*}
sub munmap(Pointer $addr, size_t $length) is native { * };

sub test (uint8 $a, uint8 $b) {
   my $code = CArray[uint8].new(
      0x90, 0x90, 0x6A, 0xC, 0xB8, 0x7, 0x0, 0x0, 0x0, 0x48, 0xC1, 0xE0, 0x20,
      0x50, 0x8B, 0x44, 0x24, 0x4, 0x3, 0x44, 0x24, 0x8, 0x4C, 0x89, 0xE3, 0x89,
      0xC3, 0x48, 0xC1, 0xE3, 0x4, 0x80, 0xCB, 0x2, 0x48, 0x83, 0xC4, 0x10, 0xC3   
   );
   my $buf =
      mmap(Pointer, nativesizeof($code), PROT_READ +| PROT_WRITE +| PROT_EXEC,
      MAP_PRIVATE +| MAP_ANON, -1, 0);

   memcpy($buf, nativecast(Pointer,$code), nativesizeof($code));

   my $c; # = ((int (*) (int, int))buf)(a, b);

   munmap($buf, nativesizeof($code));

   return $c = "Incomplete Attempt";
}

say test 7, 12;

```

In the mean time, here is a less desirable approach by writing a wrapper for the C entry, with the 64 bit instructions from PicoLisp ..

**Translation of**: C

test.c

```
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>

int test (int a, int b)
{
  char code[] = {
     0x90, 0x90, 0x6A, 0xC, 0xB8, 0x7, 0x0, 0x0, 0x0, 0x48, 0xC1, 0xE0, 0x20,
     0x50, 0x8B, 0x44, 0x24, 0x4, 0x3, 0x44, 0x24, 0x8, 0x4C, 0x89, 0xE3, 0x89,
     0xC3, 0x48, 0xC1, 0xE3, 0x4, 0x80, 0xCB, 0x2, 0x48, 0x83, 0xC4, 0x10, 0xC3
  };

  void *buf;
  int c;
  /* copy code to executable buffer */
  buf = mmap (0,sizeof(code),PROT_READ|PROT_WRITE|PROT_EXEC,
             MAP_PRIVATE|MAP_ANON,-1,0);
  memcpy (buf, code, sizeof(code));
  /* run code */
  c = ((int (*) (int, int))buf)(a, b);
  /* free buffer */
  munmap (buf, sizeof(code));
  return c;
}

```

mcode.raku

```
#!/usr/bin/env raku

# 20200501 Raku programming solution

use NativeCall;

constant LIBTEST = '/home/user/LibTest.so';

sub test(uint8 $a, uint8 $b) returns uint8 is native(LIBTEST) { * };

say test 7, 12;

```

Output:

```
gcc -Wall -fPIC -shared -o LibTest.so test.c

file LibTest.so
LibTest.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=90d2695df9a56b88a57144147fb9288ac07b172f, not stripped
./mcode.raku

19
```