How can I solve this error of a programming of ATMEGA 328P arduino micro-controller?

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

 

// shared global variables, visible to both C and assembly
word millisecs;      // C variable read by assembly code contains blink delay 
//
// setup function sets portB (digital pins 8-13) as outputs, and initialises serial port
//
void setup() 
{
  // set portB (digital pins 8-13) as outputs
  // achieved by writing to port B Data Direction Register (DDRB) at I/O address 4
  // if a bit is set, the corresponding pin is an output, otherwise it's an input
  // Bit designations for Data Direction Register B (DDRB) and Port B (PORTB)
  // bit5 = Pin13
  // bit4 = Pin12
  // bit3 = Pin11
  // bit2 = Pin10
  // bit1 = Pin9
  // bit0 = Pin8
  asm volatile(
    "          ldi r16,0x3F    ; r16 = 00111111\n"
    "          out 4,r16       ; set pins 8-13 as outputs in DDRB\n"
    ::: "r16");
  millisecs = 1000;            // 1s blink delay
  Serial.begin(9600);
}
//
// assembly language implementation of blink
//
void loop() 
{
  long starttime = millis();        // make a note of the start time

    asm volatile(
    // jump to "blink" - ie jump around the delay_ms subroutine
    "            rjmp  blink%=        ; relative jump to 'blink' \n"

    //-----------------------------------------------------------------------
    // Task 1 - complete delay_ms subroutine below until displayed error < 1%
    //-----------------------------------------------------------------------
    
    /* Input variable - millisecond count in register pair r30:r31
  
    registers used:
     r31 - millisecond count (lo byte)
     r30 - millisecond count (hi byte)
     r17 - 100 microsecond count
     r16 - 1 microsecond count
    
     Overall delay (ms) = r30:r31 * r17 * r16
    ---------------------------------------------------------------------*/
    "delay_ms%=:    "
             "load-constant r17,10  ;; delay ten times 100us        \n"
    "delay_100us%=: " 
             "load-constant r16, 100 ;; delay 100 times 1us        \n"
    "delay_1us%=:   "
    "             ;; some sequence of instructions that takes 1us \n"
    "             ;; including the looping below\n"
    "             decrement r16 ;; decrement us count \n"
    "             brnz delay1us ;; loop  \n"
    "             decrement r17 ;; decrement 100us count  \n"
    "             brnz delay100us  ;; loop  \n"
    "           sbiw r30,1          ; decrement ms count (r31:r30)\n"
    "           brne delay_ms%=     ; loop to delay_ms while > 0  \n"
    "           ret                 ; return from subroutine      \n"

    //-----------------------------------------------------------------------
    // Task 2 - modify blink code below to implement four phase traffic light
    //-----------------------------------------------------------------------
  
    " blink%=:                               ; start of blink code    \n"
    //
    // turn onboard LED on
    //   
    "               ldi  r18,0x20            ; bit 5 (pin 13) = high  \n"
    "               out  5,r18               ; output to port B       \n"
    //
    // delay by value in millisecs variable
    //
    "               lds  r30,millisecs      ; r30 = hi byte           \n"
    "               lds  r31,millisecs + 1  ; r31 = lo byte           \n"
    "               call delay_ms%=         ; call millisec delay sub \n"
    //
    // turn onboard LED off
    // 
    "               ldi  r18,0x00           ; value for all LEDs off  \n"
    "               out  5,r18              ; output to port B        \n"
    //
    // delay by value in millisecs variable
    //
    "               lds  r30,millisecs      ; r30 = hi byte           \n"
    "               lds  r31,millisecs + 1  ; r31 = lo byte           \n"
    "               call delay_ms%=         ; call millisec delay sub \n"

    ::: "r16", "r17", "r18", "r30", "r31");    // clobbered registers
  
  //--------------------------------------------------------------------------------
  // calculate the execution time of the blink routine, and print details 
  long endtime = millis();                  // make a note of the end time
  float ms = endtime-starttime;             // calculate the interval
  float expected = 2 * millisecs;           // expected delay is millisecs * 2 (2 delays in blink)
  float overheads = 17;                     // overheads due to the timing
  expected = expected + overheads;   
  float error_percent = 100.0*(ms-expected)/expected;
  Serial.print("delay="); Serial.print(ms); Serial.print("ms  "); 
  Serial.print("error: ");
  if(error_percent>0)
    Serial.print("+");
  Serial.print(error_percent);Serial.println("%");
}

 

I get this error when I run this 

C:\Users\nikhil\AppData\Local\Temp\cc80SMmF.s: Assembler messages:

C:\Users\nikhil\AppData\Local\Temp\cc80SMmF.s:1432: Error: unknown opcode `load'

C:\Users\nikhil\AppData\Local\Temp\cc80SMmF.s:1433: Error: unknown opcode `load'

C:\Users\nikhil\AppData\Local\Temp\cc80SMmF.s:1436: Error: unknown opcode `decrement'

C:\Users\nikhil\AppData\Local\Temp\cc80SMmF.s:1437: Error: unknown opcode `brnz'

C:\Users\nikhil\AppData\Local\Temp\cc80SMmF.s:1438: Error: unknown opcode `decrement'

C:\Users\nikhil\AppData\Local\Temp\cc80SMmF.s:1439: Error: unknown opcode `brnz'

lto-wrapper: C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-gcc returned 1 exit status

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

Last Edited: Wed. Jun 13, 2018 - 01:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Those aren't opcodes, they are macros so presumably there's some file you need to #include to add the definitions of the macros.

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

deepinside wrote:
Arduino assembly language instruction set - http://www.atmel.com/images/doc0... //

There is no such thing as "Arduino assembly language"

 

That's the AVR Instruction set; current link: http://ww1.microchip.com/downloads/en/devicedoc/atmel-0856-avr-instruction-set-manual.pdf

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: 0

What's the point of that inline asm anyway?

asm volatile(
    "          ldi r16,0x3F    ; r16 = 00111111\n"
    "          out 4,r16       ; set pins 8-13 as outputs in DDRB\n"
    ::: "r16");

"DDRB = 0x3f;" is not fancy enough?

 

And the "longer" part can also easily replaced by a few C lines.

Stefan Ernst

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

sternst wrote:
What's the point

It's a homework assignment - see header comment.

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: 0

awneil wrote:
It's a homework assignment - see header comment.
Ah, I missed that.

 

And I guess it would be too much work for the teacher to come up with more sensible examples of inline asm than "set a port register to a specific value" and "let a LED blink".

Stefan Ernst

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

first line of compiler horror massage is

  "delay_ms%=:    "
             "load-constant r17,10  ;; delay ten times 100us        \n"

 

? is load-constant an arduino asm opcode. Maybe, from the context, ldi would be nicer...

Then as asm is concise, decrement has some nice abreviation.

 

 

BTW : IIRC there is a difference between runnig and compiling .

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

dbrion0606 wrote:
? is load-constant an arduino asm opcode.
Nope, I already said it's clearly a macro. In fact rather curiously it would seem to be the exact equivalent of  "LDI" - presumably this is aimed at making Asm that might work on both AVR and ARM by giving common macro names to disparate opcodes but if that's the case how would ARM (or whatever) get on with things like "R18" and "R30" ??

 

I googled "load-constant" to see if I could find other usage but could not find anything so I think this is very localised (perhaps just this one school) rather than something being used all over the place in Arduino-land.

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

Well, if it is a a macro, why does OP sometimes use "ldi"?  OP Should use macro consistently, to be compatible with ARM.... or to be easier to read  or..... make a school specific assembly (c is said to be portable: perhaps schools hate portability)

 

Else, replacing by ldi would remove one error (if OP cannot find an extra file)

 

Last Edited: Wed. Jun 13, 2018 - 02:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

because the OP hasn't understood that it is a macro, and/or the reason for having the macro ... ?

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: 0

clawson wrote:
Nope, I already said it's clearly a macro. In fact rather curiously it would seem to be the exact equivalent of "LDI" - presumably this is aimed at making Asm that might work on both AVR and ARM by giving common macro names to disparate opcodes but if that's the case how would ARM (or whatever) get on with things like "R18" and "R30" ??
Other possibility:

That are not macros, but the teacher has given the delay routines as "pseudo-code" to the students, and it was their task to replace that pseudo-code with actual opcodes.

Stefan Ernst

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

It's a homework assignment - see header comment.

 

 

Your instructor is probably reading this post!!   coolangrysurprise

 

 

When in the dark remember-the future looks brighter than ever.

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

" but the teacher has given the delay routines as "pseudo-code" to the students"

 

Well, for pseudo-code, I would have used

simplified french

algol

pascal

arduino simplified c++ (seems ironical)

 

I would not have used macros with "-" minus sign inside ("_" do not hurt) .

Read #12

maybe OP should change his teacher (should have taught him/her how to read compiler horror massage : 1rst line is relevant and should be treated ... 1rst)

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

sternst wrote:
"pseudo-code" to the students
Interesting idea - sounds quite likely in fact.

 

I think we may need to hear from OP again about the history of this code and how it developed to the point we see now.