Skip to main content
22 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,837
19 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
14 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,837
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,558
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
10 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
  • 24.5k
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.9k
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
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

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

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

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

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.7k
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.5k
4 votes

Branch differently in x86 / x86-64 using only printable visible ASCII characters in the machine code

7 bytes Not relying on 66 prefix. $$@$Au! 32-bit: ...
peter ferrie's user avatar
  • 1,135
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

Tips for golfing in x86/x64 machine code

Entry point doesn't necessarily have to be first byte of submission I came across this answer, and didn't understand it at first until I realized that the intention is: ...
640KB's user avatar
  • 11.9k
4 votes

Tips for golfing in x86/x64 machine code

Use a good assembler There are dozens of x86 assemblers out there, and they are not created equal. Not only can a bad assembler be painful to use, but they might not always output the most optimal ...
EasyasPi's user avatar
  • 4,880
3 votes

Tips for golfing in x86/x64 machine code

Try XLAT for byte memory access XLAT is a one byte instruction that is equivalent to ...
640KB's user avatar
  • 11.9k
3 votes

Tips for golfing in x86/x64 machine code

Take advantage of the x86_64 code model Linux's default code model will put all of your code and globals in the low 31 bits of memory, so 32-bit pointer arithmetic here is perfectly safe. The stack, ...
EasyasPi's user avatar
  • 4,880
3 votes

Tips for golfing in x86/x64 machine code

To add or subtract 1, use the one byte inc or dec instructions which are smaller than the multibyte add and sub instructions.
user230118's user avatar
3 votes

Tips for golfing in x86/x64 machine code

Use interrupts and syscalls wisely In general, unlike the C calling conventions, most syscalls and interrupts will preserve your registers and flags unless noted otherwise, except for a return value ...
EasyasPi's user avatar
  • 4,880
3 votes

Tips for golfing in Z80 or SM83 machine code

Shadow registers When you're short on registers, you can use the shadow registers - af', bc', ...
Kamila Szewczyk's user avatar
3 votes

Tips for Golfing in ARM

Use s instructions to your advantage. ARM Unlike x86, ARM lets you choose whether to set the condition codes on its instructions, by suffixing the instructions with ...
EasyasPi's user avatar
  • 4,880
2 votes

Tips for golfing in x86/x64 machine code

Use whatever calling conventions are convenient System V x86 uses the stack and System V x86-64 uses rdi, rsi, ...
qwr's user avatar
  • 12.1k
2 votes

Tips for golfing in x86/x64 machine code

Use conditional moves CMOVcc and sets SETcc This is more a reminder to myself, but conditional set instructions exist and ...
qwr's user avatar
  • 12.1k
2 votes

Tips for golfing in x86/x64 machine code

Save on jmp bytes by arranging into if/then rather than if/then/else This is certainly very basic, just thought I would post this as something to think about when ...
Daniel Schepler's user avatar

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