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

99 lines
No EOL
5.1 KiB
NASM

; ========================================================================================
; MULTINDEX.asm - Mixed Addressing Modes for Array Copy Operations
; ========================================================================================
; This program demonstrates different addressing modes used in ARM assembly for
; array operations. It intentionally mixes various approaches to show the
; different ways memory can be accessed and how pointers are managed.
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:
; This program demonstrates various addressing modes for copying array elements:
; 1. Basic load/store without pointer advancement
; 2. Post-increment addressing (! symbol) - pointer advances after operation
; 3. Pre-indexed addressing without (!) - pointer doesn't advance automatically
; 4. Mixed approaches to show flexibility in ARM assembly programming
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 - Basic addressing (no pointer advancement)
; Load from [R0] and store to [R1] without changing pointer values
LDR R2,[R0] ; R2 = SRC[0] (R0 unchanged)
STR R2,[R1] ; DST[0] = R2 (R1 unchanged)
; Step 3: Copy next 3 elements - Post-increment addressing
; Using '!' symbol means pointer is updated after the operation
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
; Step 4: Copy next 3 elements - Pre-indexed addressing without advancement
; Without '!' symbol, pointers are not automatically updated
; Manual pointer advancement is needed (not shown here, which may be intentional)
LDR R6,[R0,#4] ; R6 = SRC[4] (R0 unchanged)
STR R6,[R1,#4] ; DST[4] = R6 (R1 unchanged)
LDR R7,[R0,#4] ; R7 = SRC[5] (R0 unchanged)
STR R7,[R1,#4] ; DST[5] = R7 (R1 unchanged)
LDR R8,[R0,#4] ; R8 = SRC[6] (R0 unchanged)
STR R8,[R1,#4] ; DST[6] = R8 (R1 unchanged)
; Step 5: Copy last 3 elements - Post-increment addressing again
; Switch back to post-increment mode for the final elements
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 6: 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
; Note: The middle section (elements 4-6) uses pre-indexed addressing without
; pointer advancement, which means the same memory locations are accessed
; multiple times. This demonstrates different addressing mode behaviors.
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 to accommodate all 10 elements
DST DCD 0
END ; End of the assembly program