I'm trying to write a program in assembly to toggle the on-board LED of the Arduino MEGA2560.

To do so I just have to indicate the direction of the pin (13 in this case which is PB7) as output and then set that pin as High or Low.

I understand that there are two registers related with this problem. One is the port's direction register (DDRB) and the register for the state of the pins (PORTB.)

I'm trying to understand what number or address those registers have so I can assign values to them in assembly. I went into the C++ library in which those constants are defined (iomxx0.h) and for example DDRB is defined the following way:

#define DDRB    _SFR_IO8(0x04)
#define DDB7    7
#define DDB6    6
#define DDB5    5
#define DDB4    4
#define DDB3    3
#define DDB2    2
#define DDB1    1
#define DDB0    0

Is the register's address 0x04?

If that is the case, what is _SFR_IO8() doing?


It is a macro to access AVR IO address space registers, and in that address space, register 4 seems to be DDRB. You might want to actually use the datasheet as the register reference, instead of digging the info from avr-libc headers.

The macro _SFR_IO8() is actually a macro to call _MMIO_BYTE((io_addr) + __SFR_OFFSET), which in turn is a macro to just access the byte of memory pointed by the address like this: (*(volatile uint8_t *)(mem_addr))


You should look at the manual for the Atmega processor. All addresses of all registers and ports are in there. In the Arduino manual, you can find which register corresponds to which Arduino pin.

What you found is the header used to write the program in C. I have found that expression in "embedded controllers using C and Arduino", explained at page 95.

Counter question: How do you upload assembly code to the Arduino? Atmel AVR studio?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.