Search code examples
assemblyx86micro-optimization

Most compact way to test for a negative number in x86 assembly?


Currently I am doing this to test for a negative number in x86 assembly (r/m32,imm8):

83F800 CMP EAX, 0

This can be followed by JL. This is 3 bytes and supposedly clocks at "1/2". I could use TEST EAX, or CMP EAX,imm32 (encoding 3D), both of which clock at "1", but take 5 bytes. In general, if I am trying to minimize code size, is the way I am doing it correct? Once again, this is to test if a number is less than zero.


Solution

  • add eax, eax is only two bytes (01 C0 or 03 C0), but destructive. (check for carry afterwards)

    test eax, eax is also only two bytes (85 C0). (check for sign afterwards)