Machine code: Difference between revisions

→‎{{header|Wren}}: Tidied and fixed a potential seg fault in C code.
No edit summary
(→‎{{header|Wren}}: Tidied and fixed a potential seg fault in C code.)
Line 1,631:
 
However, it is designed for embedding and we can therefore ask the host to do this for us. Here, we use a host program written in C, the language which Wren itself is written in.
<syntaxhighlight lang="ecmascriptwren">/* machine_codeMachine_code.wren */
class C {
Line 1,653:
<br>
We now embed this Wren script in the following C program, compile and run it.
<syntaxhighlight lang="c">#include/* <stdlibgcc Machine_code.h>c -o Machine_code -lwren -lm */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 1,679 ⟶ 1,681:
void C_runMachineCode(WrenVM* vm) {
/* unpack arguments passed from Wren */
int *len;
const char *code = wrenGetSlotBytes(vm, 1, &len);
unsigned char a = (unsigned char)wrenGetSlotDouble(vm, 2);
unsigned char b = (unsigned char)wrenGetSlotDouble(vm, 3);
 
/* obtain result */
unsigned char c = rmc_helper(code, a, b, *len);
/* return result to Wren */
Line 1,745 ⟶ 1,747:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "machine_codeMachine_code.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
9,488

edits