what is faster in asm

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

what is faster in asm

Post by os64dev »

for my interrupt handler routines i have a question regarding some asm statements

here is my code

Code: Select all

#define wrapper(x)          extern "C" void x(isr_stack_frame*);               \
                            extern "C" void wrapper_##x(void);                 \
                            asm volatile (".align 0x10;"                       \
                                          "wrapper_"#x": "                     \
                                          "leaq     (%rsp), %rdi;"             \
                                          "call "#x"; iretq"                   \
                                         )

#define vector(x)           (vintpc)wrapper_isr##x
#define interrupt(x)        dummy(void); wrapper(isr##x); void isr##x

void interrupt(DivideByZero)(isr_stack_frame * frame) {
    frame = (isr_stack_frame *)((vintp)frame - 8);
    printf("#DivideByZero\n");
    printf("- errorCode               : %x\n", frame->errorCode);
    printf("- returnInstructionPointer: %x\n", frame->returnInstructionPointer);
    printf("- returnCodeSelector      : %x\n", frame->returnCodeSelector);
    printf("- flags                   : %x\n", frame->flags);
    printf("- stackPointer            : %x\n", frame->stackPointer);
    printf("- stackSelector           : %x\n", frame->stackSelector);
    for(;;);
}
this will define the functions isrDivideByZero and wrapper_isrDivideByZero my question is regarding the leaq statement. When an interrupt service routine does not have an error code i will use leaq $-8(%rsp), %rdi otherwise leaq (%rsp), %rdi

is this faster then using the movq %rsp, %rdi; addq $-8, %rdi alternative?
Author of COBOS
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: what is faster in asm

Post by Brendan »

Hi,

This depends on which CPU you're using. For Pentium 4 (netburst architecture) LEA is slow and "MOV & ADD" would be faster (especially if you can put another instruction between the MOV and the ADD to break register dependancies). For other CPUs (those derived from the P6 architecture - Core, CoreDuo, etc) I'm not so sure, and wouldn't be surprised if LEA was faster (or equivelent speed).

In either case the difference will be negligable compared to the time it takes the CPU to invoke the exception handler and the CALL/RET pair.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply