Arduino inline assembly

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

For this lab experiment you will create a program that uses only inline assembly (See Listing
1). You will need to reference the Instruction Set Summary from the ATmega328P data sheet.
1. Your program must configure PB0 as an input with internal pull-up enabled.
2. Your program must configure PB1 as an output.
3. Implement the equivalent of the infinite while loop in assembly
4. Inside the infinite while loop read PB0 and write the same value to PB1.
 

what is wrong in my code?



// Includes
#include <stdint.h>
// Defines
int main()
{
  init();

  Serial.begin(9600);
  Serial.write("Start of program/n");
  
            asm volatile("ldi r16, 0x01    \n\t" 
             "sts 0x25, r16    \n\t");// PORTB = 0000 0001 turn on internal pull-up on PB0
            asm volatile( "ldi r16, 0x01    \n\t"
                "sts 0x24, r16    \n\t");// DDRB = 000 0010)
        while(1){
           asm volatile( "lds r16, 0x23    \n\t"    // read PINB
                         "sbrc r16, 0  \n\t");
           asm volatile("movw r16,0x23  \n\t"   //copy from PINB
                        "sts 0x24,r17  \n\t");
                 }
                 
}

 

az

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVR Freaks!

 

I applaud the fact that you have written something first, on your own, before posing a question.

 

I do not see anything that focuses on the 0 bit of PORTB and uses that value for the 1 bit of PORTB. There are several ways that it can be done.

 

Also, you are not doing: "Implement the equivalent of the infinite while loop in assembly"  What you wrote is C.  Consider what a jmp or rjmp might do.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Mon. Dec 16, 2019 - 01:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The movw instruction is wrong; I don't think it does what you think its doing...
You're writing PORTB with a full byte, without adjusting the B0/B1 as required.

You should PROBABLY be using IO instructions rather than LDS/STS instructions, depending on the emphasis of the class.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

look at SBIC & SBIS commands

 

sbic PINB, frog

sbis PINB, legs

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
Consider what a jmp or rjmp might do. 

 

I got burned very badly by that once when someone else decided to 'refactor' my assembler code into C.  He treated all code segments as subroutines, as if they all returned.  Many didn't - they 'jmp'ed back - or 'jmp'ed somewhere else.  Can we say "Stack-Smashing"!?!  I knew you could...  My code didn't do that.  His did.  Guess who got to fix it...  *grrr*  (Sorry, pet peeve.  Have fun, all!) 

 

smiley   S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was thinking of the jmp as part of the infinite loop structure.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Have you read?:

 

https://www.nongnu.org/avr-libc/user-manual/inline_asm.html

 

The code would be clearer (perhaps?) if it actually names PORTB, DDRB etc.

 

But anyway, here's a way to cheat...

 

Write exactly what you need to do in C, build with -save-temps, study the .s file it creates ;-) (this may be more difficult in the Arduino world than for normal C development!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Write exactly what you need to do in C, build with -save-temps, study the .s file it creates

+1

 

Does Arduino allow linking-in separate Assembler modules?

 

ie, is this an option ?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

As far as I know, yes it does allow .S to be added. So it would be "cleaner" than trying to use asm("") in C.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
is this an option ?

Ah - not for this particular homework assignment:

ard98 wrote:
For this lab experiment you will create a program that uses only inline assembly

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

And that is where, personally, I think this falls down as an assignment. While it's great to get students to understand the basics Asm, the tortuous inline syntax completely obfuscates the learning process (assuming you want to do it symbolically and not with magic numbers like 0x24 and 0x25 etc). 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Agreed - if you want to learn/teach assembler, then do it properly in a proper assembler file.

 

Plus inline assembler is all entirely proprietary - so not really a very useful thing to learn, IMO.

 

EDIT

 

@ ard98 - Of course, you have to do the homework as set. Just bear in mind that it's (probably) not a great way to do things in Real Life ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Dec 16, 2019 - 12:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I dunno.  If it's a more generic "embedded programming" class, then going through the "inline asm without symbols", "passing symbols to inline asm", "using external asm" sequence might be useful to show...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are several errors, let me just pick one. The numbers don't match:

 

ard98 wrote:

            asm volatile( "ldi r16, 0x01    \n\t"
                "sts 0x24, r16    \n\t");// DDRB = 000 0010)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Waiting for the original poster to come back and say something.

 

If my son is any indication, the assignment was probably due Sunday Night (and at 5pm+ they noticed that their code didn't work), and any help arriving after that is irrelevant.  Sigh.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The OP must be new to programming, he forgot to return   angel

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!