Yahoo Groups archive

AVR-Chat

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

Thread

Re: [AVR-Chat] the "case" statement

Re: [AVR-Chat] the "case" statement

2005-02-21 by Leon Heller

----- Original Message -----
From: Eric
Sent: Monday, February 21, 2005 11:02 AM
Subject: [AVR-Chat] the "case" statement

Hi all chat'ers
In C/C++ there is a "case" statement, is there such a thing in assembly code? and is there a example
around.


e.g here is the C version
switch( i )
{
case -1:
n++;
break;
case 0 :
z++;
break;
case 1 :
p++;
break;
}

how do you do the line containing the "switch( i )" in assembly code.
A 'jump table ' is the usual way to do it.
Leon
--

the "case" statement

2005-02-21 by Eric

Hi all chat'ers
In C/C++ there is a "case" statement, is there such a thing in assembly code? and is there a example
around.


e.g here is the C version
switch( i )
{
    case -1:
        n++;
        break;
    case 0 :
        z++;
        break;
    case 1 :
        p++;
        break;
}

how do you do the line containing the "switch( i )" in assembly code.

From Eric
no attachment was sent with this email, if there is one, you might have a problem.

Re: [AVR-Chat] the "case" statement

2005-02-21 by Stefan Trethan

On Tue, 22 Feb 2005 00:02:30 +1300, Eric <erichards@clear.net.nz> wrote:

>
> Hi all chat'ers
> In C/C++ there is a "case" statement, is there such a thing in assembly  
> code? and is there a example
> around.

I think you use your variable to jump to different locations.
(add it to the address).
You must make sure you can't jump "out of bounds".
You'll probably make other jumps or calls at each of the possible  
addresses...

Note that my ASM days are a while back and there might well be cleaner  
things to do this.

ST

Re: [AVR-Chat] the "case" statement

2005-02-21 by Eric

it's OK all

I am beginning to think someone used a lot of "caseX" in their code, so they could read their code
easier. even thou is no assembly code "case" equivalent.

I just inserted break at the end of each "caseX" and it never worked.


From Eric
Show quoted textHide quoted text
----- Original Message -----
From: "Eric" <erichards@clear.net.nz>
To: <AVR-Chat@yahoogroups.com>
Sent: Tuesday, February 22, 2005 12:02 AM
Subject: [AVR-Chat] the "case" statement



Hi all chat'ers
In C/C++ there is a "case" statement, is there such a thing in assembly code? and is there a example
around.


e.g here is the C version
switch( i )
{
    case -1:
        n++;
        break;
    case 0 :
        z++;
        break;
    case 1 :
        p++;
        break;
}

how do you do the line containing the "switch( i )" in assembly code.

From Eric
no attachment was sent with this email, if there is one, you might have a problem.




Yahoo! Groups Links

Re: [AVR-Chat] the "case" statement

2005-02-21 by John Stiekema

// IAR assembler, variables byte-sized in registers:

#define i r16 //int8
#define n r17
#define z r18
#define p r19

	CPI r16,LOW(-1)	//test: -1?
	BRNE $+6
  	SUBI n,LOW(-1)
	RJMP break_
	TST r16		//test: 0?
	BRNE $+6
  	SUBI z,LOW(-1)
	RJMP break_
	CPI r16,1	//test: 1?
	BRNE $+6
  	SUBI p,LOW(-1)
	RJMP break_
      case_other:
	//case other code
      break_:


Eric wrote:
Show quoted textHide quoted text
> Hi all chat'ers
> In C/C++ there is a "case" statement, is there such a thing in assembly code? and is there a example
> around.
> 
> 
> e.g here is the C version
> switch( i )
> {
>     case -1:
>         n++;
>         break;
>     case 0 :
>         z++;
>         break;
>     case 1 :
>         p++;
>         break;
> }
> 
> how do you do the line containing the "switch( i )" in assembly code.
> 
>>From Eric
> no attachment was sent with this email, if there is one, you might have a problem.
> 
> 
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
>

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

Re: the "case" statement

2005-02-22 by Luke.Wei

> 
> how do you do the line containing the "switch( i )" in assembly 
code.
> 
> From Eric
> no attachment was sent with this email, if there is one, you might 
have a problem.
Dear, 

     "switch(i) " have so many methods to describe , you can use 
compare and branch instruction to try.
ex: cp cpi brne ...

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.