/* * VectorAdd_ASM.s * * Modified: 5 June 2014 * Author: Dr. Schwartz, Josh Weaver * Purpose: This file is a secondary .s file within the project. * * Both the initial setup asm function as well as the asm version of VADD * are located in this file. * Outputs: X = address of resulting vector sum */ #include // The io.h file (in the dependencies) includes the below two lines: // #elif defined (__AVR_ATxmega128A1U__) // # include // The path for the equivalent file to the ATxmega128A1Udef.inc is: // C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.2.1002\ // avr8-gnu-toolchain\avr\include\avr\iox128a1u.h /*********************************INITIALIZATIONS***********************************/ .EQU N, 6 // Table size of vectors #define CTR R19 // Define R19 to hold the counter while running // SRAM Data Segment .section .data // This will start at address 0x2000 as default text: .asciz "hello world" // This is only here for Demo Purposes VC: .ds.b N // Define storage of bytes N long xx: .byte 0xAB // Just for demo .global __do_copy_data // Copy a defined variable in program memory to data memory // Program Flash Data Section (in Code Memory Space) // Note that the SRAM Data will take some space in the data area. Look for it near // 0x027A. The VA table appears at 0x0236 and the VB table immediately following at // 0x023C. See Memory prog APP_SECTION, Address: 0x0200 (and other addresses above) .section .text VA: .byte 1,2,3,4,5,6 VB: .byte 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0 /*******************************PRIMARY CODE****************************************/ .global MAIN_ASM // The assembly function must be declared as global MAIN_ASM: ldi R18, N // Load the number of values ldi ZL, lo8(VA) // Load the address of program memory for VA ldi ZH, hi8(VA) ldi XL, lo8(VC) // Load the address of data memory storage for VC result ldi XH, hi8(VC) call VADD // Call the ASM function for vector addition ret // Return to call from C code /************************************************************************************ * Name: VADD * Purpose: Add two contiguous N-element vectors (VA and VB) to form the resulting * vector. VC = VA + VB * Inputs: Z = address of the first vector in ROM (VA) * R18 (=N), the number of elements * Z+N is address of the second vector (VB) * Output: X = adddress of resulting vector sum in (VC) * R18=0 if succesful, else non-zero * Affected: CTR (=R19), R20, R21 ***********************************************************************************/ VADD: push R18 // Push the number of values onto the stack for later use // movw R28,R26 // movw Y, X (Y,X won't work in C; see constraints) movw YL,XL // movw Y, X (Y,X won't work in C; see constraints) mov CTR, R18 // Copy count of values in vector to counter register // Work with first vector LOOP: lpm R21, Z+ // Load values of first vector pointed to by Z st Y+, R21 // Store currently loaded value into data memory pointed by Y sts PORTH_OUT, R21 dec CTR // Decrement counter brne LOOP // Loop until counter = 0 pop CTR // Restore the count of values in vector to counter register // Add first vector to second vector AGAIN: lpm R20, Z+ // Load value of second vector pointed to by Z ld R21, X // Load value of first vector already stored and pointed by X add R20, R21 // Add two values together st X+, R20 // and store back at data memory pointed by X dec CTR // Decrement counter brne AGAIN // Loop until counter = 0 clr R18 // Clear register initially holding count value (not needed). ret