93 lines
No EOL
4.7 KiB
NASM
93 lines
No EOL
4.7 KiB
NASM
; ========================================================================================
|
|
; SWAP.asm - Basic Array Copy Using Post-Increment Addressing
|
|
; ========================================================================================
|
|
; This program demonstrates how to copy data from one array to another using
|
|
; post-increment addressing mode. Each load/store operation automatically
|
|
; advances the pointer to the next array element, eliminating the need for
|
|
; separate pointer arithmetic instructions.
|
|
|
|
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. Load addresses of source (SRC) and destination (DST) arrays
|
|
; 2. Copy each element individually using post-increment addressing
|
|
; 3. The '!' symbol after the offset means post-increment addressing:
|
|
; - Load from [R0, #4]! means: load from R0+4, then R0 = R0 + 4
|
|
; - Store to [R1, #4]! means: store to R1+4, then R1 = R1 + 4
|
|
; 4. This approach copies 10 elements (using registers R2-R11)
|
|
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: Copy first element (no post-increment for initial load)
|
|
; Load from SRC[0] into R2, store to DST[0]
|
|
LDR R2, [R0] ; R2 = SRC[0] (no increment yet)
|
|
STR R2, [R1] ; DST[0] = R2 (no increment yet)
|
|
|
|
; Step 3: Copy remaining elements using post-increment addressing
|
|
; Each load/store pair advances both pointers by 4 bytes automatically
|
|
LDR R3, [R0,#4]! ; R3 = SRC[1], then R0 = R0 + 4
|
|
STR R3, [R1,#4]! ; DST[1] = R3, then R1 = R1 + 4
|
|
|
|
LDR R4, [R0,#4]! ; R4 = SRC[2], then R0 = R0 + 4
|
|
STR R4, [R1,#4]! ; DST[2] = R4, then R1 = R1 + 4
|
|
|
|
LDR R5, [R0,#4]! ; R5 = SRC[3], then R0 = R0 + 4
|
|
STR R5, [R1,#4]! ; DST[3] = R5, then R1 = R1 + 4
|
|
|
|
LDR R6, [R0,#4]! ; R6 = SRC[4], then R0 = R0 + 4
|
|
STR R6, [R1,#4]! ; DST[4] = R6, then R1 = R1 + 4
|
|
|
|
LDR R7, [R0,#4]! ; R7 = SRC[5], then R0 = R0 + 4
|
|
STR R7, [R1,#4]! ; DST[5] = R7, then R1 = R1 + 4
|
|
|
|
LDR R8, [R0,#4]! ; R8 = SRC[6], then R0 = R0 + 4
|
|
STR R8, [R1,#4]! ; DST[6] = R8, then R1 = R1 + 4
|
|
|
|
LDR R9, [R0,#4]! ; R9 = SRC[7], then R0 = R0 + 4
|
|
STR R9, [R1,#4]! ; DST[7] = R9, then R1 = R1 + 4
|
|
|
|
LDR R10, [R0,#4]! ; R10 = SRC[8], then R0 = R0 + 4
|
|
STR R10, [R1,#4]! ; DST[8] = R10, then R1 = R1 + 4
|
|
|
|
LDR R11, [R0,#4]! ; R11 = SRC[9], then R0 = R0 + 4
|
|
STR R11, [R1,#4]! ; DST[9] = R11, then R1 = R1 + 4
|
|
|
|
; 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 one zero, but will be expanded during copy operations
|
|
; The actual size will be determined by how many elements are copied
|
|
DST DCD 0
|
|
|
|
END ; End of the assembly program |