MIT-Curricular/ES/Lab/Lab2/swap/LOOP.asm
2025-08-28 10:11:40 +05:30

79 lines
No EOL
3.9 KiB
NASM

; ========================================================================================
; LOOP.asm - Efficient Array Copy Using Loop Construct
; ========================================================================================
; This program demonstrates an efficient way to copy data from one array to another
; using a loop construct. This approach is much more scalable and maintainable
; compared to individual copy operations, especially for larger arrays.
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 R2 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 approach is much more efficient than individual operations for large arrays
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
; R2 holds the number of elements to copy (10 elements total)
MOV R2,#10 ; R2 = 10 (number of elements to copy)
; Step 3: Main copy loop
BACK
; Load from SRC array and advance pointer
; LDR R3, [R0], #4 means: R3 = [R0], then R0 = R0 + 4
LDR R3,[R0],#4 ; Load next element from SRC and advance R0
; Store to DST array and advance pointer
; STR R3, [R1], #4 means: [R1] = R3, then R1 = R1 + 4
STR R3,[R1],#04 ; Store element to DST and advance R1
; Decrement loop counter and set condition flags
SUBS R2,#1 ; R2 = R2 - 1 (set flags for branch condition)
; Branch back to BACK if R2 is not zero
BNE BACK ; If R2 != 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 - initially contains space for one element, but will be expanded
; during copy operations. The loop will copy all 10 elements from SRC to DST
DST DCD 0
END ; End of the assembly program