Arduino Assembly Guidance (Help)

Greetings people, I've been looking for information about arduino "pure" assembly (no inline) but I couldn't find anything...

The idea is to leave blank the .ino file from Arduino IDE, and use only the .S file.

The .ino has the #include "Assembly.S", the code will be in the .S file.

I have to do the blink for Arduino in pure Assembly, but I don't know how. Searching I found this code:

#include <avr/io.h>

.ORG 0x0000 // Tells the next instruction to be written
RJMP main // State that the program begins at the main label

main:
LDI r16, 0xFF // Load the immedate value 0xFF (all bits 1) into register 16
OUT DDRB, r16 // Set Data Direction Register B to output for all pins

loop:
SBI 5, 5 // Set the 5th bit in PortB. (i.e. turn on the LED)
RCALL delay_05
CBI 5, 5 // Clear the 5th bit in PortB. (i.e. turn off the LED)
RCALL delay_05
RJMP loop // Loop again

// Not sure what any of this does, it should be a delay loop though
delay_05:
LDI r16, 8

outer_loop:
LDI r24, low(3037)
LDI r25, high(3037)

delay_loop:
ADIW r24, 1
BRNE delay_loop
DEC r16
BRNE outer_loop
RET

When compiling, the lines
LDI r24, low(3037)
LDI r25, high(3037)
throws: Error: garbage at the end of line.

I haven't been able to fix it. However, when deleting those lines the .org throws an error saying something about a token.

It's incredible that any of the available codes out there works for me. This is for a University assignment.

I will appreciate any help.

There is only one good reason for using ASM - Your writing the compiler

There are two ifiy reasons for using ASM

  1. Your doing a course in ASM - Your so unlikely to need this skill that your wasting your time and money

  2. Your writing the code before the compiler has been written - Why for fucks sake?

Other than that using ASM (inline or other wise) is a very fucking stupid thing to do. The compiler will always do better than you can and it will do it a million times faster. It also makes 10 times as long to write ASM as it does to write C.

Trying to avoid using inline ASM in the way you are is just plane stupid. If it was required by your course then you have already been given the correct method.

The idea is to leave blank the .ino file from Arduino IDE, and use only the .S file.

No hope - any ASM you write must be executed within main() or the functions it calls. Unless it's in a class constructor.

Mark

Trust me, I think that way. However, I'm in an University course that requires this type of asm (no inline), and as I said before, the .ino will have the line to include the .S file, which will allow to upload it to the arduino (I suppose).

Any link to find the blink in asm?

Thanks!

The code you posted would not work if included in a normal Arduino sketch as it's designed to be the entire program/core in the AVR memory. When you add it as an include then your also getting the Arduino C++ core that sets up the pins/timers/etc prior to calling it's own main: procedure.

  LDI r24, low(3037)
  LDI r25, high(3037)

The LDI instruction only works with an immediate 8 bit value. low() and high() are helper functions that return the 8 bit low/high byte of a 16 bit value. I'm not to sure of ASM on AVR so don't know if the gcc ASM compiler has these built in or they are part of another compiler.

I'm in an University course that requires this type of asm (no inline)

In which case you would have been given the instructions.

Talk to your instructor you have miss understood something!

Mark

Hi,
Have you tried AVR Studio?

Tom..... :slight_smile:

TomGeorge:
Have you tried AVR Studio?

I can't use it. Using Arduino IDE is part of the assignment.

Hi,
Can you post the Assignment as it was presented to you please. So we can see the exact wording.
Picture of sheet will be okay.

Thanks.....Tom... :slight_smile:

kevin3377:
I can't use it. Using Arduino IDE is part of the assignment.

And your not allowed to use inline assembly?

TomGeorge:
Hi,
Can you post the Assignment as it was presented to you please. So we can see the exact wording.
Picture of sheet will be okay.

Thanks.....Tom... :slight_smile:

Well, my teacher didn't give us a sheet.

He just said that we have to make the Arduino LED blink in Assembly language (no inline, "pure" assembler), using the Arduino IDE and writting code in the .S file. Leaving the .ino blank (which I think it is impossible).

So far this code the I found is working fine:

.ino

#include "asmtest.h"

void setup(){
  asminit(0);  
}

void loop(){
  led(0);
  delay(1000);
  led(1);
  delay(1000);
}

.h

#ifdef __ASSEMBLER__
/*Assembler-only stuff*/
#else /* !ASSEMBLER */

#include <stdint.h>
extern "C" uint8_t led(uint8_t);
extern "C" uint8_t asminit(uint8_t);

#endif /*ASSEMBLER*/

.S

#include "avr/io.h"
#include "asmtest.h"
.global asminit
.global led

asminit:
  sbi _SFR_IO_ADDR(DDRB),5
  ret

led:
  cpi r24, 0x00
  breq turnoff
  sbi _SFR_IO_ADDR(PORTB),5
  ret

 turnoff:
  cbi _SFR_IO_ADDR(PORTB),5
  ret

But I don't understand what it does... It just works...

However, this is not how I'm suppose to do the assignment. As I said, I should use only the .S file.

Another problem I have is that when writting in the .S and compiling, the compiler shows: expected unqualified-id before '.' token

Help please :confused:
Thanks

Screenshot: (In this example, all files except .S are blank. The .ino has an unique line: #include "Blink_Assembly.S"

Riva:
And your not allowed to use inline assembly?

I'm not allowed. Only assembly like the code above.

outer_loop:
  LDI r24, lo8(3037)
  LDI r25, hi8(3037)

Fifteen seconds with Google produced the answer...
https://www.google.com/search?q=avr+assembly+lo+hi
http://www.avrfreaks.net/forum/how-get-high-byte-and-low-byte-gcc-assembler

The .ino has the #include "Assembly.S", the code will be in the .S file.

Don't do that. The dot-ino file should be empty.

Leaving the .ino blank (which I think it is impossible).

Extremely arrogant of you to contradict your teacher without first testing your theory.

holmes4:
The compiler will always do better than you can and it will do it a million times faster. It also makes 10 times as long to write ASM as it does to write C.

Obviously never worked much with assembler. :roll_eyes:

Sounds like Kevin hasn't quite worked out the assignment puzzle.

Please go back and clean up - mark up - the code in your first post.

It is not my intention to be arrogant. My ignorance makes me think that the .ino should have something, specially because when trying to compile the compiler says that it couldn't find the setup() or loop(), but when I write #include "Assembly.S" for example, it starts compiling the asm. By the way, the google thing, what did you search to find it? Maybe I miss it in the results.

On the other hand, any idea why the compiler does not compile the .global or .something?

Thank you very much.

By the way, the google thing, what did you search to find it?

Reply #10.

The first link is the Google search.
The second link is the first hit from that search.
The third link is the only link in that thread.

Alright, thanks.

I haven't found how to make .global work. This is what the compiler shows:

In file included from Blink_Assembly.ino:1:0:
Blink_Assembly.S:4: error: expected unqualified-id before '.' token
 .ORG  0x0000         // Tells the next instruction to be written
 ^
In file included from /home/kevin/Documentos/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/Arduino.h:23:0,
                 from Blink_Assembly.ino:15:
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:151:61: error: 'size_t' has not been declared
 extern void *bsearch(const void *__key, const void *__base, size_t __nmemb,
                                                             ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:152:8: error: 'size_t' has not been declared
        size_t __size, int (*__compar)(const void *, const void *));
        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:183:33: error: 'size_t' has not been declared
 extern void qsort(void *__base, size_t __nmemb, size_t __size,
                                 ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:183:49: error: 'size_t' has not been declared
 extern void qsort(void *__base, size_t __nmemb, size_t __size,
                                                 ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:298:21: error: 'size_t' was not declared in this scope
 extern void *malloc(size_t __size) __ATTR_MALLOC__;
                     ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:310:8: error: 'size_t' does not name a type
 extern size_t __malloc_margin;
        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:327:21: error: 'size_t' was not declared in this scope
 extern void *calloc(size_t __nele, size_t __size) __ATTR_MALLOC__;
                     ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:327:36: error: 'size_t' was not declared in this scope
 extern void *calloc(size_t __nele, size_t __size) __ATTR_MALLOC__;
                                    ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:327:51: error: expression list treated as compound expression in initializer [-fpermissive]
 extern void *calloc(size_t __nele, size_t __size) __ATTR_MALLOC__;
                                                   ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/stdlib.h:346:35: error: 'size_t' has not been declared
 extern void *realloc(void *__ptr, size_t __size) __ATTR_MALLOC__;
                                   ^
In file included from /home/kevin/Documentos/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/Arduino.h:25:0,
                 from Blink_Assembly.ino:15:
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:114:49: error: 'size_t' has not been declared
 extern void *memccpy(void *, const void *, int, size_t);
                                                 ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:115:40: error: 'size_t' has not been declared
 extern void *memchr(const void *, int, size_t) __ATTR_PURE__;
                                        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:116:47: error: 'size_t' has not been declared
 extern int memcmp(const void *, const void *, size_t) __ATTR_PURE__;
                                               ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:117:43: error: 'size_t' has not been declared
 extern void *memcpy(void *, const void *, size_t);
                                           ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:118:35: error: 'size_t' has not been declared
 extern void *memmem(const void *, size_t, const void *, size_t) __ATTR_PURE__;
                                   ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:118:57: error: 'size_t' has not been declared
 extern void *memmem(const void *, size_t, const void *, size_t) __ATTR_PURE__;
                                                         ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:119:44: error: 'size_t' has not been declared
 extern void *memmove(void *, const void *, size_t);
                                            ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:120:41: error: 'size_t' has not been declared
 extern void *memrchr(const void *, int, size_t) __ATTR_PURE__;
                                         ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:121:34: error: 'size_t' has not been declared
 extern void *memset(void *, int, size_t);
                                  ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:129:8: error: 'size_t' does not name a type
 extern size_t strcspn(const char *__s, const char *__reject) __ATTR_PURE__;
        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:131:8: error: 'size_t' does not name a type
 extern size_t strlcat(char *, const char *, size_t);
        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:132:8: error: 'size_t' does not name a type
 extern size_t strlcpy(char *, const char *, size_t);
        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:133:8: error: 'size_t' does not name a type
 extern size_t strlen(const char *) __ATTR_PURE__;
        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:135:44: error: 'size_t' has not been declared
 extern char *strncat(char *, const char *, size_t);
                                            ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:136:48: error: 'size_t' has not been declared
 extern int strncmp(const char *, const char *, size_t) __ATTR_PURE__;
                                                ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:137:44: error: 'size_t' has not been declared
 extern char *strncpy(char *, const char *, size_t);
                                            ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:138:52: error: 'size_t' has not been declared
 extern int strncasecmp(const char *, const char *, size_t) __ATTR_PURE__;
                                                    ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:139:8: error: 'size_t' does not name a type
 extern size_t strnlen(const char *, size_t) __ATTR_PURE__;
        ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/string.h:144:8: error: 'size_t' does not name a type
 extern size_t strspn(const char *__s, const char *__accept) __ATTR_PURE__;
        ^
In file included from /home/kevin/Documentos/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/Arduino.h:28:0,
                 from Blink_Assembly.ino:15:
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/avr/pgmspace.h:1137:55: error: 'size_t' has not been declared
 extern const void * memchr_P(const void *, int __val, size_t __len) __ATTR_CONST__;
                                                       ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/avr/pgmspace.h:1138:49: error: 'size_t' has not been declared
 extern int memcmp_P(const void *, const void *, size_t) __ATTR_PURE__;
                                                 ^
/home/kevin/Documentos/arduino-1.6.5-r5/hardware/tools/avr/avr/include/avr/pgmspace.h:1139:57: error: 'size_t' has not been declared
 extern void *memccpy_P(void *, const void *, int __val, size_t);
                                                         ^


More things like the others (Can't add it because of the 9000 charts limit)

expected unqualified-id before '.' token
In file included from Blink_Assembly.ino:1:0:

How can you be getting errors compiling the dot-ino file when it is empty?