how to write digitalRead function in assembly

Hi dude,
I'm trying about fast read/write IO pins. The following code takes about 9 usec in Arduino board running 16MHz with IDE v0022.

uint8_t value = digitalRead(pinButton);   // read the input pinButton

So I want to try on assembly code as well but not must knowledge in assembly language. :sweat_smile: Can anyone help me to write digitalRead function in assembly?

Thank in advance,
pak

You don't need assembler, look at using port manipulation.

Thank for reply, I just want to try to put some assembly code in C and test how efficient and fast they are.

regards,
pak

Unless you write them really badly, they'll be as fast and efficient as the C code.

Unless you write them really badly, they'll be as fast and efficient as the C code.

Well put.

Duane B

x = PORTB & 4;

The yellow part will probably compile to the same couple of instructions you would use in assembler. ASM will be faster if you leave the value in a register but if you store it in RAM it will be the same.

Even that may be the same if the compiler decides to keep x in a register but I don't know how it makes that decision.

As to what those instructions are I would have to look that up as I haven't written any ASM for quite some time, the instruction set is at the end of the data sheet but it might something like this (this is also how you include ASM in C)

asm ("CLR   r20");
asm ("SBIS  PORTB,2);
asm ("LDI   r20,1");

You can also combine the lines into a single asm() if you add \n after every one.


Rob

Hi Rob,
I have some error message after compiling the code below:

asm ("CLR   r20");
  asm ("SBIS  PORTB,2");
  asm ("LDI   r20,1");

The error messages are:

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccFuIy36.s: Assembler messages:
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/ccFuIy36.s:61: Error: constant value required

Is there any example or tutorial about putting in-line assembly code in C?

Regards,
pak

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

Sorry I thought I'd made it clear that wasn't kosher code.

It's probably complaining about PORTB, try the value 5.

That said it's useless code because what are you going to do with r21, and in fact it may trash something C is using.


Rob

Cut a long story short its generally not worth writing assembler (gross generalisation i know), but as a first step to realizing this, have a look at the assembler output of your Arduino code using the objdump tool.

You might need to google for instructions, I did and here is a sample related thread -

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241117308/0#0

Most of the generated assembly code is reading variables from memory into registers, doing something with the variable and writing it back again - it isn't all that exciting and the compiler is generally pretty good at making optimisation decisions.

If you get familiar with objdump and the code that the compiler outputs, you can make a better judgment of whether you time is well spend trying to beat the compiler at its own game - or not.

Duane B

rcarduino.blogspot.com

Cut a long story short its generally not worth writing assembler (gross generalisation i know), but as a first step to realizing this, have a look at the assembler output of your Arduino code using the objdump tool.

Reading pin D11 (or PORTB3) of arduino

unsigned char dIn;
asm volatile("in %0, %1" : "=r" (dIn) : "I" (_SFR_IO_ADDR(PORTB)), "I" (3) );

but no luck,

Thank for advice,
pak