In: Computer Science
Write an AVR assembly language code to find the average of A to C variables, placing the result into variable F:
Provided variables A, B and C are holding an 8-bit unsigned number.
Assembly code:
.DSEG
A: .BYTE 1
B: .BYTE 1
C: .BYTE 1
F: .BYTE 1
.CSEG
Average:
lds r18, A
lds r19, B
lds r20, C
add r18, r19
lsr r18
add r19, r20
lsr r19
add r18, r19
lsr r18
sts F, r18
rjmp Average
Description:
.DSEG ; Data segment or Data space
A: .BYTE 1 ; Variable A
B: .BYTE 1 ; Variable B
C: .BYTE 1 ; Variable C
F: .BYTE 1 ; Result Variable F
.CSEG ; Code segment or Code space
Average: ; Procedure Average
lds r18, A ; Loads a single byte from the labeled data space A to r18 register.
lds r19, B ; Loads a single byte from the labeled data space B to r19 register.
lds r20, C ; Loads a single byte from the labeled data space C to r20 register.
add r18, r19 ; Addition of two register. r18 = r18 + r19
lsr r18 ; Logically shift right that divides by two.
add r19, r20 ; Addition of two register. r19 = r19 + r20
lsr r19 ; Logically shift right that divides by two.
add r18, r19 ; Addition of two register. r18 = r18 + r19
lsr r18 ; Logically shift right that divides by two.
sts F, r18 ; Stores a single byte from the r18 Register to the labeled data space F.
rjmp Average ; Relative jump to Average