Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
zhaneforever
PostPosted: Jan 21, 2012 - 07:00 AM
Newbie


Joined: Oct 23, 2011
Posts: 2


I am a starter and I want to write a small AVR assembler program. Add 5 numbers defined in the memory and stored into r0 finally. My code can be assembled successfully. But there is a warning about "warning: .cseg .db misalignment - padding zero byte".

The problem is, r0 and r1 keep being 0 all the time when debugging step by step. I don't know why. Please help me and thanks a lot!

My code:

Code:

.NOLIST

.INCLUDE "m328Pdef.inc"

.LIST
; Code starts here

;Data starts here(On this processor this is always 0x0100)
.dseg

data:    .BYTE   5   ;Space for 5 bytes
sum:   .BYTE   1   ;Space for result

.cseg
   jmp main;
   
main:

var: .db 0x09,0x02,0x03,0x04,0x05
   LDI   r16,LOW(RAMEND) ;Initiate Stackpointer
      OUT   SPL,r16
      LDI   r16,HIGH(RAMEND)
      OUT   SPH,r16
   clr r0
   ldi   r16,5

   ldi   XH,high(var)
   ldi   XL,low(var)

loop:
   ld   r1,X+
   add   r0,r1
   dec   r16
   brne   loop

   st   X+,r0

   sleep
 
 View user's profile Send private message  
Reply with quote Back to top
zhaneforever
PostPosted: Jan 21, 2012 - 07:24 AM
Newbie


Joined: Oct 23, 2011
Posts: 2


By the way, I don't understand the so called "byte address" in the program segment. Anyone can help me understand this?
 
 View user's profile Send private message  
Reply with quote Back to top
js
PostPosted: Jan 21, 2012 - 07:31 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20387
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
But there is a warning about "warning: .cseg .db misalignment - padding zero byte".
The assembler is helping you out by putting a zero in the last loacation to keep the data on even boundaries. Remember that data is kept in 16 bit locations therefore 2 bytes per address is used.
Code:
   jmp main;
   
main:

var: .db 0x09,0x02,0x03,0x04,0x05
EXTREMELY BAD idea. The first thing the processor will do is trying to use your data as instructions with unkown results. Move you data at the end of the program after some terminating loop.
Code:

.
.
   st   X+,r0

   sleep

loop:
   rjmp loop

var: .db 0x09,0x02,0x03,0x04,0x05
And finally you CANNOT get data from the Flash as you are trying to do. Read up on the LPM instruction.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
JohanEkdahl
PostPosted: Jan 21, 2012 - 08:14 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18576
Location: Lund, Sweden

Sometimes the search function here at AVRfreaks fails, but it does an excellent job for e.g. "misalignment - padding zero byte". Don't forget to tick "Search for all words". It will give you excellent prior explanations for this message, e.g.: http://www.avrfreaks.net/index.php?name ... +zero+byte .

Also note that in that thread there is a link to a prior thread with more discussion.

I'm sure there are other valuable reads on this subject if you go through more of the search results.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Koshchi
PostPosted: Jan 22, 2012 - 04:55 AM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13847
Location: Vancouver, BC

Quote:
And finally you CANNOT get data from the Flash as you are trying to do.
To be more specific, you are putting "var" into flash, but trying to read it from SRAM.

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jan 22, 2012 - 10:38 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62341
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

To be more specific, you are putting "var" into flash, but trying to read it from SRAM.

To be even more specific - look up the LPM opcode in your manual.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits