; ======================================================================================== ; LOOP2.asm - Array Copy Using Loop with Proper Destination Array Size ; ======================================================================================== ; This program demonstrates the same array copy algorithm as LOOP.asm but with ; two key improvements: ; 1. Uses R12 as the loop counter (R12 is typically used for intra-procedure calls) ; 2. Pre-allocates the correct size for the destination array (10 elements) ; This version is more complete and avoids potential memory issues. AREA RESET, DATA, READONLY ; Define a read-only data section for the vector table EXPORT __Vectors ; Export the vector table for external linking __Vectors ; Start of the vector table DCD 0x10001000 ; Stack pointer initial value (points to top of stack) DCD Reset_Handler ; Address of the reset handler (program entry point) ALIGN ; Ensure proper alignment for the next section AREA mycode,CODE,READONLY ; Define the code section as read-only ENTRY ; Mark the entry point of the program EXPORT Reset_Handler ; Export the reset handler function ; ======================================================================================== ; Reset_Handler - Main program execution ; ======================================================================================== ; Algorithm Overview: ; 1. Initialize array pointers (R0 = SRC address, R1 = DST address) ; 2. Set loop counter R12 to the number of elements to copy (10) ; 3. In each iteration of the loop: ; a. Load element from SRC array using post-increment addressing ; b. Store element to DST array using post-increment addressing ; c. Decrement counter and check if more iterations are needed ; 4. This version properly allocates destination array space beforehand Reset_Handler ; Step 1: Initialize array pointers ; R0 points to the beginning of the source array LDR R0, =SRC ; R0 = address of first element in SRC array ; R1 points to the beginning of the destination array LDR R1, =DST ; R1 = address of first element in DST array ; Step 2: Initialize loop counter ; R12 holds the number of elements to copy (10 elements total) ; Using R12 is a common convention for loop counters in ARM assembly MOV R12, #10 ; R12 = 10 (number of elements to copy) ; Step 3: Main copy loop Loop ; Load from SRC array and advance pointer ; LDR R2, [R0], #4 means: R2 = [R0], then R0 = R0 + 4 LDR R2, [R0], #4 ; Load next element from SRC and advance R0 ; Store to DST array and advance pointer ; STR R2, [R1], #4 means: [R1] = R2, then R1 = R1 + 4 STR R2, [R1], #4 ; Store element to DST and advance R1 ; Decrement loop counter and set condition flags ; SUBS R12, R12, #1 is equivalent to SUBS R12, #1 SUBS R12, R12, #1 ; R12 = R12 - 1 (set flags for branch condition) ; Branch back to Loop if R12 is not zero BNE Loop ; If R12 != 0, continue loop ; Step 4: Program termination ; Create an infinite loop to stop program execution STOP B STOP ; Branch to STOP label (infinite loop) ALIGN ; Ensure proper alignment for data section ; ======================================================================================== ; Data Section - Source and Destination Arrays ; ======================================================================================== ; SRC array contains 10 elements (40 bytes total): ; Each element is a 32-bit word in hexadecimal format SRC DCD 0x00000032, 0x12345644, 0x00000005, 0x00000098, 0x000000AB, 0x000000CD, 0x00000055, 0x00000032, 0x000000CA, 0x00000045 AREA mydate, DATA, READWRITE ; Define a read-write data section ; DST array - properly pre-allocated with 10 elements (all initialized to 0) ; This ensures there is enough space for all copied elements DST DCD 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 END ; End of the assembly program