views:

870

answers:

4

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

+6  A: 

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.

Byron Whitlock
"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.
R. Bemrose
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
What's a 028? .
configurator
+4  A: 

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.

Gamecat
+3  A: 

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
DigitalRoss
Citiation needed. Either that, or a smiley face somewhere.
Mark Ransom
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, http://remarque.org/~doug/
DigitalRoss
+3  A: 

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”? for more details on this one.

Justin Grant
from http://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 D
@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 D
XOR was mostly useful because it's one less byte and programs needed to be *small* back when dinosaurs rules the earth
configurator