Subroutine and extend precision write a program in assembly for PTC 18F452 that
ID: 3688679 • Letter: S
Question
Subroutine and extend precision write a program in assembly for PTC 18F452 that compotes the following math operation x = Sigma^n_i = 0 in other words x = 1+2+3+ +n for all n, 0 inequality N inequality 255 Implement this Using a Subrouting equivalent to the following C function declaration: in + mysum (unsigned char n)i write a test main program in PIC 18F452 assembly that invokes this subroutine. This will equivalent to the following C program frogment: in + main() in + x j Unsined char n = 50j X = mysum (n)jExplanation / Answer
#include <p18F8720.inc>
n equ D’50’
sum_hi set 0x01 ;high byte of sum
sum_lo set 0x00 ;low byte of sum
i set 0x02 ;loop index i
org 0x00 ;reset vector
goto start
org 0x08
retfie
org 0x18
retfie
start clrf sum_hi,A ; initialize sum to 0
clrf sum_lo,A ; “
clrf i,A ; initialize i to 0
incf i,F,A ; i starts from 1
sum_lp movlw n ; place n in WREG
cpfsgt i,A ; compare i with n and skip if i > n
bra add_lp ; perform addition when i 50
bra exit_sum ; it is done when i > 50
add_lp movf i,W,A ; place i in WREG
addwf sum_lo,F,A ; add i to sum_lo
movlw 0
addwfc sum_hi,F,A ; add carry to sum_hi
incf i,F,A ; increment loop index i by 1
bra sum_lp
exit_sum nop
end