/* * VectorAdd_ASM.s * * Created: 6/18/2013 12:49:46 PM * Author: Josh Weaver, Dr. Schwartz * 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 /*********************************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 .global __do_copy_data // Copy a defined variable in program memory to data memory // Program Flash Data Section (in Code Memory Space) .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 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