share
Stack OverflowHidden features of x86 assembly language?
[+7] [5] bludger
[2009-10-15 18:50:43]
[ assembly x86 tips-and-tricks hidden-features ]
[ http://stackoverflow.com/questions/1574308/hidden-features-of-x86-assembly-language ] [DELETED]

What are some hidden-features [1] of x86 Assembly Language? What Tips and Tricks do you have for working with x86 Assembly language?

(3) @Ravi in line with the other 'hidden features of' questions, this should be made wiki. - George Stocker
(6) There is no one "assembly language" - it depends on the assembler, and the target platform - 1800 INFORMATION
(2) "Hidden"? It's the essential definition of the processor chip. Are you asking about chip features undocumented in the assembler reference? Or are you asking a about "magical", "previously unknown" things you can do with assembler? - S.Lott
@ S.Lott Its the later one I am talking about. - bludger
@Jacob: I think our edits collided; so I rolled back your changes for my own broader changes. That way you can peruse what I did and make changes based off of that. - George Stocker
Not a real question?What the heck does this mean? - bludger
It's vague, doesn't have an answer, etc. Really it becomes whatever the majority thought it should be closed as. I picked 'not a real question' because it was vague, doesn't have a discrete answer, and at the time wasn't a wiki'd question. - George Stocker
@ George Now its a wiki as well as a specific question and not vague anymore.So what you say? - bludger
I think it deserve to reopen now. - bludger
@Michael our edits collided. - George Stocker
@RAvi: Petitioning for re-opening is not something done in the body of the question. It's a Stack Overflow 'no-no'. I moved to re-open it, for what it's worth, but that was before your plea went out. - George Stocker
George OK n sorry about that.I am learning a lot about Stack Overflow day by day. Thanks to guys like you. :) - bludger
(2) @S. Lott "Hidden" means "Most developers don't know it, because they are too lazy to read the documentation". - starblue
@starblue: Really? The question doesn't actually say that. How do you know what the question means? - S.Lott
@S.Lott That definition is inferred from other questions asking for "hidden" features. - starblue
[+6] [2009-10-15 18:55:46] Byron Whitlock [ACCEPTED]

Assembly rocked the world of its era because it freed programmers from manually writing machine code. We got lots of complex instruction sets to help programmers do multiple things in one instruction. There isn't anything hidden or powerful that cannot be accomplished with a compiler. We are at billions of cycles per second, so a instruction that does something in 1 cycle instead of 2 or 3 is not very exciting anymore.


(3) "Assembly rocked the world of its era because it freed programmers from manually writing machine code." All it did was change those instructions to symbolic names. It's still essentially the same language, and it's still tied to a specific processor architecture. - Powerlord
(1) True, but have you ever written machine code? Absolute memory addressing, no symbolic constants and instructions that look like 1001010011. Assembler was revolutionary in its time. - Byron Whitlock
@Byron: Most of them are not going to believe us. /* was originally a bootloader - tweaking down one or two bytes could get your employer's $50,000 investment useful ... ever heard of "The Wortman Mod" ? a successful attempt to get an 028 to print R tp L - it worked, I saw it. - Nicholas Jordan
(2) What's a 028? . - configurator
1
[+5] [2009-10-15 18:56:46] Gamecat

Almost each processor has undocumented instructions and or registers. But they are often undocumented for a reason so its often not wise to use them.


2
[+4] [2009-10-15 19:03:17] Justin Grant

One of the interesting things about assembly language is that the smallest and/or fastest instructions are not necessarily intuitive. For example, to set the EAX register to zero, instead of mov eax,0, you use xor eax,eax which is fewer bytes but does the same thing at the same speed.

See Any reason to do a “xor eax, eax”? [1] for more details on this one.

[1] http://stackoverflow.com/questions/1396527/any-reason-to-do-a-xor-eax-eax

(2) from en.wikipedia.org/wiki/Michael_Abrash For example, the bit-oriented XOR %EAX, %EAX instruction was the fastest way to set a register to zero in the early generations of the x86, but most code is generated by compilers and compilers rarely generated XOR instruction. So the IA designers, decided to move the frequently occurring compiler generated instructions up to the front of the combinational decode logic making the literal MOVL $0, %EAX instruction execute faster than the XOR instruction. - Nick Dandoulakis
(5) @Nick: just another reason that you must always always profile your code when doing optimizations. 9 times in 10 the difference will be undetectable, and in half of the rest it will be opposite of what you think. - Mark Ransom
@Mark Ransom, I totally agree. - Nick Dandoulakis
(1) XOR was mostly useful because it's one less byte and programs needed to be small back when dinosaurs rules the earth - configurator
3
[+3] [2009-10-15 18:58:49] DigitalRoss

Formerly secret hidden-feature revealed...


Now that computers are so fast, they are hard to actually stop. A single halt instruction is unreliable, and so just calling halt() in a high level language isn't necessarily going to work if it's an old library routine.

Therefore, the following only-in-assembler design pattern is suggested:

   _halt::
      halt
      halt
      halt
      halt
      jmp   _halt
      halt          ; fill branch delay slot

(8) Citiation needed. Either that, or a smiley face somewhere. - Mark Ransom
(2) I have no idea if this is serious or not. That's kinda sad, isn't it? - Michael Myers
@Mark Ransom: I typed this in from memory but I think it's originally from Doug Merritt, remarque.org/~doug - DigitalRoss
4
[0] [2011-03-23 16:13:05] Manish Kumar Baderiya
- Set mm7 to 0x FF00FF00FF00FF00:
pcmpeqd mm7, mm7 // 0xFF FF FF FF FF FF FF FF
psllq mm7, 8 // 0xFF FF FF FF FF FF FF 00
pshufw mm7, mm7, 0x0 // 0xFF 00 FF 00 FF 00 FF 00

Each instruction takes two clock cycles to complete. The whole operation will finish in six clock cycles.

Faster:

pxor mm7, mm7 // 0x 0
pcmpeqd mm0, mm0 // 0x FFFFFFFFFFFFFFFF
punpcklbw mm7, mm0 // 0x FF00FF00FF00FF00

5