eleboog-astro/src/content/posts/2025-03-11-jwl-03.mdx

47 lines
No EOL
2 KiB
Text

---
date: 2025-03-11 13:00:00
title: A Quick Dive into Intel x86-64 Bytecode (JWL 02)
summary: Intel x86-64 assembly can be daunting just by itself... but what if you had to write *in* bytecode? Here's a brief tutorial to get you started. The second entry into my JustWriteLol series.
---
If you've ever delved into writing assembly before, you may know that it takes a *vastly* different approach than writing in most other languages,
even languages fairly close to assembly like C. Registers, memory addresses, opcodes, all of it can be pretty daunting just on their own...
But what if you had to do more than just write the instructions in text form? What if... you had to write. *every. single. byte.* of **every. single.
line. of assembly.**
You might think that this is the craziest thing you have ever heard. Who in their right mind would do such a thing? And who on the face of Planet
Earth would actually find this... fun????
Me. I'm the problem. It's me.
I've always found assembly a fun change of pace from most other programming since I took a class all about it required for my major. I already
knew about how assembly can be represented in bytecode, but I never really had to directly write it myself... until this course I'm taking now,
where the professor decided to run x86-64 assembly code in C++ like this:
```cpp
char *prog;
int value;
int p_offset = 0;
prog = (char*) mmap(0, 50000, PROT_EXEC | PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
prog[p_offset++] = 0xb8;
prog[p_offset++] = 0x2a;
prog[p_offset++] = 0x00;
prog[p_offset++] = 0x00;
prog[p_offset++] = 0x00;
prog[p_offset++] = 0xc3;
value = (int(*)(void) prog)();
cout << value << endl;
```
```bash
$ ./a.out
42
```
<MDXImage src="https://media1.tenor.com/m/giGudNYLk_sAAAAd/benny-fallout.gif" alt='Benny from Fallout: New Vegas turning around to face you and exclaiming, in shock at your not-dead-ed-ness: "What in the goddamn...?"'/>
Yes, that's really how he wrote it. Yes, *it actually works*. Maybe Intel engineers discovered the meaning to life after all.