@@@严肃的问题@@@ [英] @@@ A Serious Question @@@
问题描述
大家好,我编写了以下C代码,并使用
编译Microsoft(R)32位C / C ++优化编译器版本13.10.3077
#include< stdio.h>
int main(void){
int a = 0;
int b;
if(a< 0)b = -5;
else if(a> = 0)b = 5;
其他b = 0;
printf("%d \ nn",b);
返回0; <
}
并且在拆解后,它变成了
_main proc near;修改后的版本带有我的评论
nB = dword ptr -8
nA = dword ptr -4
mov ebp,esp
sub esp,8
mov [ebp + nA],0; nA = 0
cmp [ebp + nA],0
jge short GE; if(nA> = 0)然后转到GE
mov [ebp + nB],-5
jmp short句柄
GE:
cmp [ebp + nA],0
jle short LE; if(nA <= 0)然后转到LE
mov [ebp + nB],5;这里只能是nA> 0是真的
jmp短句柄
LE:
mov [ebp + nB],0; if(nA> = 0&& nA< = 0)然后
句柄:
mov eax,[ebp + nB]
push eax
推送偏移格式; %d \ n>
调用_printf
添加esp,8
xor eax,eax
mov esp,ebp
pop ebp
retn
_main endp
哪个使用某种非 - 像这样的直截了当的逻辑
if(a> = 0){
if(a< = 0)b = 0;
其他b = 5;
}否则b = -5;
并且我以更简洁的方式重写原始C代码
只在这样的一行声明中
b =(a< 0)? - 5:((a> 0)?5:0);
然后反汇编代码就这样改变了
_main proc附近
nB = dword ptr -0Ch
nTmp = dword ptr -8
nA = dword ptr -4
push ebp
mov ebp,esp
sub esp,0Ch;设置堆栈框架
mov [ebp + nA],0; nA = 0
cmp [ebp + nA],0;比较nA与0
jge短GE; if(nA> = 0)然后转到GE
mov [ebp + nB],-5; if(nA <0)则nB = -5
jmp short句柄
GE:
xor eax,eax;这是KINDA TRICKY,任何评论
cmp [ebp + nA],0;观察结果将是欢迎!!!!
setle al; if< =然后AL = 1
dec eax; EAX--;特别是如何以及为什么
和eax,5
mov [ebp + nB],eax
句柄:
mov ecx,[ebp + nB]
mov [ebp + nTmp],ecx;为什么使用TEMPORARY VARIABLE?
mov edx,[ebp + nTmp];将nB存储到EDX
push edx;传递nB作为_printf的参数
push offset aD; "%d \ n"
call _printf;打印出值
add esp,8;恢复堆栈
xor eax,eax;将返回值设为0
mov esp,ebp;恢复堆栈指针
pop ebp;具有原始价值的退回EBP
retn;返回
_main endp
我的问题是:1)参考评论部分
2)它是否有用,可能为了优化目的?
3)你更喜欢什么样的逻辑风格?
4)如果被问到,你会如何在汇编中编写这个程序?
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
#include <stdio.h>
int main(void) {
int a=0;
int b;
if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;
printf("%d\n",b);
return 0;
}
and after disassembled, it became
_main proc near ; modified version with my comments
nB = dword ptr -8
nA = dword ptr -4
push ebp
mov ebp, esp
sub esp, 8
mov [ebp+nA], 0 ; nA=0
cmp [ebp+nA], 0
jge short GE ; if(nA >= 0) then goto GE
mov [ebp+nB], -5
jmp short Handle
GE:
cmp [ebp+nA], 0
jle short LE ; if(nA <= 0) then goto LE
mov [ebp+nB], 5 ; here only can be nA>0 is true
jmp short Handle
LE:
mov [ebp+nB], 0 ; if (nA >= 0 && nA <= 0 ) then
Handle:
mov eax, [ebp+nB]
push eax
push offset format ; "%d\n"
call _printf
add esp, 8
xor eax, eax
mov esp, ebp
pop ebp
retn
_main endp
which using some kinda non-straightforward logic like this
if(a>=0) {
if(a<=0) b=0;
else b=5;
} else b=-5;
and after I rewrite the original C code in a more succinct way
only in one line statement like this
b=(a<0)?-5:((a>0)?5:0);
then the disassembled code changed accordingly like this
_main proc near
nB = dword ptr -0Ch
nTmp = dword ptr -8
nA = dword ptr -4
push ebp
mov ebp, esp
sub esp, 0Ch ; Set up stack frame
mov [ebp+nA], 0 ; nA=0
cmp [ebp+nA], 0 ; compare nA with 0
jge short GE ; if (nA >= 0) then goto GE
mov [ebp+nB], -5 ; if (nA < 0) then nB=-5
jmp short Handle
GE:
xor eax, eax ; IT''S KINDA TRICKY, ANY COMMENTS
cmp [ebp+nA], 0 ; OBSERVATIONS WOULD BE WELCOME !!!!
setle al ; if <= then AL=1
dec eax ; EAX-- ;ESPECIALLY ON HOW AND WHY
and eax, 5
mov [ebp+nB], eax
Handle:
mov ecx, [ebp+nB]
mov [ebp+nTmp], ecx ;WHY USE A TEMPORARY VARIABLE ?
mov edx, [ebp+nTmp] ; store nB to EDX
push edx ; pass nB as parameter of _printf
push offset aD ; "%d\n"
call _printf ; print out the value
add esp, 8 ; restore stack
xor eax, eax ; set return value to 0
mov esp, ebp ; restore stack pointer
pop ebp ; retore EBP with original value
retn ; return
_main endp
my question is: 1) refer to the comment parts
2) was it any good, probably for the optimization purpose ?
3) what kinda logic style would you prefer ?
4) if asked, how would you write this program in assembly ?
推荐答案
sugaray写道:
sugaray wrote:
并且在拆解后,它变成了
and after disassembled, it became
它似乎你的问题更适合在
comp.lang.asm.x86而不是comp.lang.c
It seems that your questions would be more appropriate in
comp.lang.asm.x86 instead of comp.lang.c
sugaray< ru **** @ sohu.com>写道:
sugaray <ru****@sohu.com> wrote:
嗨伙计们,我编写了以下C代码,并编译使用
Microsoft(R)32位C / C ++优化编译器版本13.10.3077
#include< ; stdio.h中>
int main(void){
int a = 0;
int b;
if(a< 0)b = -5;
else if(a> = 0)b = 5;
b = 0;
这个分支永远不会被采取,无论'a'有什么价值。
printf("%d \ nn,b);
返回0;
}
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 #include <stdio.h> int main(void) {
int a=0;
int b;
if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;
This branch never can be taken, whatever value ''a'' has.
printf("%d\n",b);
return 0;
}
你不需要查看反汇编的代码来优化这个>
程序,只需将其写为
#include< stdio.h>
#include< stdlib.h>
int main(无效)
{
puts(" 5");
返回EXIT_SUCCESS;
}
这就是归结为。这就是使用
优化的聪明编译器应该很容易找到的。但是什么样的机器代码
编译器将你的程序翻译成clc的主题。
问候,Jens
-
_ _____ _____
| || _ _ || _ _ | Je *********** @ physik.fu-berlin.de
_ | | | | | |
| | _ | | | | | | http://www.physik.fu-berlin.de/~toerring
\ ___ / ens | _ | homs | _ | oerring
You don''t need to look at the disassembled code to optimize this
program, just write it as
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
puts( "5" );
return EXIT_SUCCESS;
}
which is all it boils down to. And that''s what a clever compiler with
optimization should easily find out. But to what kind of machine code
the compiler translates your program to is off-topic on clc.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
sugaray写了一个关于微优化的问题< ot>汇编代码
检查< / ot>:
sugaray wrote a question about micro-optimization with <ot>assembly code
inspection</ot>:
嗨大家好,我写了下面的C代码,并使用
Microsoft编译(R )32位C / C ++优化编译器版本13.10.3077
#include< stdio.h>
int main(void){
int a = 0;
int b;
if(a< 0)b = -5;
如果(a> = 0)b = 5;
b = 0;
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077
#include <stdio.h>
int main(void) {
int a=0;
int b;
if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;
我无法相信那些会写上述内容的人给出了关于优化的老鼠'屁股'
。如果!(a <0)则(a> = 0)。这意味着如果(a <0)b = -5,上述
条件将崩溃为
;否则b = 5;
或
b =(a <0)? -5:5;
第二个'if''没用,第二个''else''。
-
Martin Ambuhl
I cannot believe that someone who would write the above gives a rat''s ass
about optimization. If !(a<0) then (a>=0). That means that the above
conditionals collapse to
if (a < 0) b = -5; else b = 5;
or
b = (a < 0) ? -5 : 5 ;
The second ''if'' is useless, as is the second ''else''.
--
Martin Ambuhl
这篇关于@@@严肃的问题@@@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!