85 lines
		
	
	
		
			No EOL
		
	
	
		
			4.3 KiB
		
	
	
	
		
			NASM
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			No EOL
		
	
	
		
			4.3 KiB
		
	
	
	
		
			NASM
		
	
	
	
	
	
; ========================================================================================
 | 
						|
; array_reversal.asm - Array Reversal Using Swap Algorithm
 | 
						|
; ========================================================================================
 | 
						|
; This program demonstrates how to reverse an array in-place using a swap algorithm.
 | 
						|
; The algorithm uses two pointers - one starting from the beginning and one from the end
 | 
						|
; of the array. Elements are swapped in each iteration, and the pointers move towards
 | 
						|
; the center until they meet or cross each other.
 | 
						|
 | 
						|
	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 counter R2 to half the array size (5 iterations for 10 elements)
 | 
						|
; 2. Set R0 to point to the start of the array (SRC)
 | 
						|
; 3. Set R1 to point to the end of the array (SRC + 36 bytes = last element)
 | 
						|
; 4. In each iteration:
 | 
						|
;    a. Load element from start pointer (R0) into R3
 | 
						|
;    b. Load element from end pointer (R1) into R4
 | 
						|
;    c. Store R3 to end pointer location and decrement R1 by 4 bytes
 | 
						|
;    d. Store R4 to start pointer location and increment R0 by 4 bytes
 | 
						|
;    e. Decrement counter and repeat until counter reaches zero
 | 
						|
Reset_Handler
 | 
						|
	; Step 1: Initialize loop counter
 | 
						|
	; Set R2 to 5 (half of array size 10) - number of swap operations needed
 | 
						|
	MOV R2, #5                        ; R2 = 5 (loop counter for 5 swap operations)
 | 
						|
 | 
						|
	; Step 2: Initialize array pointers
 | 
						|
	; R0 points to the beginning of the array
 | 
						|
	LDR R0, =SRC                      ; R0 = address of first element in SRC array
 | 
						|
 | 
						|
	; R1 points to the end of the array (SRC + 36 bytes)
 | 
						|
	; 36 = 9 * 4 bytes (offset to reach the last element from first)
 | 
						|
	LDR R1, =SRC + 36                 ; R1 = address of last element in SRC array
 | 
						|
 | 
						|
	; Step 3: Main reversal loop
 | 
						|
Loop
 | 
						|
	; Load the element from the start of array into R3
 | 
						|
	LDR R3, [R0]                      ; R3 = element at current start position
 | 
						|
 | 
						|
	; Load the element from the end of array into R4
 | 
						|
	LDR R4, [R1]                      ; R4 = element at current end position
 | 
						|
 | 
						|
	; Swap: Store start element (R3) to end position
 | 
						|
	; Use post-decrement addressing: store R3 to [R1], then R1 = R1 - 4
 | 
						|
	STR R3, [R1], #-4                 ; Store R3 to end position and move pointer left
 | 
						|
 | 
						|
	; Swap: Store end element (R4) to start position
 | 
						|
	; Use post-increment addressing: store R4 to [R0], then R0 = R0 + 4
 | 
						|
	STR R4, [R0], #4                  ; Store R4 to start position and move pointer right
 | 
						|
 | 
						|
	; Decrement loop counter
 | 
						|
	SUBS R2, #1                       ; R2 = R2 - 1 (set flags for branch condition)
 | 
						|
 | 
						|
	; Continue loop if counter is not zero
 | 
						|
	BNE Loop                          ; Branch to Loop if R2 != 0
 | 
						|
 | 
						|
	; Step 4: Program termination
 | 
						|
	; Create an infinite loop to stop program execution
 | 
						|
STOP
 | 
						|
	B STOP                            ; Branch to STOP label (infinite loop)
 | 
						|
 | 
						|
	AREA mydate, DATA, READWRITE      ; Define a read-write data section
 | 
						|
 | 
						|
; ========================================================================================
 | 
						|
; Data Section - Source Array
 | 
						|
; ========================================================================================
 | 
						|
; SRC array contains 10 elements (40 bytes total):
 | 
						|
; Each element is a 32-bit word in hexadecimal format
 | 
						|
; Original array: [32, 12345644, 05, 98, AB, CD, 55, 32, CA, 45]
 | 
						|
; After reversal: [45, CA, 32, 55, CD, AB, 98, 05, 12345644, 32]
 | 
						|
SRC DCD 0x00000032, 0x12345644, 0x00000005, 0x00000098, 0x000000AB, 0x000000CD, 0x00000055, 0x00000032, 0x000000CA, 0x00000045
 | 
						|
 | 
						|
	END                               ; End of the assembly program |