175k views
0 votes
(a) Write a code in AVR to load the value $15 into location $67 and add it to R19 seven times and place the results in R19 as added. R19 should be zero before the addition starts. (b) Repeat part (a) except place the result in location $70.

User Johnny V
by
8.5k points

1 Answer

5 votes

Here is the code for part (a) of the problem:

ldi R19, 0 ; set R19 to zero

ldi R20, $15 ; load $15 into R20

sts $67, R20 ; store $15 in memory location $67

add R19, R20 ; add $15 to R19

subi R24, 1 ; decrement counter

brne loop ; loop until counter is zero

And here is the code for part (b) of the problem:

ldi R19, 0 ; set R19 to zero

ldi R20, $15 ; load $15 into R20

sts $67, R20 ; store $15 in memory location $67

add R19, R20 ; add $15 to R19

subi R24, 1 ; decrement counter

brne loop ; loop until counter is zero

sts $70, R19 ; store result in memory location $70

Note that in both cases, we use a loop to add $15 to R19 seven times. The loop is controlled by a counter in R24, which is initialized to 7 and decremented by 1 in each iteration of the loop. The brne instruction branches to the loop label if the counter is not zero. Finally, in part (b), we store the result in memory location $70 using the sts instruction.

User Manita
by
8.7k points