Simple inline asembler question.

Hello all. New member here and totally new to Arduino. I hope this is the right forum to post a question about inline asm. Don't get me wrong, any "real" projects that I do will most likely use C/C++, but initially I'd like to just do a few simple things in asm as it's a good way to familiarize myself with the hardware.

Right now I'm just playing around with Arduino IDE software, looking through some of the inbuilt examples and reading some of the Amtel datasheets. I haven't even got the hardware yet, so just reading through the examples and trying to learn a bit in advance.

I was looking at the little LED blink example from the IDE, as it seems about the simplest program to test the hardware when it arrives. I had a go at writing some of it in assembler (yeah I know it's probably been done a million times before) just to test if it will compile. Unfortunately however, even with just the port manipulations translated to assembler it failed to compile with the following rather obscure error message.

collect2.exe: error: ld returned 5 exit status

Strangely, if I add any C code port access prior to the asm part of the main loop (such as the "fiik=digitalRead" line) then it compiles without error. But if I remove that line then it fails with the above message.

Appreciate any help, thanks. :slight_smile:

// the setup function runs once when you press reset or power the board
void setup() {
  //replace "pinMode(LED_BUILTIN, OUTPUT)";
  asm volatile (
    "sbi 0x04,05    \n"                // Set bit 5 of port B to output (DDRB = 0x24 mem / 0x04 IO)
  );  
}

// the loop function runs over and over again forever
void loop() {
  int fiik = digitalRead(2);          // // Does nothing (useful) but wont compile without it.
  asm volatile (
    "sbi 0x03,05    \n"                // Toogle B5 by writing 1 to PINB5 (PINB = 0x23 mem / 0x03 IO)
  );
  delay(1000);  // wait for a second
}

Append \t.

[quote author=Coding Badly link=msg=2982408 date=1477942529]
Append \t.[/quote]

Do you mean to replace each "\n" with "\n\t". Sorry, that didn't help.

BTW. What exactly does the \t do?

Which operating system are you using?

stuart0:
BTW. What exactly does the \t do?

Adds a tab to the next line. Appears to no longer be necessary.

pert:
Which operating system are you using?

Windows XP.

So this might be a bug in the IDE with XP?

I'll give it a try on my Windows7 laptop in a moment and report back.

stuart0:
Windows XP.

stuart0:
So this might be a bug in the IDE with XP?

Yep. pert nailed it. That particular error message is a give-away.

The problem is that later versions of "ld.exe" aren't fully compatible with Windows XP. (Your code, with the unnecessary 'digitalRead()' line removed, compiles and works fine for me using IDE V1.6.9 on Windows 10.)
It's one of those weird problems that comes and goes depending on your exact code.

You didn't mention which version of the IDE you're using, but I guess it's later than V1.0.6.
For me, when I was using Win XP SP3 and IDE V1.6.5, the fix was to replace "ld.exe" with a copy from IDE V1.0.6. (I downloaded the zipped version of that IDE and manually swapped the file. I renamed the original rather than deleting it, just in case.)

That fix doesn't work for everyone, however, so cross your fingers if you try it.

I'll give it a try on my Windows7 laptop in a moment and report back.

It should work fine on that machine. :slight_smile:

Ok it compiles correctly under Windows7 (without requiring the extra line of unnecessary code) . So the IDE must be buggy under XP. That's disappointing.

OldSteve:
You didn't mention which version of the IDE you're using, but I guess it's later than V1.0.6.
For me, when I was using Win XP SP3 and IDE V1.6.5, the fix was to replace "ld.exe" with a copy from IDE V1.0.6. (I downloaded the zipped version of that IDE and manually swapped the file. I renamed the original rather than deleting it, just in case.)

Thanks Steve. I'm just looking into replacing ld.exe now.

BTW. The IDE version is 1.6.12

stuart0:
Thanks Steve. I'm just looking into replacing ld.exe now.

BTW. The IDE version is 1.6.12

As I said, cross your fingers because that fix doesn't work for everyone. :slight_smile:
(The last person I suggested it to was also using IDE V1.6.12 and swapping "ld.exe" didn't help in that case.)

If you still have a problem, the most reliable way to fix it would be to install IDE V1.0.6 on the XP machine.
It's not technically a "bug", it's just that XP is a very old OS, no longer supported, and the Arduino IDE has moved on to better support the later operating systems.

OldSteve:
As I said, cross your fingers because that fix doesn't work for everyone. :slight_smile:
(The last person I suggested it to was also using IDE V1.6.12 and swapping "ld.exe" didn't help in that case.)

Yep, I just found that out. The old version ld wasn't compatible with some of the options 1.6.12 was giving it.

Failed with error message: "e:/arduino/arduino-1.6.12/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld.exe: unrecognized option '-plugin'"

If you still have a problem, the most reliable way to fix it would be to install IDE V1.0.6 on the XP machine.

Was going to be my next step. :slight_smile:

  asm (
    "sbi %0, %1 \n"
    : : "I" (_SFR_IO_ADDR(DDRB)), "I" (DDB5) 
  );

More here.