21 votes

Tips for golfing in x86/x64 machine code

Choose your calling convention to put args where you want them. The language of your answer is asm (actually machine code), so treat it as part of a program written in asm, not C-compiled-for-x86. ...
Peter Cordes's user avatar
  • 4,817
18 votes

Tips for golfing in x86/x64 machine code

mov-immediate is expensive for constants This might be obvious, but I'll still put it here. In general it pays off to think about the bit-level representation of a ...
ბიმო's user avatar
  • 16.8k
17 votes

8bit virtual machine

C (gcc), 1381 1338 1255 1073 bytes Huge improvement thanks to ceilingcat and Rogem. ...
Max Yekhlakov's user avatar
13 votes

Tips for golfing in x86/x64 machine code

Use special-case short-form encodings for AL/AX/EAX, and other short forms and single-byte instructions Examples assume 32 / 64-bit mode, where the default operand size is 32 bits. An operand-size ...
Peter Cordes's user avatar
  • 4,817
13 votes

Bootloader golf: Brainf***

171 bytes1 Wooohoooo! It took half the day, but it was fun... So, here it is. I think it conforms to the specs (wraparound of cell pointer, echo of characters on input, reading char by char, echo of ...
dim's user avatar
  • 8,548
13 votes

Tips for golfing in x86/x64 machine code

In a lot of cases, accumulator-based instructions (i.e. those that take (R|E)AX as the destination operand) are 1 byte shorter than general-case instructions; see ...
Govind Parmar's user avatar
12 votes
Accepted

Smallest possible runnable Mach-O executable

Smallest runnable Mach-O has to be at least 0x1000 bytes. Because of XNU limitation, file has to be at least of PAGE_SIZE. See <...
stek29's user avatar
  • 236
10 votes

Tips for golfing in x86/x64 machine code

Create 3 zeroes with mul (then inc/dec to get +1 / -1 as well as zero) You can zero eax and ...
peter ferrie's user avatar
  • 1,135
9 votes

Box-256 Assembly - BIG SQUARE II

8 instructions My smallest solution to the Big Square II challenge is 8 lines of code. There's a simple loop which cycles through a table of 8 pixel locations. ...
impomatic's user avatar
  • 351
9 votes

Tips for golfing in x86/x64 machine code

Subtract -128 instead of add 128 0100 81C38000 ADD BX,0080 0104 83EB80 SUB BX,-80 Samely, add -128 instead of subtract 128
l4m2's user avatar
  • 23.9k
9 votes

8bit virtual machine

APL (Dyalog Classic), 397 332 330 bytes thanks @Adám for -8 bytes ...
ngn's user avatar
  • 15.3k
8 votes

Branchless (MIPS) assembly code for median of 3

7 instructions slt $t3, $t0, $t1 slt $t4, $t2, $t1 slt $t5, $t2, $t0 Compare all three pairs of values. ...
m90's user avatar
  • 10.3k
7 votes

Tips for golfing in x86/x64 machine code

CPU registers and flags are in known startup states For a full/standalone program, we can assume that the CPU is in a known and documented default state based on platform and OS. For example: DOS http:...
640KB's user avatar
  • 11.8k
7 votes

Tips for golfing in x86/x64 machine code

Skipping instructions Skipping instructions are opcode fragments that combine with one or more subsequent opcodes. The subsequent opcodes can be used with a different entrypoint than the prepended ...
ecm's user avatar
  • 171
7 votes

8bit virtual machine

JavaScript (ES6), 361 bytes Takes input as (memory)(program_counter), where memory is an ...
Arnauld's user avatar
  • 192k
6 votes

Assembly Language Quine

GAS x86-64 for Linux (verified on GCC 10.1): 13 12 5 instructions 148 136 129 0 byte data section 298 274 260 51 49 byte source code Self-imposed rules/assumptions: No standard library (including <...
General Grievance's user avatar
6 votes

Smallest possible runnable Mach-O executable

28 Bytes, Pre-compiled. Below is a formated hex dump of the Mach-O binary. ...
ATaco's user avatar
  • 10.8k
6 votes

Tips for golfing in x86/x64 machine code

Use do-while loops instead of while loops This is not x86 specific but is a widely applicable beginner assembly tip. If you know a while loop will run at least once, rewriting the loop as a do-while ...
qwr's user avatar
  • 12.1k
6 votes

Tips for golfing in x86/x64 machine code

mov small immediates into lower registers when applicable If you already know the upper bits of a register are 0, you can use a shorter instruction to move an ...
qwr's user avatar
  • 12.1k
6 votes

What is the smallest possible binary output you can generate

x86 assembly DOS .COM file 1 byte ret # assembles to 0xC3 COM files are executable and have no header or other mandatory content. An empty COM file produces an ...
Nick Kennedy's user avatar
  • 21.1k
5 votes

Tips for golfing in x86/x64 machine code

The FLAGS are set after many instructions After many arithmetic instructions, the Carry Flag (unsigned) and Overflow Flag (signed) are set automatically (more info). The Sign Flag and Zero Flag are ...
qwr's user avatar
  • 12.1k
5 votes

Tips for golfing in x86/x64 machine code

lea for math This is probably one of the first things one learns about x86, but I leave it here as a reminder. lea can be used ...
qwr's user avatar
  • 12.1k
5 votes

Tips for golfing in x86/x64 machine code

Use fastcall conventions x86 platform has many calling conventions. You should use those that pass parameters in registers. On x86_64, the first few parameters are ...
anatolyg's user avatar
  • 13.6k
5 votes

Tips for golfing in x86/x64 machine code

Combinations with CDQ for certain piecewise-linear functions CDQ sign-extends EAX into EDX, making EDX 0 if EAX is nonnegative and -1 (all 1s) if EAX is negative. ...
m90's user avatar
  • 10.3k
5 votes

Branchless (MIPS) assembly code for median of 3

8 instructions slt $t4, $t2, $t1 movn $t3, $t1, $t4 movn $t1, $t2, $t4 movn $t2, $t3, $t4 Rearrange $t1 and ...
m90's user avatar
  • 10.3k
5 votes

Write the most optimized assembly program to detect a prime number (from a bigger range!)

Stuff at the bottom is for the earlier version of the question. Here's a simple technique to optimize the basic trial division from the example code, at 2736719982 cycles. It hopefully won't be the ...
harold's user avatar
  • 1,419
4 votes
Accepted

Box-256 Assembly - BIG SQUARE II

1 instruction This solution is to illustrate that counting instructions is not a proper way of scoring for BOX-256. ...
orlp's user avatar
  • 39.1k
4 votes

Tips for golfing in x86/x64 machine code

The loop and string instructions are smaller than alternative instruction sequences. Most useful is loop <label> which is smaller than the two instruction ...
user230118's user avatar
4 votes

Assembly Language Quine

NASM, 223 bytes ...
MD XF's user avatar
  • 13.9k

Only top scored, non community-wiki answers of a minimum length are eligible