delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/14/13:04:45

From: Robert Hoehne <Robert DOT Hoehne AT Mathematik DOT tu-chemnitz DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How is switch implemented?
Date: Mon, 14 Apr 1997 14:51:37 +0200
Organization: TU Chemnitz-Zwickau
Lines: 71
Message-ID: <33522859.6EB9@Mathematik.tu-chemnitz.de>
References: <F0AF0B21ED AT fs2 DOT mt DOT umist DOT ac DOT uk>
NNTP-Posting-Host: cancel.hrz.tu-chemnitz.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Anthony.Appleyard wrote:
> 
>   What happens if a switch(i){...} contains `case 0:' and also `case 0xffff:'?
> Does it make a jump table 0x10000 elements long, occupying 0x40000 bytes of
> program memory? Or if a jump table would be too big does djgpp use tests and
> jumps?

You can get the answer yourself for all the questions, by creating
a small source file with all the interesting cases and compile
it with

gcc -S test.c

Then you can look at the created test.s and all you questions
are answered.

(You will see, that gcc will optimize the switch in most
cases as good as you can imagine for speed and size.

Example:
/* file test.c */
int test(int val)
{
  switch(val)
  {
    case 0:
      return 0;
    case 0xffff:
      return 0xffff;
  }
}
/* end of file test.c */

/* file test.s after gcc -O -S test.c */
        .file   "test.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
        .align 2
.globl _test
_test:
        pushl %ebp
        movl %esp,%ebp
        movl 8(%ebp),%eax
        testl %eax,%eax
        je L3
        cmpl $65535,%eax
        je L4
        leave
        ret
        .align 2,0x90
L3:
        xorl %eax,%eax
        leave
        ret
        .align 2,0x90
L4:
        movl $65535,%eax
        leave
        ret

/* end of file test.s */

Robert
-- 
*****************************************************************
* Robert Hoehne, Fakultaet fuer Mathematik, TU-Chemnitz-Zwickau *
* Post:    Am Berg 3, D-09573 Dittmannsdorf                     *
* e-Mail:  Robert DOT Hoehne AT Mathematik DOT TU-Chemnitz DOT DE              *
* WWW:     http://www.tu-chemnitz.de/~rho                       *
*****************************************************************

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019