18

In either of setup or loop, if I were to add an exit(0) call, where would control be passed to? What would the next state of the microcontroller be? Would it stop execution and power down?

I am using a revision 2 Arduino Uno.

Anonymous Penguin
6,36510 gold badges34 silver badges62 bronze badges
asked Feb 14, 2014 at 1:28
0

2 Answers 2

12

My initial guess is wrong. I would have thought it would simply return from loop and the core library would just call loop() again. However, I see the following code was created. Noticing that __stop_program is a hard loop...

An extract of Blink.ino's listing, with exit(0) added:

// the loop routine runs over and over again forever:
void loop() {
 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
 delay(1000); // wait for a second
 exit(0);
}

The disassembly of the above:

// the loop routine runs over and over again forever:
void loop() {
 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
 100: 80 91 00 01 lds r24, 0x0100
 104: 61 e0 ldi r22, 0x01 ; 1
 106: 0e 94 ca 01 call 0x394 ; 0x394 <digitalWrite>
 delay(1000); // wait for a second
 10a: 68 ee ldi r22, 0xE8 ; 232
 10c: 73 e0 ldi r23, 0x03 ; 3
 10e: 80 e0 ldi r24, 0x00 ; 0
 110: 90 e0 ldi r25, 0x00 ; 0
 112: 0e 94 f7 00 call 0x1ee ; 0x1ee <delay>
 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
 116: 80 91 00 01 lds r24, 0x0100
 11a: 60 e0 ldi r22, 0x00 ; 0
 11c: 0e 94 ca 01 call 0x394 ; 0x394 <digitalWrite>
 delay(1000); // wait for a second
 120: 68 ee ldi r22, 0xE8 ; 232
 122: 73 e0 ldi r23, 0x03 ; 3
 124: 80 e0 ldi r24, 0x00 ; 0
 126: 90 e0 ldi r25, 0x00 ; 0
 128: 0e 94 f7 00 call 0x1ee ; 0x1ee <delay>
 exit(0);
 12c: 80 e0 ldi r24, 0x00 ; 0
 12e: 90 e0 ldi r25, 0x00 ; 0
 130: 0e 94 1e 02 call 0x43c ; 0x43c <_exit>
...
0000043c <_exit>:
 43c: f8 94 cli
0000043e <__stop_program>:
 43e: ff cf rjmp .-2 ; 0x43e <__stop_program>

Note that if _exit had not called cli, interrupts would be able to do stuff. But that is not the case.

Connor Wolf
2,72413 silver badges16 bronze badges
answered Feb 14, 2014 at 4:00
4
  • avr-objdump -S {compiled *.elf file} produces a file that includes the C code that lead to each section of assembly code. It's much easier to follow. Commented Feb 17, 2014 at 4:02
  • aaaand I just tried it, and it's not correctly emitting inline C code for the loop function. What the hell? Commented Feb 17, 2014 at 4:08
  • Whoa, extremely strange. I compiled the project with Stino instead of the arduino editor, decompiled the *.elf from that, and then I get the proper debugging symbols. I think the Arduino text-editor/button-macro (I refuse to call it an IDE since it's not) thing is stripping the debug info from just the compiled main C++ file, for some bizarre and stupid reason. Commented Feb 17, 2014 at 4:15
  • There is an explanation, and it is to do with the way the IDE copies your files to a temporary location. You can "fix" that by telling avr-objdump where the source is: avr-objdump -S -I/path/to/the/sketch/folder xxx.elf . That is the sketch folder path, not the .ino file itself. Then you should get the C source in the dump. Commented Aug 15, 2015 at 4:59
11

Well I just tested it with my Arduino Uno and it just completely stopped the code and left all the outputs as they were when the code stopped running (so it left an LED I had on on). There seems to not be a IO cleanup when you call exit. This was what I expected because the Arduino IDE provides the setup and loop functions, if you program the ATMEGA*28 with with any other AVR IDE you start with the main function like all C/C++ programs. The setup and loop functions are not standard on AVR MCU's.

Note: The press of the reset button restarts the code, if you were wondering.

answered Feb 14, 2014 at 1:42
2
  • Good to know. I was looking for something more detailed and at a lower level. On calling exit(0) the disassembled instructions are (IIRC) __stop_program, cli and a spinlock. I wanted to verify if that is correct with an explanation of how the control is passed i.e. call stack pop?, ISR call? Commented Feb 14, 2014 at 2:05
  • Ah, well I haven't looked into arduino on such a low level, for that information you might want to check the atmel website. Commented Feb 14, 2014 at 2:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.