2011-02-09 22:24:42

by Jidong Xiao

[permalink] [raw]
Subject: Can anyone explain "movl %eax %eax"?

Hi,

In the kernel source, I see in a couple of places, there is "movl %eax
%eax". Is this used for alignment purpose?

For example, in the following piece of code we can see "movl %eax,%eax".

407 ENTRY(ia32_syscall)
408 CFI_STARTPROC32 simple
409 CFI_SIGNAL_FRAME
410 CFI_DEF_CFA rsp,SS+8-RIP
411 /*CFI_REL_OFFSET ss,SS-RIP*/
412 CFI_REL_OFFSET rsp,RSP-RIP
413 /*CFI_REL_OFFSET rflags,EFLAGS-RIP*/
414 /*CFI_REL_OFFSET cs,CS-RIP*/
415 CFI_REL_OFFSET rip,RIP-RIP
416 PARAVIRT_ADJUST_EXCEPTION_FRAME
417 SWAPGS
418 /*
419 * No need to follow this irqs on/off section: the syscall
420 * disabled irqs and here we enable it straight after entry:
421 */
422 ENABLE_INTERRUPTS(CLBR_NONE)
423 movl %eax,%eax
424 pushq %rax
425 CFI_ADJUST_CFA_OFFSET 8
426 cld
427 /* note the registers are not zero extended to the sf.
428 this could be a problem. */
429 SAVE_ARGS 0,0,1
430 GET_THREAD_INFO(%r10)
431 orl $TS_COMPAT,TI_status(%r10)
432 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%r10)
433 jnz ia32_tracesys
434 cmpq $(IA32_NR_syscalls-1),%rax
435 ja ia32_badsys
436 ia32_do_call:
437 IA32_ARG_FIXUP
438 call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
439 ia32_sysret:
440 movq %rax,RAX-ARGOFFSET(%rsp)
441 ia32_ret_from_sys_call:
442 CLEAR_RREGS -ARGOFFSET
443 jmp int_ret_from_sys_call


Regards
Jidong


2011-02-09 22:32:30

by H. Peter Anvin

[permalink] [raw]
Subject: Re: Can anyone explain "movl %eax %eax"?

On 02/09/2011 02:24 PM, Jidong Xiao wrote:
> Hi,
>
> In the kernel source, I see in a couple of places, there is "movl %eax
> %eax". Is this used for alignment purpose?
>
> For example, in the following piece of code we can see "movl %eax,%eax".
>

In x86-64, a dword (long) operation clears the upper 32 bits of the
target register, so "movl %eax,%eax" clears the upper 32 bits of %rax.

-hpa

2011-02-09 23:02:12

by Jidong Xiao

[permalink] [raw]
Subject: Re: Can anyone explain "movl %eax %eax"?

Oh, I see. Thank you. So similarly, the operation "xorl %eax,%eax" is
used for the same reason, right? I see that appears in more files.

Regards
Jidong

On Wed, Feb 9, 2011 at 5:32 PM, H. Peter Anvin <[email protected]> wrote:
> On 02/09/2011 02:24 PM, Jidong Xiao wrote:
>> Hi,
>>
>> In the kernel source, I see in a couple of places, there is "movl %eax
>> %eax". Is this used for alignment purpose?
>>
>> For example, in the following piece of code we can see "movl %eax,%eax".
>>
>
> In x86-64, a dword (long) operation clears the upper 32 bits of the
> target register, so "movl %eax,%eax" clears the upper 32 bits of %rax.
>
> ? ? ? ?-hpa
>
>

2011-02-09 23:04:39

by David Miller

[permalink] [raw]
Subject: Re: Can anyone explain "movl %eax %eax"?

From: Jidong Xiao <[email protected]>
Date: Wed, 9 Feb 2011 18:02:09 -0500

> Oh, I see. Thank you. So similarly, the operation "xorl %eax,%eax" is
> used for the same reason, right? I see that appears in more files.

The xorl clears the entire register.

2011-02-10 01:32:44

by Jidong Xiao

[permalink] [raw]
Subject: Re: Can anyone explain "movl %eax %eax"?

Thank you David and Peter.

And I found more information in the following website, other people
who are not very clear can have a look at this document:

http://www.x86-64.org/documentation/assembly.html

======================
Implicit zero extend

Results of 32-bit operations are implicitly zero extended to 64-bit
values. This differs from 16 and 8 bit operations, that don't affect
the upper part of registers. This can be used for code size
optimisations in some cases, such as:

movl $1, %eax # one byte shorter movq $1, %rax
xorq %rax, %rax # three byte equivalent of mov $0,%rax
andl $5, %eax # equivalent for andq $5, %eax
======================

Regards
Jidong

On Wed, Feb 9, 2011 at 6:05 PM, David Miller <[email protected]> wrote:
> From: Jidong Xiao <[email protected]>
> Date: Wed, 9 Feb 2011 18:02:09 -0500
>
>> Oh, I see. Thank you. So similarly, the operation "xorl %eax,%eax" is
>> used for the same reason, right? I see that appears in more files.
>
> The xorl clears the entire register.
>