Re: Question about MS C code generation

tmk@netvision.net.il (Michael Tiomkin)
3 Dec 2003 20:21:05 -0500

          From comp.compilers

Related articles
Question about MS C code generation ruicui@sohu.com (2003-11-21)
Re: Question about MS C code generation joachim.durchholz@web.de (Joachim Durchholz) (2003-12-03)
Re: Question about MS C code generation tmk@netvision.net.il (2003-12-03)
Re: Question about MS C code generation vbdis@aol.com (2003-12-03)
Re: Question about MS C code generation bonzini@gnu.org (2003-12-03)
| List of all articles for this month |

From: tmk@netvision.net.il (Michael Tiomkin)
Newsgroups: comp.compilers
Date: 3 Dec 2003 20:21:05 -0500
Organization: http://groups.google.com
References: 03-11-090
Keywords: code
Posted-Date: 03 Dec 2003 20:21:05 EST

ruicui@sohu.com (sugaray) wrote in message news:03-11-090...
> Hi folks, I wrote the following C code, and compiled using
> Microsoft 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
>
  .... asm translation
>
> which using some kinda non-straightforward logic like this
>
> if(a>=0) {
> if(a<=0) b=0;
    if (a<0) ...
> else b=5;
> } else b=-5;


    No, this translation was a natural translation of your code.
When the compiler sees 'if (cond) stmt', the easiest translation is
first to jump to the else part of this 'if' if the condition is false,
and go through the 'stmt' under the 'if' otherwise.


> 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);


    This is not equivalent to your original code. The second
comparison should be '>='.


> 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
eax = 0 /* prepare the mask as all 0s */
> cmp [ebp+nA], 0 ; OBSERVATIONS WOULD BE
> WELCOME !!!!
> setle al ; if <= then AL=1
> dec eax ; EAX-- ;ESPECIALLY ON HOW AND
>WHY
eax = all 1s if a>0, and all 0s for a<=0, for the & operation below
/* the task is to make less jumps and more computations. */
> and eax, 5
if a>0 eax = 5, otherwise eax = 0
> 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 format ; "%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 ?
  No
> 3) what kinda logic style would you prefer ?


  To save space, you'd better remove the 'if(a>=0) ' and 'else b=0;' parts
from you program. They are irrelevant for execution of the program.


> 4) if asked, how would you write this program in assembly ?


    Compile your first program with -O2, and you'll see that the
compiler would translate only the 'printf(...,5)' statement. The
conditions in your code and the value of b (5) can be evaluated at
compile time.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.