learning assembly, unexpected behavior

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

hello, I'm trying to learn assembler and I'm using an Attiny 4313, using avr-as. I modified the .include file by replacing all the = with , and it compiles.
I wrote a simple hello world program that turns on a light and that worked, however looping doesn't seem to work:

.nolist
.include "tn2313def.h"
.list
rjmp init

init: 
	ser r16
	out DDRB, r16   ;DDRB = 0xff
	out DDRD, r16   ;DDRD = 0xff

       clr r16
       ldi r17, 0xfe
       out PORTD, r17
       rjmp rob

rob:
;r17 will be my portd counting reg.
        ;ror r16 
        inc r16 
	out PORTB, r16 
        dec r17
        out PORTD, r17
        dec r17
        out PORTD, r17
rjmp rob

I'm operating a matrix where the rows are + and hooked to PORTB and columbs are - and hooked to PORTD. so, this code in its first run of the loop, increments r16 to 1, so one row is active. I use r17 with PORTD and set it to 0xfe, so it grounds one column, once it enters the loop, i dec r17 again so its 1111 1101, then output to portd for some reason and dec again so that its 1111 1100, and that shows up as two lights on. But heres the thing, it doesnt jump back to rob + 1, otherwise, the rows would be changing, or all on bc of the inc r16 out PORTB, r16. I'm really new to all of this and it could be how I'm setting up the actual assembler file, perhaps it needs .global or the .cseg version for avr-as. it could even be my manipulated tn2313def.h being used on a 4313. however I'm pretty sure that its all code compatible. Any help would be much appreciated
-robby

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

Are you running this through a simulator? On the real hardware you would have no delays between changes so I suspect the LED's would all be on dimly or flicker.

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

Choose either avr-as (the GCC assembler) or avrasm2 (the Atmel assembler)

You should never alter a 'system' header file like "tn2313def.h". "tn2313def.h" is designed for avrasm2 not avr-as.

If you are using a tiny4313 then surely you should use "tn4313def.h"

David.

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

Yeah, I'm doing this all on the actual 4313, but that's what I'm sayin, If it was working correctly I would see two full columns in respect to PIND1:0. But right now, it's frozen on PORTB=0x01, PORTD=0xfc so there are two LEDs side by side on.

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

I created an Assembler project in AS6. This uses the Atmel avrasm2.

Comment out the .include because AS6 pre-includes the correct 4313 header file.

I simulated your program, and everything seems very normal.

What do you want to do?
Decrement r17 or rotate a pattern of bits ?

Oh. And my usual rant: "Use consistent indentation. Make the code attractive to read. Add adequate comments"

David.

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

Quote:
shows up as two lights on.
And would I be right in saying that the "lights" (LEDs??) don't have any resistors in series with them?

Can you show a diagram of your circuit?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Add this.

init:
	ldi	r16, RAMEND			;stack @ end of SRAM
	out	SPL, r16

RES

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

Quote:
Add this.
Even though it's good to initialise the stack, and I always do, modern AVRs come up with the correct stack pointers at power up.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

That plus his code calls no subroutines, so it doesn't use the stack. At least not yet.

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

The program does exactly what robwasab has asked it to do. The Simulator behaves normally.

Of course, robwasab may want it do do something completely different. How can we tell? There are no explanatory comments.

David.

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

Thanks for the comments, Ill respond tomorrow, I was busy all day and forgot to check my post. Yes, I put resistors on, like 20 ohms where Vcc is 3v to get 20 ma through the led, at it's forward voltage. Umm, what does it show on the simulator? All I was looking for was to see PORTB entirely lit up to the human eye. I'm pretty sure it's bc I'm mixing define files, I need to swap out that 4313 with a 2313 and get back to you guys.

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

I am not too sure what LEDs have a forward voltage of 2.6V @ 20mA. But yes, a 20R resistor should drop 400mV @ 20mA. An AVR should manage to drive 160mA with 8 LEDs lit.

Please note that most punters here would be using VCC of 5V. In which case, those 20R resistors will give you 120mA per LED. This is far too high a current for the AVR.

No. The Simulator tells you nothing about Ohm's Law. Mind you, I don't think that Proteus Simulation would tell you either.

Yes. All LEDs will appear lit. The loop operates so fast that your eyes will see all 256 patterns in quick succession.

What is PORTD supposed to do ?

Regarding your .include files. AS6 includes the MCU defines whether you like it or not.

David.

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

Just to note that if you really do want to use avr-as then replace:

.include "tn2313def.h"

with

#define __SFR_OFFSET 0
#include 

but note that by default the CRT will still be linked. Some think this is good (I do) as it provides you a vector table and a ,ot of startup code you'd write yourself anyway. The only requirement is that your own code starts with the label "main:" because that's what the CRT will call to enter your program just as it calls main() when C is used). If you don't want this but want a "clean machine" where your assembler code locates to 0x0000 and providing any vector table is your responsibility then use -nostartfiles or -nostdlib.

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

Quote:
hello, I'm trying to learn assembler

Atmel assembler is 'easier' to learn with.

GCC 'avr-as' is far more powerful, but more complex to learn.

IMHO, 10 - 100 lines of ASM is plenty enough for any human being. Either assembler is suitable.

If you want to write 100000 lines of code, you need to be well organised and use the 'best' tools. avr-as would definitely be preferred.

David.

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

Quote:

but more complex to learn

They both process the same opcodes (almost!) and nothing says you have to make use of every last directive available. So why is the Atmel assembler "easier"? Do you mean it has better documentation?

Personally I'd get some dialect of GNU as under my belt now so it will be applicable to all variants of as you may encounter in later life.

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

The main 'advantage' is that you can access PORTB etc without wrapping in macros. The documention is better too.

I agree that GNU-as is a useful experience.
Principally to ensure that any ASM is < 100 lines of code. Learning how to read and tweak ASM generated by a HLL is a valuable skill. Especially if it involves < 10 ASM instructions.

David.

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

Quote:

The main 'advantage' is that you can access PORTB etc without wrapping in macros.

As you can in avr-as (note my #define __SFR_OFFSET 0 above).

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

Quote:
what LEDs have a forward voltage of 2.6V @ 20mA.
Some Green ones or even Blue ones....or a Red one about to explode. :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly