Go Down

### Topic: Using goto instead void calls. (Read 788 times)previous topic - next topic

#### Begraphics

##### Mar 01, 2016, 11:45 am
I am thinking this method to avoid a lot void calls that results variables must be saved outside void setup, void loop, and other calls functions which leads bigger program size.

F.ex:
loop()
{
// code here

position with if else if as void function would do.
code with if else if to using goto to go to correct position with value before function.
<- goto pass_fake_void_function;
|     trigonometry:
|     Doing calculation square root
|     // square root calculation
|     // then
|     if ( value == 1 ) { goto value1; } else if ( value == 2 ) { goto value2; }
|   }
|   value1: ;
-> pass_fake_void_function: ;
//loop go as normal....
if several function call
value = 2;
goto trigonometry;
value2: ;

Do you understood?

#### Whandall

#1
##### Mar 01, 2016, 11:54 am
Did you understand the C-language switch construct?
Why do you want to develop a goto based replacement?

BTW: Using functions (if not used for clarity only) most often leads to smaller code.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

#### Begraphics

#2
##### Mar 01, 2016, 12:04 pm
You asking me this question, then prove me an example of switch construct.
You did not mention where to put switchCase.

#### Whandall

#3
##### Mar 01, 2016, 12:07 pm
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

#4
OK, I will try.

#### aarg

#5
##### Mar 01, 2016, 12:13 pmLast Edit: Mar 01, 2016, 12:32 pm by aarg
In effect, you have created an indexed return stack. It is hampered by the need to associate multiple return points with arbitrary numbers. It has the overhead of having to test the numbers, to match the correct return point. This cancels out any gain that you might have made from not having to place return addresses on the stack and pull them off (which is automatic on most processors). Note that you are not required to pass and return parameters from a function. You can use global variables. It is just convenient and aligns well with a lot of fundamental design approaches, to pass and return parameters..

Code: [Select]
`value = 2;    goto trigonometry;    value2: ;`
is like the processor level actions that go with a subroutine call:
Code: [Select]
`save return addressload the sub address into the program counter`
but the processor does that faster than your code.
Close, but no cigar.
... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

#### Robin2

#6
##### Mar 01, 2016, 12:22 pm
to avoid a lot void calls that results variables must be saved outside void setup, void loop, and other calls functions which leads bigger program size.
The size of storage space for variables is unlikely to be reduced by the use of GOTO whereas the tangles in your code will grow exponentially.

Just use functions.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### Budvar10

#7
##### Mar 01, 2016, 12:35 pmLast Edit: Mar 01, 2016, 12:36 pm by Budvar10
It is able to define the function as naked ( __attribute__ ((naked)) ) in avr-gcc. Compiler doesn't provide any prolog and epilog for such function - save/restore the registers. In C it is still function, in result it is replaced by jump.
Maybe this helps.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

#### aarg

#8
##### Mar 01, 2016, 01:09 pmLast Edit: Mar 01, 2016, 01:12 pm by aarg
It is able to define the function as naked ( __attribute__ ((naked)) ) in avr-gcc. Compiler doesn't provide any prolog and epilog for such function - save/restore the registers. In C it is still function, in result it is replaced by jump.
Maybe this helps.

I doubt it. A jump would be the result of a goto.

Also, I wonder if the optimizer doesn't already remove all the overhead automatically when a function is a void f(void);
... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

#### Begraphics

#9
##### Mar 01, 2016, 03:09 pm
I doubt it. A jump would be the result of a goto.

Also, I wonder if the optimizer doesn't already remove all the overhead automatically when a function is a void f(void);
Yes, I wondering that too about the optimizer.
That's the reason I started this topic.

Go Up