User Tools

Site Tools


smallcpus

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
smallcpus [2012/12/09 18:39]
kris
smallcpus [2015/10/21 19:05] (current)
kris
Line 4: Line 4:
  
 ===== Enabling Assembler Programming on Arduino/​Uno32 ===== ===== Enabling Assembler Programming on Arduino/​Uno32 =====
 +
 +**More recent versions of the Arduino IDE recognize .S files, and no changes are needed. I've tested this with version 1.6.5. If you're using a recent IDE, you can skip ahead to 'The Blink Example In Assembler'​**
 +
 +**Make sure to also check '​Feedback from Readers'​ at the end: it contains some important info for newer versions of the IDE.**
  
 It is hard to get an in-depth understanding of Arduino and the various CPUs involved. The standard IDE involves a lot of '​magic'​ and there is not much help when you want to get a better understanding of how the magic works. It is hard to get an in-depth understanding of Arduino and the various CPUs involved. The standard IDE involves a lot of '​magic'​ and there is not much help when you want to get a better understanding of how the magic works.
Line 379: Line 383:
 http://​www.mindkits.co.nz/​tutorials http://​www.mindkits.co.nz/​tutorials
  
-as I ordered their ready-made tutorial stuff. I haven'​t progressed very far yet, as I keep getting sidetracked on all kinds of interesting ​things.+as I ordered their ready-made tutorial stuff. I haven'​t progressed very far yet, as I keep getting sidetracked on all kinds of interesting ​thing, like assembler-programming tricks.
  
-My next subject is part of Tutorial#0, where you wire up 8 LEDs to pins 2-9 of the Arduino and then make the LEDs light up in sequence, from left to right, then back from right to left, and so on.+My next subject is an assembler version for part of Tutorial#0, where you wire up 8 LEDs to pins 2-9 of the Arduino and then make the LEDs light up in sequence, from left to right, then back from right to left, and so on.
  
 Look for Exercise 0.1 on  Look for Exercise 0.1 on 
Line 392: Line 396:
  
 <​code>​ <​code>​
-/* 
-  Blink 
-  Turns on an LED on for one second, then off for one second, repeatedly. 
-  
-  This example code is in the public domain. 
- */ 
-  
-// Pin 13 has an LED connected on most Arduino boards. 
-// give it a name: 
 #define ledFrom ​ 2 #define ledFrom ​ 2
 #define ledTo 9 #define ledTo 9
Line 442: Line 437:
 </​code>​ </​code>​
  
-Then I set out to do as assembler-only version. In this version, the .ino file remains empty, and all you do is add a file '​knightrider.S'​ to the sketch on a second tab. You need to use the patched IDE for this - standard IDEs won't recognize the .S file.+Then I wrote assembler-only version. In this version, the .ino file remains empty, and all you do is add a file '​knightrider.S'​ to the sketch on a second tab. You need to use the patched IDE for this - standard IDEs won't recognize the .S file. As you'll see further, this first attempt is far from optimal.
  
 <​code>​ <​code>​
Line 643: Line 638:
 Before we can use this 8-bit value, we need to do some more shifting, because the LED's are driven from two different ports: 6 LEDs are driven by bit 2-7 of PORTD, and 2 LED are driven by bit 0-1 of PORTB. That's what all the //lsl// and //lsr// stuff is about. Before we can use this 8-bit value, we need to do some more shifting, because the LED's are driven from two different ports: 6 LEDs are driven by bit 2-7 of PORTD, and 2 LED are driven by bit 0-1 of PORTB. That's what all the //lsl// and //lsr// stuff is about.
  
-The rest of the code is very similar to our previous BlinkWithoutDelay as far as calculating millis and so on - so this version returns properly from the '​loop'​ routine each timt it is called; it does not get '​stuck'​.+The rest of the code is very similar to our previous BlinkWithoutDelay as far as calculating millis and so on - so this version returns properly from the '​loop'​ routine each time it is called; it does not get '​stuck'​.
  
 Now, I thought I should be able to do better, so I rewired the LEDs in such a fashion that I could drop those extra shifts. Now, I thought I should be able to do better, so I rewired the LEDs in such a fashion that I could drop those extra shifts.
Line 698: Line 693:
 </​code> ​ </​code> ​
  
-This one compiles to 610 bytes, and that surprised me - that was way smaller than I expected!+This one compiles to 610 bytes, and that surprised me - that is way smaller than I expected!
  
 I then did some digging, and checked out the assembly language output of the C-compiler, and as a result, learned a few new tricks. I then did some digging, and checked out the assembly language output of the C-compiler, and as a result, learned a few new tricks.
  
-First trick: the AVR instruction set has no '​ADCI'​ or '​ADI'​ instruction, and my first solution was to load the value into a register and then use ADC or ADD. +First trick: the AVR instruction set has no '​ADCI'​ or '​ADI'​ instruction. My solution was to load the value into a register and then use ADC or ADD. 
  
-The C-compiler has a much better trick up it's sleeve: the AVR instruction set //does// have a SBCI and SBI instruction,​ so instead of adding a constant value, ​they simply ​subtract ​the negative of the value, and no intermediate register is needed to hold the immediate values.+The C-compiler has a much better trick up it's sleeve: the AVR instruction set //does// have a SBCI and SBI instruction,​ so instead of adding a constant value, ​it simply ​subtracts ​the negative of the value, and no intermediate register is needed to hold the immediate values.
  
-I also completely overlooked the IN and OUT instructions,​ and instead was using memory addressing to access DDRB, DDRD, PORTB, PORTD as memory locations. The C compiler instead ​used IN and OUT, which again saved a few bytes.+I also completely overlooked the IN and OUT instructions,​ and instead was using memory addressing to access DDRB, DDRD, PORTB, PORTD as memory locations. The C compiler instead ​uses IN and OUT, which again saved a few bytes.
  
 While I was looking over the instruction set I also noticed a few more instructions that allowed me to save some more bytes: I found STS and LDS, which allow addressing via the Y register but with an offset applied. While I was looking over the instruction set I also noticed a few more instructions that allowed me to save some more bytes: I found STS and LDS, which allow addressing via the Y register but with an offset applied.
Line 828: Line 823:
 This last version is 590 bytes - 20 bytes less than the C compiler. ​ This last version is 590 bytes - 20 bytes less than the C compiler. ​
  
-Conclusion: the C compiler is doing a pretty good job, and the extra effort of writing things in assembler is probably rarely worth it. Subtracting the 462 bytes for the runtime, we're looking at saving a little over 10% in size for this particular exercise.+Conclusion: the C compiler is doing a pretty good job, and the extra effort of writing things in assembler is probably rarely worth it. Subtracting the 462 bytes for the runtime ​overhead from both compiled sketch sizes, we're looking at saving a little over 10% in size for this particular exercise. ​
  
 Nevertheless,​ sometimes 20 bytes can be the difference between 'it fits' and 'it does not fit', and, more importantly,​ I love tinkering, so I'll do some more assembler, just because I can! Nevertheless,​ sometimes 20 bytes can be the difference between 'it fits' and 'it does not fit', and, more importantly,​ I love tinkering, so I'll do some more assembler, just because I can!
Line 842: Line 837:
  
 Run it again. Is that cool, or what? Run it again. Is that cool, or what?
 +
 +===== Feedback from readers =====
 +
 +Hi Kris!
 +
 +Thank you very much for your Arduino asm introduction. I helped me a lot getting started.
 +
 +But then I tried to modify version 1.5.2 the same way, because according to the developer of the arduino eclipse plugin, this is the latest version compatible with the plugin. And I found out that you need to modify the file .../​hardware/​arduino/​avr/​platform.txt,​ too. So I thought, maybe you want to mention this in your introduction.
 +
 +in the "AVR compile patterns"​ section, adding the following solved the issue:
 +
 +## Compile S files
 +recipe.S.o.pattern="​{compiler.path}{compiler.c.cmd}"​ {compiler.S.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -D{software}={runtime.ide.version} {build.extra_flags} {includes} "​{source_file}"​ -o "​{object_file}"​
 +
 +Regards,
 +
 +Ralf
  
 ===== Stuff collected on the Internet ===== ===== Stuff collected on the Internet =====
smallcpus.1355031564.txt.gz ยท Last modified: 2012/12/09 18:39 by kris