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.)


100H:
 
/* 8080 MACHINE CODE TO ADD TWO BYTES:
   79       MOV A,C     ; LOAD FIRST ARG INTO ACCUMULATOR
   83       ADD E       ; ADD SECOND ARG TO ACCUMULATOR
   C9       RET         ; RETURN */
 
DECLARE ADD$8080 DATA (79H, 83H, 0C9H);
 
/* THE 8080 PL/M CALLING CONVENTION IS THAT THE
   NEXT-TO-LAST ARG IS PUT IN (B)C, THE LAST ARG IN (D)E.
   (THE REST ARE IN MEMORY BUT WE DO NOT NEED ANY MORE.)
   THE RETURN ARGUMENT SHOULD BE IN THE ACCUMULATOR.
   WE CAN DEFINE A WRAPPER PROCEDURE TO DECLARE THE
   TYPES OF THE ARGUMENTS. */
 
EXEC$ADD: PROCEDURE (A,B) BYTE;
    DECLARE (A,B) BYTE;
    /* WE CAN 'GO TO' CONSTANTS OR VARIABLES, BUT NOT TO
       EXPRESSIONS. SO WE HAVE TO FETCH THE ADDRESS FIRST. */ 
    DECLARE LOC ADDRESS;
    LOC = .ADD$8080;
    GO TO LOC;
END EXEC$ADD;
 
/* IN FACT, PL/M DOES NOT COME WITH ANY STANDARD LIBARIES.
   IT IS FROM BEFORE THE TIME THAT YOU COULD ASSUME THERE
   WOULD EVEN BE AN OPERATING SYSTEM, THOUGH CP/M
   (THE PREDECESSOR TO DOS) WOULD QUICKLY BECOME STANDARD.
 
   WE NEED TO USE THIS EXACT TRICK TO GET CP/M TO PRINT THE
   RESULT TO THE OUTPUT. LUCKILY (AND NOT COINCIDENTALLY), 
   THE CP/M SYSCALL ENTRY POINT IS COMPATIBLE WITH THE
   PL/M CALLING CONVENTION. */
 
BDOS: PROCEDURE (FUNC, ARG);
    DECLARE FUNC BYTE;
    DECLARE ARG ADDRESS;
    /* 5 IS THE CP/M BDOS ENTRY POINT */
    GO TO 5; 
END BDOS;
 
/* WE ALSO NEED OUR OWN NUMBER OUTPUT ROUTINE. WE CAN WRITE
   IT IN PL/M, THEN USE THE ABOVE ROUTINE TO TELL CP/M
   TO PRINT THE RESULT. */
 
PRINT$NUMBER: PROCEDURE(N);
    DECLARE S (4) BYTE INITIAL ('...$');
    DECLARE P ADDRESS;
    DECLARE (N, C BASED P) BYTE;
 
    /* EXTRACT EACH DIGIT AND WRITE THEM BACKWARDS TO A STRING */
    P = .S(3);
DIGIT:
    P = P-1;
    C = (N MOD 10) + '0';
    N = N/10;
    IF N > 0 THEN GO TO DIGIT;
 
    /* TELL CP/M TO PRINT THE RESULTING STRING */
    CALL BDOS(9, P);
END PRINT$NUMBER;
 
/* USING OUR OWN MACHINE CODE WORKS IN THE SAME WAY */
CALL PRINT$NUMBER( EXEC$ADD( 7, 12) ); /* THIS PRINTS 19 */
 
CALL BDOS(0,0); /* EXIT */
EOF