Question about MS C code generation

ruicui@sohu.com (sugaray)
21 Nov 2003 00:52:11 -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: ruicui@sohu.com (sugaray)
Newsgroups: comp.compilers
Date: 21 Nov 2003 00:52:11 -0500
Organization: http://groups.google.com
Keywords: code, question
Posted-Date: 21 Nov 2003 00:52:11 EST

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


_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 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 ?
                                3) what kinda logic style would you prefer ?
                                4) if asked, how would you write this program in assembly ?


Post a followup to this message

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