/* 
 * ParamPassing1a.asm
 *   Modified: 6 Feb 2024
 *   Authors:  Dr. Schwartz, Josh Weaver
 *   Purpose: Demonstrate passing of parameter data to a subroutine using registers
 */ 
.include "ATxmega128A1Udef.inc"
;******************************INITIALIZATIONS***************************************
.equ	stack_init = 0x3FFF     ; Initialize stack pointer (between 0x2000 & 0x3FFF)	
                                ; Default is 0x3FFF
.org 0x0000
    rjmp MAIN
;*******************************PROGRAM START****************************************
.org 0x200
MAIN:
    ldi r16, low(stack_init)
    out CPU_SPL, r16	        ; Initialize low byte of stack pointer
    ldi r16, high(stack_init)
    out CPU_SPH, r16	        ; Initialize high byte of stack pointer
		
    ldi r16, 0x37           ; Load Data into r16 and
    ldi r17, 0xA3           ; r17
    
    rcall AVG               ; Call subroutine AVG to get the average
		
HERE:	
    rjmp HERE
;********************************SUBROUTINES*****************************************
/************************************************************************************
 * Name:     AVG
 * Purpose:  Get the Average of r16 & r17, Output back to r16
 * Inputs:   r16 is the first number
             r17 is the second number
 * Outputs:  r16 is the place for the result
 * Affected: r16
 ***********************************************************************************/
AVG:
    add r16, r17    ; r16 = r16 + r17
    asr r16         ; Shift A right by 1 bit (divide by 2) keeping bit 7
                    ; (for sign extension) 
    ret             ; Return from subroutine