Why would one use “movl $1,

后端 未结 6 1513
旧时难觅i
旧时难觅i 2021-01-31 10:30

As the title states, why would one use \"movl $1, %eax\" as opposed to, say, \"movb $1, %eax\", I was told that movl would zero out the high order bits of %eax, but isn\'t %eax

6条回答
  •  青春惊慌失措
    2021-01-31 10:48

    Your second choice will just produce an error, x86 doesn't have that instruction. X86 is a bit unique with respect to loading bytes into certain registers. Yes, on most instruction set architectures the operand is zero or sign-extended, but x86 allows you to write just the lower byte or lower 16 bits of some of them.

    There are certainly other choices, like clearing the register and then incrementing it, but here are three initially reasonable-looking choices you have:

       0:   b8 01 00 00 00          movl   $0x1,%eax
    
       5:   31 c0                   xorl   %eax,%eax
       7:   b0 01                   movb   $0x1,%al
    
       9:   b0 01                   movb   $0x1,%al
       b:   0f b6 c0                movzbl %al,%eax
    

    The first is 5 bytes, the second 4, the third 5. So the second is the best choice if optimizing for space, otherwise I suppose the one most likely to run fast is the first one. X86 is deeply pipelined these days, so the two instructions will interlock and the machine may need quite a few wait states depending on details of the pipeline hardware.

    Of course, these x86 ops are being translated in CPU-specific ways into CPU micro-ops, and so who knows what will happen.

提交回复
热议问题