Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

problems

problems

2008-03-22 by steamphreaque

I am using AVRStuio, most recent release (with SP2).  I have a problem:

I have the following:

.cseg

LABEL:  .db 0x41, 0x42, 0x00
LABLE1: .db 0x43, 0x44, 0x45, 0x00   etc.

Everything assembles without error.  When I do the following:

    ldi     Z,      LABLE
    lds     R22,    Z+

   Instead of getting 0x41, I am getting 0xff, suggesting that I am
reading somewhere off in never-never land in the program memory.

   The Z register does hold the correct address for the string in
question.

   In looking through the .obj and .hex files, I note that the strings
I am defining (there of 350 bytes worth) do not exist in these files.

   What am I doing wrong, please?

avrFreak

Re: problems

2008-03-23 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "steamphreaque" <tjkeller@...> 
wrote:
>I have a problem:
You have two problems.  Firstly, the Z "register" is actually two 8-
bit registers and you have to load them separately.  Secondly, to 
read indirectly through the Z register, you need to use either ld or 
ldd.  (The lds instruction is for loading from an absolute address.)

ldi r30, lo8(LABEL)
ldi r31, hi8(LABEL)
ld r22, Z+

I have used a macro to simplify loading the Z register:

// macro to load a data address into r31:30
.macro	ldiZ	_data
  ldi  r30, lo8(\_data)
  ldi  r31, hi8(\_data)
.endm

Then, you can use it thusly:

ldiZ LABEL
ld r22, Z+

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Re: problems

2008-03-23 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, John Samperi <samperi@...> wrote:
> Also the address need to be multiplied by 2
The pm() operator is useful for that purpose:

ldi r30, lo8(pm(LABEL))
ldi r31, hi8(pm(LABEL))

Or, if you use the macro that I posted earlier:

ldiZ pm(LABEL)

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Re: [AVR-Chat] problems

2008-03-23 by John Samperi

At 11:59 AM 23/03/2008, you wrote:
>     lds     R22,    Z+
>......
>    What am I doing wrong, please?

Apart from what Don said, you need to use LPM to get
stuff from code space NOT LDS which is from ram.

Also the address need to be multiplied by 2 as data is sored
in bytes but the program space address is in words (16 bits)

So:
         ldi             zl,low(LABEL*2)
         ldi             zh,high(LABEL*2)
         lpm             temp,z+                 ;read char

And finally all data bytes that are not in even boundaries will
have a 0x00 added to it.

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Re: [AVR-Chat] Re: problems

2008-03-23 by John Samperi

At 02:44 PM 23/03/2008, you wrote:
>the pm() operator is useful for that purpose:
>
>ldi r30, lo8(pm(LABEL))

Not for the Atmel assembler :)
Looks like you are using GAS or other.

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Re: problems

2008-03-23 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, John Samperi <samperi@...> wrote:
> Looks like you are using GAS or other.
Yes, that's what I use.  The OP mentioned AVR Studio and I assumed 
(probably incorrectly) that he was using avr-gcc and avr-gas.  All of 
my comments were based on that assumption.

With regard to multiplying by two to get a word address, this is only 
needed if you are going to use the address to refer to code.  For 
example, if you want to use the ijmp or icall instructions you must 
have a word address in r31:30.  For use with LPM, you want a byte 
address in r31:30.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Re: [AVR-Chat] Re: problems

2008-03-23 by John Samperi

At 01:48 AM 24/03/2008, you wrote:
>Yes, that's what I use.

GAS gives me flatulence.....

>With regard to multiplying by two to get a word address, this is only
>needed if you are going to use the address to refer to code.

His DATA is in code space .cseg, so the address needs to
be multiplied by 2.

For OP:
.cseg

LABEL:  .db 0x41, 0x42, 0x00
LABLE1: .db 0x43, 0x44, 0x45, 0x00   etc.

    ldi     Z,      LABEL


Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.