Anonymous user
Machine code: Difference between revisions
m
Updated whitespace.
Thundergnat (talk | contribs) m (→{{header|Raku}}: enable syntax highlighting) |
m (Updated whitespace.) |
||
Line 68:
#include <sys/mman.h>
#include <string.h>
int test (int a, int b) {▼
char code[] = {0x8B, 0x44, 0x24, 0x4, 0x3, 0x44, 0x24, 0x8, 0xC3};▼
▲int test (int a, int b)
void * buf;▼
int c;▼
buf = mmap (0, sizeof(code), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0);▼
memcpy (buf, code, sizeof(code));▼
c = ((int (*) (int, int)) buf) (a, b);▼
munmap (buf, sizeof(code));▼
return c;▼
▲ char code[] = {0x8B, 0x44, 0x24, 0x4, 0x3, 0x44, 0x24, 0x8, 0xC3};
▲ void *buf;
▲ int c;
▲ buf = mmap (0,sizeof(code),PROT_READ|PROT_WRITE|PROT_EXEC,
▲ memcpy (buf, code, sizeof(code));
▲ c = ((int (*) (int, int))buf)(a, b);
▲ munmap (buf, sizeof(code));
▲ return c;
}
int main () {▼
printf("%d\n", test(7,12));▼
▲int main ()
return 0;▼
▲ printf("%d\n", test(7,12));
▲ return 0;
}</lang>
|