Question #240463

Write an AVR assembly program for the ATmega328PB that uses a loop to sum the decimal numbers 1, 2, 3, ..., 19, 20, noting where the result is stored as well as its value in hexadecimal and decimal.

Expert's answer

    ORG OOOOh
    LJMP main
    ORG 0x40h
main:
    MOV R0,#14h      ; N=20 value to get Sum from 1 to 20
    MOV R1,#01h
loop:
    ADD A,R1
    INC R1
    DJNZ R0, loop
    MOV R4,A         ; Final Sum is stored in register R4
end
LATEST TUTORIALS
APPROVED BY CLIENTS