Specific element in an array in Assembly on the AVR

1

In the following section of code:

    ldi     r16,255
    out     IO(DDRB),r16
    ldi     r30,1
    subi    r30,lo8(-(array))
    sbci    r31,hi8(-(array))
    ld      r24,Z
    out     IO(PORTB),r24

Because there is an - (array) within both lo8 and hi8? And why are the subi and subci instructions used here? How does the address for the 2nd element get this? I'm using avr-gcc.

    
asked by anonymous 26.08.2014 / 01:43

1 answer

1

The goal is to calculate array+1 . At first the value 1 is loaded in r30 . I imagine that some code before zeroes the r31 register because there is nothing else that does it. In sequence, add the address of the array (16 bits) in the pair of registers r30 and r31 . The problem is that the AVR assembly does not have a statement to add immediate . So instead of doing 1+array , it made 1-(-array) .

subi    r30,lo8(-(array))
sbci    r31,hi8(-(array))

The two subtraction statements capture the first and second byte of the denied array address and operate with the registers that now contain the number 1 . The second statement is sbci to carry the carry of the first.

    
20.09.2014 / 16:21