Starting with AVR Assembly

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

Hi,

I really want to get started with AVR assembly but I need a mentor. I have some tutorials online but there are a few commands I don't quite understand. if someone would please help me out i would very much appreciate it, so PM me for my skype or MSN account.

i am a high-school student and got the AVRISP mkII with AVRStudio4 and WinAVR downloaded.

Thanks in advance!!!! :)

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

Quote:
I need a mentor
You can get lots of mentoring here but publicly.
Are you planning on using the Atmel assembler or the GCC assembler that comes with winAvr?

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I don't know, which do you think would be better for an assembler?

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

The Atmel assembler is "simpler". There is no magic mentoring, all you need to do is LOTS of practise just like when you want to learn any other language.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Are you proficient in assembler for any processor family? Is it just the "AVR" part that you need help with, or is it the whole "assembly language" thing?

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

It is the assembly language thing, since I don't know it but want to learn. I think I can understand the AVR part pretty well if i look at the datasheet of the AVR i am using.

i requested some samples from Atmel, hopefully I'll get them :)

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

Stert here. Follow this.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Wow thanks, this is very helpful!

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

Is this with Studio asm or WinAVR asm?

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

Studio. Yet the common recommendations/guidelines are essentially the same.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

This is definitely for fun, and i would really, really like to learn asm.

Thanks for all the help so far

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

Even if use C all the time to write code for avr it is useful to have an idea what is behind C commands and how these commands are translated into avr code.So it useful to learn assembly getting this way more familiar yourself to the architectural of the specific mcu's.
And its very useful in time critical applications also.
And as any other knowledge you get in life is something that remains and never get lost.

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

millwood wrote:

assembly is one of the hardest language to learn and one of the hardest language to use. It is incredibly to build up a usable assembly code base, and it is even harder to transport your assembly skills from one family of mcus to another.

Are you under drugs, man? :D:D:D

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

millwood wrote:
assembly is one of the hardest language to learn and one of the hardest language to use.

geez ... Alharad, if he's a newbee, will believe this if we let it go. Lying to him helps no-one at all.

Last Edited: Mon. Jan 3, 2011 - 02:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe assembly is the easiest thing to learn on this planet,maybe not.But modifications,and transportations to another device of the ready code are very difficult.
First choose the appropriate device that meets application specifications and then start writing any code.
If you are studying medicine it is not neccessary to learn assembly.In learning of any mcu i think its neccessary.

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

Make sure you get the AVR assembler and instruction set manuals. To get the most out of this forum, you'll need to learn C eventually because most of what's here is written in C.

I recommend the tutorial forum, esp. timers / PWM, and how to use avr studio tuts. What are the instructions you don't understand ? What MCU do you have ?

https://www.avrfreaks.net/index.p...
http://www.avrbeginners.net/

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

I was gonna leave this alone BUUUUUUUUUUUUTTTT!!!!

I can read and somewhat understand 'C', but to write a program I am a dead duck. I have used assembler for close to 20 years, and I agree with some of the posts that If you are getting started 'C' is probably the best course to possibly follow(F#$%^, I cannot believe I said that, sorry js)

None the less it is very difficult to remember the commands for various processors where 'C' can be debugged after compiliation from one platform to another without much asprin to kill the headache.

This community has many phenominal gurus on both assember and "c" Use them.

WElcome, and Good luck to you.

Jim

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

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

Quote:
F#$%^, I cannot believe I said that,
I hope you didn't write this in front of junior!! Go wash your mouth with soap now....

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

start from avr tutorial..expecially moron tutorial.

Regards
Jeckson

www.tokopedia.com/madagang .Buy and Donated cheap electronics and manuscripts.

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

avrbeginners.net is another good assembly language resource.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

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

http://www.avr-asm-tutorial.net/avr_en/index.html
The site is still maintained, every now and then there is something "new on this page". It tells you everything about how to get things like a keypad or HD44780 character display working.
Mister Gerhard Schmidt is a True Assembly Programmer who will never convert to C.
And get a modern USB connected STK200 boeard from Kanda.com. Many people here may want to give away an old STK200.

Many example asm programs are here: http://avr-asm.tripod.com/

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

I've been looking through the AVR tutorials, and it doesn't seem so bad. The best way for me to become good at anything is just practice, which I've noticed while learning C. I appreciate all the help so far, you've made it easier for me to learn assembly. I will let you know if i have any other questions or get stuck, and i hope you will all be just as helpful! :)

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

Best way to learn C, I feel, is to write some dumb console programs in C (not C++) using free C compiler for MS WIndows, such as Visual Studio Express or there are several other freebies.
Then you can debug, catch bad pointers, etc. On a micro, it just inexplicably fails to blink the LED. Unless you sprinkle lots of printf() or have a JTAG debugger.

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

Yea, it's really good to get practice in! Theory can only take you so far, and then you have to get down and do it. Plus you remember more from experience!!

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

I have one question now. The Data Direction Register sets your I/O ports as inputs or outputs. The PORT register sets whatever port you have as a high or low. But I'm wondering if I have this right

START:
	LDI LED, LOW(RAMEND)	; Setup Stack Pointer
	OUT SPL, LED	; So calls to subroutines
	LDI LED, HIGH(RAMEND) 	; Work correctly
	OUT SPH, LED
	LDI LED, 0xFF		; Set all PORTB for output
	OUT DDRB, LED		; Write 1's to Direct in Regs

so to set outputs with DDRB i have to set it has 1's, right? so if i wanted the first bit to be an input for a switch maybe it would be:

LDI LED, 0xFE
OUT DDRB, LED

is that right?

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

Quote:
is that right?
Yes, however it's nicer if you use a register named something other than LED, temp is usually used for general purpose register. I reserve R24 and R25 for tmp and tmp1.

Also you can use bit NAMES for the bits you want set as one. ie

#define	SCK PB5						;SCK pin
#define	Miso pb4					;Miso pin not used
#define	Mosi pb3					;Master data out
#define	latch_enable pb2			;Latch enable bit (SPI /SS pin)
#define	PWM PB1						;PWM brightness pin
#define	input PB0					;External input
.
.
.

	ldi		temp,(1<<Miso | 1<<input | 1<<latch_enable | 1<<PWM) ;Enable pull ups 
	out		portb,temp							                 ;set /SS (latch_enable) and PWM high
	ldi		temp,(1<<SCK | 1<<Mosi | 1<<latch_enable | 1<<PWM) ;PORTB B0=input and B4=Miso as inputs
	out		DDRB,temp

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Here is my first blinking LED program, does it look ok?

.INCLUDE "M8DEF.INC"

; Define Registers

.DEF TEMP = R16	; LED I/O Port
.DEF I = R21	; Index for Loop Control
.DEF J = R22

.ORG 0

START:
	LDI TEMP, LOW(RAMEND)	; Setup Stack Pointer
	OUT SPL, TEMP	; So calls to subroutines
	LDI TEMP, HIGH(RAMEND) 	; Work correctly
	OUT SPH, TEMP
	LDI TEMP, 0xFF		; Set all PORTB for output
	OUT DDRB, TEMP		; Write 1's to Direct in Regs

; Main Routine

BLINK:
	SER TEMP		; Turn LED ON
	OUT PORTB, TEMP
		RCALL PAUSE		; Delay Subroutine
	CLR TEMP		; Turn LED OFF
	OUT PORTB, TEMP
		RCALL PAUSE		; Delay Subroutine
	RJMP BLINK		; Loop back to main routine

; Subroutines
PAUSE:
	CLR J
PLUPE:
	NOP
	DEC J
	BRNE PLUPE
	RET

I'm not very sure why i need to use a I/O register in order to loop? can't I use a variable like in C?

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

Quote:
I'm not very sure why i need to use a I/O register in order to loop?
You're not using an I/O register, you're using a GP register, which is exactly where C would put the variable (though it might have a master copy of the variable in SRAM).

Regards,
Steve A.

The Board helps those that help themselves.

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

Could you explain a GP variable? What does it stand for and what does it do?

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

Newer models can toggle pin state using PINx istead of PORTx.
So this code (which changes all pins of PORTB)

BLINK:
   SER TEMP      ; Turn LED ON
   OUT PORTB, TEMP
      RCALL PAUSE      ; Delay Subroutine
   CLR TEMP      ; Turn LED OFF
   OUT PORTB, TEMP
      RCALL PAUSE      ; Delay Subroutine
   RJMP BLINK      ; Loop back to main routine

could be replaced by

BLINK:
   sbi pinB, LED  ;toggle LED
      RCALL PAUSE      ; Delay Subroutine
   RJMP BLINK      ; Loop back to main routine

This toggles only the pin where LED is connected. You need to define that pin such as
.def LED = 2 or whatever pin you're using.

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

Lennart wrote:
You need to define that pin such as
.def LED = 2 or whatever pin you're using.

Bullshit. The .def directive deals with registers, not with bits/pins.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Quote:
Bullshit. The .def directive deals with registers, not with bits/pins.
True, my bad.
And since your normal helpful attitude do not give the correct way to do it.
.equ   LED = 2
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Could you explain a GP variable?

No, but a "GP register" is a "General Purpose register" i.e. one of the registers R0..R31.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:
.def directive deals with registers
However #define works for all with ASM2.
.
.
.
#define	days_counter r6				;Calculated number of days to target date
#define scroll_n_bits r5			;Number of bits to rotate dis buffer for scroll
#define hundreds r4					;Hundreds for HTOD8 and DTOH8
#define	tens r3						;Tens for HTOD8 and DTOH8
#define	units r2					;Units for HTOD8 and DTOH8
//#define	 r1						;Leave unused for MUL
//#define	 r0						;Leave unused for MUL


#define	monitor_timeout	4000		;In 1ms counts (4000 4s)

#define	input_buffer_size 16		;16 bytes monitor input buffer size
#define	PKIO_DATA_SIZE 100			;256 max. ($FF)
#define	display_size 5				;Size of display 5, 10, 15, 20 or 25
.
.
#define	PWM PB1						;PWM brightness pin
#define	input PB0					;External input

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
However #define works for all with ASM2

Oh, really? :D:D:D

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Then please come back when your 2nd week of programming class is complete :D:D:D

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Quote:
Unless the assembler compiler is just written by some bonehead,
Why the people at Atmel, Microchip, Motorola, TI etc will be very upset at being called boneheads.

	
	LIST  P=16f877 
        #INCLUDE "P16f877.INC"
.
.
;   Built with IAR Embedded Workbench Version: 3.21A
;*****************************************************************************
#include  
.
.
$INCLUDE "\EVM11\DAT2\68HC11EX.ASM"

It is possible however, because assembler is such an advanced language, that the professsor doesn't cover these things untill much later in the programming course.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

@js

Quote:
I hope you didn't write this in front of junior!! Go wash your mouth with soap now....

Nahh, he was sleeping.

I guess you have truly went to the dark side or became a weenie since you became a moderator. :D

I can remember you defending assembler over 'C' vehemently. Oh well. I guess I am the last hope for the Jedi(lol)

Jim
Edit:
Cannot spell 'Truly" either

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

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

Quote:
I guess you have truly went to the dark side
..think of double agent... :evil:

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Never fear, there are many of us that still prefer assembly.

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

Quote:
Never fear, there are many of us that still prefer assembly.

Bless you

Jim

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

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

dksmall wrote:
Never fear, there are many of us that still prefer assembly.
FWIW (not much in the Grand Scheme) I've converted myself to C (CVAVR) from years of AVR assembly over the last 3 months.

After a few initial mindset and pointer/address hurdles I really can't see myself ever going back. I've converted a large handcrafted ASM app to CVAVR, expanded it to include floating point maths and floating point printf and it's about around 5% bigger (code size) than the the original.

I don't regret the assembly journey one bit - down and dirty with the chip and ASM is a great way to understand exactly how things work. It also allows you to look at, and understand, the compiler .lst file output to see how your C code is being to turned into ASM if you're unsure about anything.

I did dally with GCC in the past, but it just seemed to alienate me with its complexity when doing some things. I've found CVAVR very simple to grasp...

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

.DEF TEMP = R16   ; LED I/O Port 
.DEF I = R21   ; Index for Loop Control 
.DEF J = R22 

Just a small suggestion, is that you use more descriptive names on your Registers. Just like variables in other languages, a more descriptive name really really helps with assembly.

One thing to remember when programming asm is that you will tend to reuse a few registers for "general" use. Like a "temp" register that is never used for anything but throwaway values. Also, don't forget that when using the stack, it isn't the monster size that is available on other platforms. Recursion can be death on an AVR.

As was stated before R0-R31 are called the General Purpose Registers. The reason for this as they can be used for a multitude of things.

The Registers from 16-25 are the ones that most of the Opcodes work with. Some, (MUL) put their results in R0:R1. R26-R31 are your X,Y,Z registers, and if you use them for other things bad bad stuff can happen.

As with any other languages there are always many different ways to accomplish the same thing, however with a low level language such as this you are able to be very efficient and also screw up massively from a single boo-boo.

(I have to stop tying, the editor here is driving me nuts)

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

Quote:
I have to stop tying, the editor here is driving me nuts

I usually tye the long ones in notepad or wordpad, then paste into the edit box when I'm done tweaking it. It helps when looking for tyos, too. :)

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org