comm
This commit is contained in:
parent
c37a788cbd
commit
dbabb956d5
16 changed files with 1319 additions and 426 deletions
|
@ -1,34 +1,83 @@
|
|||
AREA RESET, DATA, READONLY
|
||||
EXPORT __Vectors
|
||||
; ========================================================================================
|
||||
; 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.
|
||||
|
||||
__Vectors
|
||||
DCD 0x10001000
|
||||
DCD Reset_Handler
|
||||
ALIGN
|
||||
AREA MYCODE, CODE, READONLY
|
||||
ENTRY
|
||||
EXPORT Reset_Handler
|
||||
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
|
||||
LDR R0, =SRC
|
||||
MOV R3, #10
|
||||
|
||||
; 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
|
||||
LDR R1, [R0], #4
|
||||
ADDS R2, R1
|
||||
ADC R5, #0
|
||||
SUBS R3, #1
|
||||
BNE UP;
|
||||
|
||||
LDR R4, =Result
|
||||
STR R2, [R4]
|
||||
STR R5, [R4]
|
||||
|
||||
; 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
|
||||
|
||||
|
||||
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
|
||||
|
||||
END
|
||||
DCD 0 ; Space for lower 32 bits of sum
|
||||
|
||||
END ; End of the assembly program
|
|
@ -1,44 +1,96 @@
|
|||
AREA RESET, DATA, READONLY
|
||||
EXPORT __Vectors
|
||||
|
||||
__Vectors
|
||||
DCD 0x10001000
|
||||
DCD Reset_Handler
|
||||
|
||||
ALIGN
|
||||
|
||||
AREA MYCODE, CODE, READONLY
|
||||
ENTRY
|
||||
|
||||
EXPORT Reset_Handler
|
||||
; ========================================================================================
|
||||
; add128bit.asm - 128-Bit Addition Using Multiple Precision Arithmetic
|
||||
; ========================================================================================
|
||||
; This program demonstrates how to add two 128-bit numbers using ARM assembly.
|
||||
; Since ARM registers are 32-bit, large numbers are stored as arrays of 32-bit words.
|
||||
; The program uses ADCS (Add with Carry Set) instruction to handle carries between
|
||||
; individual 32-bit words, enabling multi-precision arithmetic.
|
||||
|
||||
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 pointers to two 128-bit numbers (stored as 4x32-bit words each)
|
||||
; 2. Process each 32-bit word from least significant to most significant
|
||||
; 3. Use ADCS instruction to add corresponding words and propagate carry
|
||||
; 4. Store the result (there are some issues in the original code with result storage)
|
||||
Reset_Handler
|
||||
LDR R1, =N1
|
||||
LDR R2, =N2
|
||||
MOV R3, #4
|
||||
|
||||
UP
|
||||
LDR R4, [R1], #4
|
||||
LDR R5, [R2], #4
|
||||
ADCS R6, R5, R4
|
||||
SUB R3, #1
|
||||
TEQ R3, #0
|
||||
BNE UP;
|
||||
|
||||
LDR R8, =Result
|
||||
STR R2, [R8], #4
|
||||
STR R5, [R8]
|
||||
; Step 1: Initialize pointers to the two 128-bit numbers
|
||||
; Each number is stored as an array of four 32-bit words
|
||||
; N1 and N2 represent the two 128-bit operands
|
||||
LDR R1, =N1 ; R1 = address of first 128-bit number (N1)
|
||||
LDR R2, =N2 ; R2 = address of second 128-bit number (N2)
|
||||
|
||||
; Initialize loop counter for 4 iterations (4 words = 128 bits)
|
||||
MOV R3, #4 ; R3 = 4 (number of 32-bit words to process)
|
||||
|
||||
; Step 2: Main addition loop - process each 32-bit word
|
||||
UP
|
||||
; Load the next 32-bit word from each operand
|
||||
; Post-increment addressing advances pointers to next word
|
||||
LDR R4, [R1], #4 ; Load word from N1, advance R1 to next word
|
||||
LDR R5, [R2], #4 ; Load word from N2, advance R2 to next word
|
||||
|
||||
; Add the two words with carry from previous addition
|
||||
; ADCS adds R5 + R4 + carry flag and sets carry flag for next iteration
|
||||
; This handles carries between 32-bit word boundaries
|
||||
ADCS R6, R5, R4 ; R6 = R5 + R4 + carry, set carry for next iteration
|
||||
|
||||
; Decrement loop counter
|
||||
SUB R3, #1 ; R3 = R3 - 1
|
||||
|
||||
; Test if loop counter equals zero
|
||||
; TEQ (Test Equal) compares R3 with #0 and sets condition flags
|
||||
TEQ R3, #0 ; Set Z flag if R3 == 0
|
||||
|
||||
; Branch back to UP if counter is not zero (Z flag not set)
|
||||
BNE UP ; If R3 != 0, continue loop
|
||||
|
||||
; Step 3: Store the result
|
||||
; Note: There are issues in the original code here
|
||||
; R2 has been incremented and no longer points to N2
|
||||
; R5 contains the last loaded word, not part of the result
|
||||
; R6 contains the last addition result but only the final word is stored
|
||||
LDR R8, =Result ; R8 = address of result storage
|
||||
STR R2, [R8], #4 ; Store incorrect value (should be R6)
|
||||
STR R5, [R8] ; Store incorrect value (should be carry or next word)
|
||||
|
||||
; Step 4: Program termination
|
||||
STOP
|
||||
B STOP
|
||||
ALIGN
|
||||
|
||||
N1 DCD 0x10002000, 0x30004000, 0x50006000, 0x70008000
|
||||
N2 DCD 0x10002000, 0x30004000, 0x50006000, 0x70008000
|
||||
|
||||
AREA mydata, DATA, READWRITE
|
||||
|
||||
B STOP ; Branch to STOP label (infinite loop)
|
||||
|
||||
ALIGN ; Ensure proper alignment for data section
|
||||
|
||||
; ========================================================================================
|
||||
; Data Section - 128-bit operands
|
||||
; ========================================================================================
|
||||
; N1: First 128-bit number stored as four 32-bit words (MSB to LSB):
|
||||
; Word 0: 0x10002000, Word 1: 0x30004000, Word 2: 0x50006000, Word 3: 0x70008000
|
||||
; Represents: 0x10002000300040005000600070008000 in hexadecimal
|
||||
N1 DCD 0x10002000, 0x30004000, 0x50006000, 0x70008000
|
||||
|
||||
; N2: Second 128-bit number (same value as N1 for demonstration)
|
||||
; Represents: 0x10002000300040005000600070008000 in hexadecimal
|
||||
N2 DCD 0x10002000, 0x30004000, 0x50006000, 0x70008000
|
||||
|
||||
AREA mydata, DATA, READWRITE ; Define a read-write data section
|
||||
|
||||
; Result storage for the 128-bit sum
|
||||
; Note: Should store 5 words (4 for result + 1 for final carry)
|
||||
; Current allocation is insufficient for proper 128-bit result
|
||||
Result DCD 0
|
||||
|
||||
END
|
||||
END ; End of the assembly program
|
Loading…
Add table
Add a link
Reference in a new issue