JMP Is Optimized?
From: Bryan Parkoff (bryan.nospam.parkoff_at_nospam.com)
Date: 03/27/04
- Next message: Bryan Parkoff: "Same Performance for Both Routines???"
- Previous message: Anish: "Re: NASM fails to FAR Jump to specified Address"
- Next in thread: Matt Taylor: "Re: JMP Is Optimized?"
- Reply: Matt Taylor: "Re: JMP Is Optimized?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Mar 2004 21:14:02 +0000 (UTC)
I understand that Matt Taylor has advised me that switch (...) is always
optimized by C/C++ Compiler rather than doing optimization by my hand.
Please look at my machine code below.
push esi
xor esi,esi
cmp esi,4
ja $L608+0Fh
jmp dword ptr [esi*4+401038h]
push 0
jmp $L608+2 // goto printf
push 1
jmp $L608+2 // goto printf
push 2
jmp $L608+2 // goto printf
push 3
jmp $L608+2 // goto printf
push 4
push offset string "%d"
call _printf
add esp,8
inc esi
cmp esi,5
jl main+3
pop esi
ret
Notice that my first machine code has "jmp $L608+2 // goto
printf" is executed each time it encounters then it goes back again to
execute next "jmp $L608+2 // goto printf". It is the fact that
switch (...) is inside while (x < y).
Look at my second machine code.
push esi
xor esi,esi
cmp esi,4
ja $L608+0Fh
jmp dword ptr [esi*4+401038h]
push 0
push offset string "%d"
call _printf
add esp,8
inc esi
cmp esi,5
jl main+3
jmp dword ptr [esi*4+401038h]
push 1
push offset string "%d"
call _printf
add esp,8
inc esi
cmp esi,5
jl main+3
jmp dword ptr [esi*4+401038h]
push 2
push offset string "%d"
call _printf
add esp,8
inc esi
cmp esi,5
jl main+3
jmp dword ptr [esi*4+401038h]
push 3
push offset string "%d"
call _printf
add esp,8
inc esi
cmp esi,5
jl main+3
jmp dword ptr [esi*4+401038h]
push 4
push offset string "%d"
call _printf
add esp,8
inc esi
cmp esi,5
jl main+3
jmp dword ptr [esi*4+401038h]
pop esi
ret
Notice "jmp $L608+2 // goto printf" is replaced to "jmp
dword ptr [esi*4+401038h]". Each "case xxx" has its own individual code
rather than single code through JMP $L608+2". "JMP [esi*4+xxxxxxxx]" helps
to handle individual code.
I believe that second machine code can be very optimized better than
first machine code by running much faster, but I am aware that second
machine code can increase the size like duplicated JMP routine.
Please tell me what you think is best to use first machine code or
second machine code. Thanks...
-- Bryan Parkoff
- Next message: Bryan Parkoff: "Same Performance for Both Routines???"
- Previous message: Anish: "Re: NASM fails to FAR Jump to specified Address"
- Next in thread: Matt Taylor: "Re: JMP Is Optimized?"
- Reply: Matt Taylor: "Re: JMP Is Optimized?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|