/*
 * ParamPassing5.asm
 * Modified: 28 May 2026
 *   Authors:  Dr. Schwartz, Josh Weaver
 *   Purpose:  Demonstrate the use of parameters in constants
  */ 
.include "ATxmega128A1Udef.inc"
;******************************INITIALIZATIONS***************************************
.equ	stack_init = 0x3FFF ; Initialize stack pointer (between 0x2000 & 0x3FFF)	
                            ; Default is 0x3FFF
.equ    DATA1 = 0x37        ; Define parameter 1
.equ    DATA2 = 0xA3        ; Define parameter 2
; or you can do this!!!
.equ	AvgData = (DATA1+DATA2)/2

.org 0x0000
	jmp 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
		
    rcall AVG                   ; Call subroutine AVG to get the average
HERE:	
    rjmp HERE
;********************************SUBROUTINES*****************************************
/************************************************************************************
 * Name:     AVG
 * Purpose:  Get the Average of parameters given label DATA1 and DATA2
 * Inputs:   GLOBALS
 * Outputs:  r16 is the place for the result
 * Affected: r16, r17, r18, X, Stack
 ***********************************************************************************/
AVG:
    ldi r16, DATA1  ; Load global parameter 1 into r16
    ldi r17, DATA2  ; Load global parameter 2 into r17

    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