Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: the "case" statement

2005-02-21 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, Eric <erichards@c...> wrote:
> In C/C++ there is a "case" statement, is there such a thing
> in assembly code? and is there a example around.

There are two ways to implement the logic of a switch/case.  Which 
one is chosen depends on the circumstances.  If the case values are 
constant and are mostly contiguous, the best way is to use a jump 
table, i.e., a table of addresses of code that handles each case.  
You index the table by the selection value, load r31:r30 and do an 
ijmp (or icall).

On the other hand, if the case values are scattered or are not 
constant (think of a Basic select case statement) you probably will 
be best served by a series of tests - essentially an if/elseif/else 
chain.  Sometimes the two methods can be combined to handle the range 
of expected values.

The C compiler generates code using one or the other of these 
strategies (or a combination) depending on the nature of the switch.

Don

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.