Timeline for Tips for golfing in x86/x64 machine code
Current License: CC BY-SA 3.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 3, 2023 at 14:32 | comment | added | Peter Cordes |
For the record, push imm was new in 186. pushbx.org/ecm/doc/insref.htm#insPUSH
|
|
Feb 18, 2019 at 19:16 | comment | added | 640KB |
Caveat is that PUSH immediate is not supported on the 8086/8088.
|
|
Mar 29, 2018 at 18:52 | comment | added | Peter Cordes |
If you can solve the problem with uint8_t / int8_t , then you can do everything with 8-bit registers. e.g. if ecx is busy for something else so you can't use loop , and your trip-count is less than 255, then you can mov bl, 123 / dec bl / jnz . Oh, but note that dec bl is 2 bytes while dec ebx is 1 byte in 32-bit mode. dec ebx is 2 bytes in x86-64, so you aren't missing out on inc/dec by using 8-bit operand size.
|
|
Mar 29, 2018 at 18:41 | comment | added | qwr | @PeterCordes hmm. I'm still not sure about when to use 8-bit operands so I'm not sure what to put in that answer. | |
Mar 29, 2018 at 18:37 | comment | added | Peter Cordes |
Should probably go into the existing answer about constants, where anatolyg already suggested it in a comment. I'll edit that answer. IMO you should rework this one to suggest using 8-bit operand-size for more stuff (except xchg eax, r32 ) e.g. mov bl, 10 / dec bl / jnz so your code doesn't care about the high bytes of RBX.
|
|
Mar 29, 2018 at 18:30 | comment | added | qwr | @PeterCordes I've added your push/pop technique | |
Mar 29, 2018 at 18:29 | history | edited | qwr | CC BY-SA 3.0 |
added 166 characters in body
|
Mar 29, 2018 at 18:17 | comment | added | Peter Cordes |
The other use-case would be for constants from [0x80..0xFF] , which are not representable as a sign-extended imm8. Or if you already know the upper bytes, e.g. mov cl, 0x10 after a loop instruction, because the only way for loop to not jump is when it made rcx=0 . (I guess you said this, but your example uses an xor ). You can even use the low byte of a register for something else, as long as the something else puts it back to zero (or whatever) when you're done. e.g. my Fibonacci program keeps -1024 in ebx, and uses bl.
|
|
Mar 29, 2018 at 18:13 | comment | added | Peter Cordes |
mov al, 0xa is good if you don't need it zero-extended to the full reg. But if you do, xor/mov is 4 bytes vs. 3 for push imm8/pop or lea from another known constant. This could be useful in combination with mul to zero 3 registers in 4 bytes, or cdq , if you need a lot of constants, though.
|
|
Mar 29, 2018 at 17:57 | history | answered | qwr | CC BY-SA 3.0 |