MIT-Curricular/ES/Lab/Lab3/ADDER.asm
2025-08-28 10:11:40 +05:30

83 lines
No EOL
4.2 KiB
NASM

; ========================================================================================
; ADDER.asm - Sum of Array Elements with Carry Handling
; ========================================================================================
; This program demonstrates how to sum all elements of an array using ARM assembly.
; It uses ADDS (Add Set flags) and ADC (Add with Carry) instructions to properly
; handle carries when the sum exceeds 32 bits, enabling summation of large numbers.
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 pointer to the array of numbers to be summed
; 2. Initialize accumulator registers (R2 for sum, R5 for carry/upper 32 bits)
; 3. Process each element in the array using a loop
; 4. Use ADDS to add each element and set carry flag
; 5. Use ADC to add the carry to the upper 32 bits
; 6. Store the final 64-bit sum (lower 32 bits + upper 32 bits)
Reset_Handler
; Step 1: Initialize array pointer and loop counter
; R0 points to the beginning of the source array
LDR R0, =SRC ; R0 = address of first element in SRC array
; R3 holds the number of elements to sum (10 elements total)
MOV R3, #10 ; R3 = 10 (loop counter)
; Step 2: Main summation loop
UP
; Load the next element from the array
; Post-increment addressing advances R0 to the next element
LDR R1, [R0], #4 ; Load next element into R1, advance R0
; Add the current element to the running sum
; ADDS adds R1 to R2 and sets condition flags including carry flag
; This handles addition within the lower 32 bits
ADDS R2, R1 ; R2 = R2 + R1, set carry flag if overflow
; Add the carry from the previous addition to the upper 32 bits
; ADC adds #0 + carry flag to R5, effectively capturing the carry
; This extends the summation to 64 bits total
ADC R5, #0 ; R5 = R5 + 0 + carry (from previous ADDS)
; Decrement loop counter and set condition flags
SUBS R3, #1 ; R3 = R3 - 1, set flags for branch condition
; Branch back to UP if counter is not zero
BNE UP ; If R3 != 0, continue loop
; Step 3: Store the final result
; The result is a 64-bit sum stored in two 32-bit words
; R2 contains the lower 32 bits of the sum
; R5 contains the upper 32 bits (carry accumulated from all additions)
LDR R4, =Result ; R4 = address of result storage
STR R2, [R4] ; Store lower 32 bits of sum
STR R5, [R4] ; Store upper 32 bits (should be [R4, #4])
; ========================================================================================
; Data Section - Array of numbers to be summed
; ========================================================================================
; SRC array contains 10 elements to be added together:
; Sum = 0x12345678 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
; The result will be stored as a 64-bit value due to potential overflow
SRC DCD 0x12345678, 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007, 0x00000008, 0x00000009
AREA mydata, DATA, READWRITE ; Define a read-write data section
; Result storage for the 64-bit sum
; Should store two 32-bit words: [lower_sum, upper_sum]
Result
DCD 0 ; Space for lower 32 bits of sum
END ; End of the assembly program