Reading Data PORT A -- Data PORT Using the Z-index register (ASSEMBLER )


This is a question of understanding:

I need this “code / cludge” in a program examining some special registers in the ATMega 2560.

I do not have the correct I/O port addresses here, so I make some dummy ADDRESS assumptions.

When I in assembler want to read port A,B,C…L value I write:





Now I want to USE the Z-pointer

LDS R16,Z ; read PORT A Data


ADIW Z , 3
LDS R16,Z ; read PORTB data (assuming PORT B address is “3 greater” than PORTA ADDRESS)

And so on til

ADIW Z, (number to point to L-DATAPORT)

Is that possible ?

(and iF not, WHY not ?)

aka snestrup2016

I feel for you, I did assembly for over 40 years, and switching to any high level language drove me batty. Over time I learned to let the tools do what they do best and not try to make the decision for them. Do you really care what registers it uses, probably not. If you do then you need to do it in Assembler. As you get into the language there are a few things like upper, lower etc, It is hard but it works. If speed is a problem use a faster processor, they are cheap. If you dig into what is available you can actually read each individual port.

Thanks for the kind words.

My project is to control a Robot-arm. Using a 3 Axis Gyro.
(I have thought of a GSM- module, but as the Robot arm- base is fixed (only the Robot arm is movable), and I want to be able to (re)-position the head (grab ??) to any (pre) position I might want. I already have the Robot-arm. )

And to be honest. YES, I do care about the content of the registers (espicially the registers in the Analog Digital Control block), because I need to use these registers in a project , where in periods the ADC block will be heavily loaded. By examining these registers (and an careful analysis), I have a hope that the results will make me better in understanding what is actually happening in these registers (and as such a better programmer).

And YES, i know that these registers in a ATM2560 (as far as I remember) is only accessible by using the Z-index pointer,, but I have read ( somewhere ??? - "luckily forgot" (sigh)) that IT IS possible to read any IO-Port by using the Z-pointer. But I do not know why for the IO-port in the lower memory (IO) block.


My project is

I am in the same boat buy you have a step up on me, I know virtually nothing about the inner workings of the micro. I did that deliberately so I could learn the language. I am fairly sure the Arduino language supports inline assembler, try this link: Arduino Inline Assembly Tutorial #1 (Basic asm) | µC eXperiment All I know is that An inline assembly statement is processed as a string which specifies assembler code. The string can contain any instructions recognized by the assembler, including directives. GCC does not parse the assembler instructions and does not know what they mean or even whether they are valid. Multiple assembler instructions can be placed together in a single asm string. Hopefully the link will fill in the blanks.

Yes, it’s possible.
There is a big gap between registers A-G and registers H-K.
A-G are in low memory and will need to have 0x20 added to their symbol values. (PORTA is at 0x20 when you use LD, but at 0x0 when you use IN.)

You can use X or Y to hold the address as well. Use:

   LD R17, Z

For example, or

   LDD R17, Z+0

(The second D means add a displacement.)


Thanks for all the comments.

I have solved the problem. ( At last !!! )

I found the 2560 Datasheet on the WWW. Read carefully how to use the Z-index register (ZL and ZH - of course) and found that I can do this:

ldi r16,0xff
out ddra,r16 ; SET PORT A AS OUTPUT ( adr = 0x01

ldi r16,0xaa ; test pattern
out porta,r16

; here is the "exciting point

ldi r16,0xff
ldi zl,0x00 ; Z-reg points to PORTA

ldi zh,0x(20 + 1) ; port DDRA has a 0x20 offset when using the Z_index register
sts Z,r16

; this is a Z_index pointer routien initaizing port A (ddra) as output


lds r17,Z-1 ; Z = 20 - port PINA


ldi r17,Z+1 ; Z = 22 (portA OUT )

and so on for B,C... L but not i .

THansk for all the help given !

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.