Invalid register

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

I did a code to buzz a buzzer whenever the water flow sensor rpm is above average. I had no idea why it has error in line 20

 

initialize:
        ldi r16, 0xff
        ldi r17, 0b0000000
        out ddrb, r16 
        out ddrd, r17 
        cbi portb, 1 
        out portc, r17 
        out portd, r17 
        clr r17
        clr r16
Main:        
        clr r16
        in r16, portc
        cpi r16, 0b00000001 
        BREQ sound 
        clr r16
        nop 
        rjmp main
sound:    
        out portd, 0b00000100
        ret

EDIT: Code window - JGM

ok

Last Edited: Wed. Jan 8, 2020 - 03:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

POst the error you are getting from the assembler  Also, what AVR are you using?

 

Jim

 

EDIT:

Post ALL the code.  What you have there is not even 20 lines worth.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Wed. Jan 8, 2020 - 03:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Shahrul Rafi wrote:
line 20

Go on - you could point out which line that is, rather than leaving people to count it!

 

And Remember that we don't necessarily know which line you're counting as 'one' ...

 

Anyhow, have you looked at the Instruction Set document, to check what values are valid? Does your code comply with that?

 

 

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

I'm guessing the assembler does not accept register numbers in binary.

Does it accept other numbers in binary?

Iluvatar is the better part of Valar.

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

When I try building that the error is on:

        out portd, 0b00000100

@OP remember this is RISC micro - that means that each instruction can only do a small part of a job. If you want to do this you need the 0b00000100 LDI'd into a register first then OUT that register to the PORTD location. Something like:

ldi r20, 0b00000100
out portd, r20

(though you might want to assign more meaningful name to the r16, r17 and now r20 (or whatever) that you choose to use).

 

With the suggested change I now get:

Build succeeded.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

 

Last Edited: Wed. Jan 8, 2020 - 03:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
What you have there is not even 20 lines worth.

I make it 21 lines total ?

 

But anyhow,  we don't know where "line 1" is - so "line 20" is not useful.

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


Umm, gents...

 

 

so the error is indeed on line 20 as posted.

 

(I just did a copy/paste of the code in #1 when I tested this).

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

Do the labels need to match in case?   Main:  vs   rjmp main ???

also sound: is a subroutine (has a return)

but does the BREQ sound,  push the return address on the stack???

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Wed. Jan 8, 2020 - 05:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I showed it seems to build successfully so the implication would seem to be that it is case insensitive. Indeed it kind of needs to be for "portd" etc to resolve to "PORTD"

 

I do wonder about the "BREQ sound" and then "sound: ... RET" thing though!! I'm kind of guessing BREQ might have really been intended as a BRNE/RCALL combination or something??

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

You 'OUT' a constant value, instead of a register to a port...
That's why it gives you an error.

First load the value to a register and then output to port

ldi r16, 0b00000100
out portd, r16

 

EDIT: I just saw that already an answer exist above.
So to add something new, since you 'breq' you must return with 'rjmp'
(to whatever line you wish - i suppose somewhere after what's happended

if the equation is not valid), not with 'ret' becasue the MCU will get from stack
the 'return address' of your (supposed) rcall, so probably nothing good will happen like that...

Last Edited: Wed. Jan 8, 2020 - 06:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I also said that in #9 too ;-)

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

Ntano wrote:
you must return with 'rjmp'
(to whatever line you wish

 

Please don't even mention it. That's spaghetti code.

 

        BREQ sound 

 

 

Since there is no RCALLEQ instruction, it's better to check for the reverse condition and skip over an unconditional jump:

 

    BRNE    continue
    RCALL   sound
continue:
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are right, already mentioned at #9

Last Edited: Thu. Jan 9, 2020 - 11:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The GNU AVR assembler accepts decimal integers and some expressions as register names.

Had this assembler (GNU?) accepted binary integers, OP's code would have assembled without a diagnostic.

I'm somewhat surprised it didn't.

Iluvatar is the better part of Valar.