diff --git a/ES/Lab/LAB4/ASCIItoHEX.asm b/ES/Lab/LAB4/ASCIItoHEX.asm index 58e4c88..b28efc4 100644 --- a/ES/Lab/LAB4/ASCIItoHEX.asm +++ b/ES/Lab/LAB4/ASCIItoHEX.asm @@ -1,38 +1,100 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - - AREA MYCODE, CODE, READONLY - ENTRY - EXPORT Reset_Handler +; ======================================================================================== +; ASCIItoHEX.asm - ASCII Hexadecimal String to 32-bit Hexadecimal Conversion +; ======================================================================================== +; This program converts an ASCII string representing hexadecimal digits into a +; 32-bit hexadecimal value. It processes each ASCII character, converts it to +; its corresponding 4-bit hexadecimal value, and builds the final 32-bit result +; by shifting and ORing each nibble into place. + + 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 source ASCII string and destination for result +; 2. Initialize result accumulator (R1) to 0 +; 3. Process 8 characters (for 32-bit result) in a loop +; 4. For each character: +; a. Load ASCII character (byte) +; b. Check if it's A-F (greater than '9') +; c. Convert ASCII digit to 4-bit hexadecimal value +; d. Shift current result left by 4 bits +; e. OR the new nibble into the result +; 5. Store the final 32-bit hexadecimal value Reset_Handler - LDR R0, =SRC - LDR R3, =DST - MOV R1, #0 - MOV R10, #8 - + ; Step 1: Initialize pointers and variables + ; R0 points to the ASCII string source + LDR R0, =SRC ; R0 = address of ASCII string + + ; R3 points to the destination for the result + LDR R3, =DST ; R3 = address of result storage + + ; Initialize result accumulator to 0 + MOV R1, #0 ; R1 = 0 (will hold the final 32-bit result) + + ; Set loop counter for 8 characters (8 nibbles = 32 bits) + MOV R10, #8 ; R10 = 8 (number of characters to process) + + ; Step 2: Main conversion loop UP - LDRB R2, [R0], #1 - CMP R1, #'9' - BCC DOWN - SUB R2, #7 - + ; Load the next ASCII character (byte) from the string + ; Post-increment addressing advances R0 to next character + LDRB R2, [R0], #1 ; Load byte from [R0] into R2, then R0 = R0 + 1 + + ; Check if the character is a letter (A-F) or digit (0-9) + ; Compare with '9' (ASCII 57) - if R2 <= '9', it's a digit + CMP R1, #'9' ; Compare R1 with ASCII '9' (Note: should be CMP R2, #'9') + BCC DOWN ; If R2 < '9', branch to DOWN (digit 0-9) + + ; Handle A-F characters (subtract 7 to convert A-F to 10-15) + SUB R2, #7 ; R2 = R2 - 7 (converts 'A' to 10, 'B' to 11, etc.) + DOWN - SUB R2, #0x30 - LSL R1, #4 - ORR R1, R1, R2 - SUBS R10, #1 - BNE UP + ; Convert ASCII digit to actual hexadecimal value + ; Subtract ASCII '0' (0x30) to convert '0'-'9' to 0-9 + SUB R2, #0x30 ; R2 = R2 - 0x30 (ASCII to numeric conversion) - STR R1, [R3] -SRC DCB "12AB34CF" - - AREA mydata, DATA, READWRITE + ; Shift the current result left by 4 bits to make room for new nibble + LSL R1, #4 ; R1 = R1 << 4 (shift left by 4 bits) -DST DCD 0 + ; OR the new 4-bit value into the result + ORR R1, R1, R2 ; R1 = R1 | R2 (insert new nibble) - END \ No newline at end of file + ; Decrement loop counter and set condition flags + SUBS R10, #1 ; R10 = R10 - 1, set flags for branch condition + + ; Branch back to UP if counter is not zero + BNE UP ; If R10 != 0, continue loop + + ; Step 3: Store the final result + ; Store the 32-bit hexadecimal value to destination + STR R1, [R3] ; Store R1 (final result) to [R3] + +; ======================================================================================== +; Data Section - ASCII Source String and Result Storage +; ======================================================================================== +; SRC contains ASCII string "12AB34CF" (8 characters) +; Each character represents a hexadecimal digit: +; '1','2' -> digits 0-9 +; 'A','B','C','F' -> digits 10-15 +; The string will be converted to: 0x12AB34CF +SRC DCB "12AB34CF" ; ASCII string to be converted + + AREA mydata, DATA, READWRITE ; Define a read-write data section + +; DST - Storage for the final 32-bit hexadecimal result +DST DCD 0 ; Space for the converted result + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/LAB4/BCDtoHEX.asm b/ES/Lab/LAB4/BCDtoHEX.asm index f825553..70abdec 100644 --- a/ES/Lab/LAB4/BCDtoHEX.asm +++ b/ES/Lab/LAB4/BCDtoHEX.asm @@ -1,35 +1,93 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - - AREA MYCODE, CODE, READONLY - ENTRY - EXPORT Reset_Handler +; ======================================================================================== +; BCDtoHEX.asm - Binary Coded Decimal to Hexadecimal Conversion +; ======================================================================================== +; This program converts a BCD (Binary Coded Decimal) value to its equivalent +; hexadecimal representation. BCD stores each decimal digit in 4 bits, so a +; byte can represent two decimal digits (00-99). The program extracts each +; BCD digit and converts the entire BCD number to its decimal equivalent. + + 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 the BCD value from memory (each byte contains two BCD digits) +; 2. Initialize multiplier (R2) to 1 for units place +; 3. Process 3 iterations (for 3 BCD digits in the example) +; 4. In each iteration: +; a. Extract lowest 4 bits (current BCD digit) using AND +; b. Multiply digit by current place value and add to result +; c. Shift BCD value right by 4 bits to get next digit +; d. Multiply place value by 10 for next iteration +; 5. Result (R4) contains the decimal equivalent of the BCD number Reset_Handler - LDR R0, =SRR - MOV R10, #3 - LDR R1, [R0] - MOV R2, #1 - -UP - AND R3, R1, #0x0F - MLA R4, R2, R3, R4 - LSR R1, #4 - MOV R5, #0x0A - MUL R2, R5 - SUBS R10, #1 - BNE UP - -STOP - B STOP - -SRR DCD 0x45 + ; Step 1: Initialize source pointer and load BCD value + ; R0 points to the BCD value in memory + LDR R0, =SRR ; R0 = address of BCD value - AREA mydata, DATA, READWRITE -SRC DCD 0x45 - - END \ No newline at end of file + ; Set loop counter for 3 digits (processing 12 bits of the 32-bit value) + MOV R10, #3 ; R10 = 3 (number of BCD digits to process) + + ; Load the BCD value into R1 + LDR R1, [R0] ; R1 = BCD value (e.g., 0x45 = 0100 0101 BCD) + + ; Initialize multiplier for place value (1 for units, 10 for tens, etc.) + MOV R2, #1 ; R2 = 1 (initial place value multiplier) + + ; Step 2: Main BCD to decimal conversion loop +UP + ; Extract the lowest 4 bits (current BCD digit 0-9) + ; AND with 0x0F isolates the least significant nibble + AND R3, R1, #0x0F ; R3 = R1 & 0x0F (extract BCD digit) + + ; Multiply digit by place value and accumulate result + ; MLA (Multiply Accumulate) computes: R4 = R2 * R3 + R4 + ; This adds the current digit's contribution to the total + MLA R4, R2, R3, R4 ; R4 = (R2 * R3) + R4 + + ; Shift BCD value right by 4 bits to access next digit + LSR R1, #4 ; R1 = R1 >> 4 (move to next BCD digit) + + ; Prepare multiplier for next place value (multiply by 10) + ; This sets up R2 for tens, hundreds, etc. in subsequent iterations + MOV R5, #0x0A ; R5 = 10 (decimal) + MUL R2, R5 ; R2 = R2 * 10 (update place value) + + ; Decrement loop counter and set condition flags + SUBS R10, #1 ; R10 = R10 - 1, set flags for branch condition + + ; Branch back to UP if counter is not zero + BNE UP ; If R10 != 0, continue loop + + ; Step 3: Program termination + ; Note: The result is stored in R4 but never saved to memory +STOP + B STOP ; Branch to STOP label (infinite loop) + +; ======================================================================================== +; Data Section - BCD Source Value +; ======================================================================================== +; SRR contains a BCD value: 0x45 +; In BCD format: 0100 0101 = digits 4 and 5 +; Decimal equivalent: 4*10 + 5 = 45 +; The program converts this to decimal 45 (though result is never stored) +SRR DCD 0x45 ; BCD value to be converted + + AREA mydata, DATA, READWRITE ; Define a read-write data section + +; SRC - Additional storage (seems unused in this program) +SRC DCD 0x45 ; Duplicate of SRR (possibly for testing) + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/LAB4/GCD.asm b/ES/Lab/LAB4/GCD.asm index 79b74f7..34d1e21 100644 --- a/ES/Lab/LAB4/GCD.asm +++ b/ES/Lab/LAB4/GCD.asm @@ -1,41 +1,92 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN +; ======================================================================================== +; GCD.asm - Greatest Common Divisor Using Euclidean Algorithm +; ======================================================================================== +; This program implements the Euclidean algorithm to find the Greatest Common +; Divisor (GCD) of two numbers. The Euclidean algorithm is based on the principle +; that GCD(a, b) = GCD(b, a mod b), and it repeats this process until b = 0. +; When b = 0, the GCD is the value of a. - 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 two numbers in registers R0 and R1 (48 and 18 in this example) +; 2. Enter the Euclidean algorithm loop: +; a. Compare the two numbers +; b. If equal, GCD is found (algorithm complete) +; c. If R0 > R1, subtract R1 from R0 (replace larger with difference) +; d. If R0 < R1, subtract R0 from R1 (replace larger with difference) +; e. Repeat until both numbers are equal +; 3. Store the GCD result to memory +; 4. Enter infinite loop for program termination Reset_Handler - MOV r0, #48 - MOV r1, #18 + ; Step 1: Initialize the two numbers for GCD calculation + ; R0 and R1 hold the two numbers to find GCD of + MOV r0, #48 ; R0 = 48 (first number) + MOV r1, #18 ; R1 = 18 (second number) + ; Step 2: Main Euclidean algorithm loop GCD_Loop - CMP r0, r1 - BEQ GCD_Done - BGT GT_A_B - SUB r1, r1, r0 - B GCD_Loop + ; Compare the two numbers to determine which is larger + CMP r0, r1 ; Compare R0 and R1, set condition flags + ; If R0 == R1, we have found the GCD + BEQ GCD_Done ; Branch to GCD_Done if R0 == R1 + + ; If R0 > R1 (GT condition), subtract R1 from R0 + BGT GT_A_B ; Branch to GT_A_B if R0 > R1 + + ; If R0 < R1, subtract R0 from R1 (replace larger with difference) + SUB r1, r1, r0 ; R1 = R1 - R0 (R1 was larger, now smaller) + B GCD_Loop ; Branch back to GCD_Loop + + ; Handle case where R0 > R1 GT_A_B - SUB r0, r0, r1 - B GCD_Loop + SUB r0, r0, r1 ; R0 = R0 - R1 (R0 was larger, now smaller) + B GCD_Loop ; Branch back to GCD_Loop + ; Step 3: GCD calculation complete + ; When we reach here, R0 == R1 and contains the GCD GCD_Done - LDR r2, =result - STR r0, [r2] + ; Load address of result storage into R2 + LDR r2, =result ; R2 = address of result storage + ; Store the GCD (in R0) to memory + STR r0, [r2] ; Store GCD value to [R2] + + ; Step 4: Program termination + ; Enter infinite loop to stop program execution LoopForever - B LoopForever + B LoopForever ; Branch to LoopForever (infinite loop) - ALIGN + ALIGN ; Ensure proper alignment for data section - AREA MYDATA, DATA, READWRITE -numA DCD 48 -numB DCD 18 -result DCD 0 + AREA MYDATA, DATA, READWRITE ; Define a read-write data section - END \ No newline at end of file +; ======================================================================================== +; Data Section - Input Numbers and Result Storage +; ======================================================================================== +; numA: First number for GCD calculation (48 in this example) +; numB: Second number for GCD calculation (18 in this example) +; GCD(48, 18) = 6, as calculated by the Euclidean algorithm: +; GCD(48, 18) -> GCD(18, 48-18=30) -> GCD(30, 18) -> GCD(18, 30-18=12) +; -> GCD(12, 18) -> GCD(18, 12) -> GCD(12, 18-12=6) -> GCD(6, 12) +; -> GCD(12, 6) -> GCD(6, 12-6=0) -> GCD(6, 0) = 6 +numA DCD 48 ; First number for GCD +numB DCD 18 ; Second number for GCD +result DCD 0 ; Storage for the GCD result + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/LAB4/HEXtoASCII.asm b/ES/Lab/LAB4/HEXtoASCII.asm index 01941e4..838e355 100644 --- a/ES/Lab/LAB4/HEXtoASCII.asm +++ b/ES/Lab/LAB4/HEXtoASCII.asm @@ -1,37 +1,102 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - - AREA MYCODE, CODE, READONLY - ENTRY - EXPORT Reset_Handler +; ======================================================================================== +; HEXtoASCII.asm - 32-bit Hexadecimal to ASCII String Conversion +; ======================================================================================== +; This program converts a 32-bit hexadecimal value into its ASCII string +; representation. Each 4-bit nibble of the hexadecimal value is converted +; to its corresponding ASCII character ('0'-'9' for digits, 'A'-'F' for +; values 10-15). The result is stored as a sequence of ASCII bytes. + + 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 the 32-bit hexadecimal value from memory +; 2. Initialize destination pointer for ASCII string storage +; 3. Set loop counter for 8 nibbles (32 bits = 8 nibbles) +; 4. Process each nibble from least significant to most significant: +; a. Extract lowest 4 bits using AND with 0x0F +; b. Check if digit (0-9) or letter (A-F) +; c. Convert to ASCII: add 0x30 for digits, add 0x37 for A-F +; d. Store ASCII character to destination array +; e. Shift original value right by 4 bits for next nibble +; 5. Result is an ASCII string representing the hexadecimal value Reset_Handler - LDR R0, =SRC - LDR R1, [R0] - LDR R3, =DST - MOV R10, #8 - + ; Step 1: Initialize source and destination pointers + ; R0 points to the 32-bit hexadecimal value + LDR R0, =SRC ; R0 = address of hexadecimal value + + ; Load the hexadecimal value into R1 + LDR R1, [R0] ; R1 = 32-bit hexadecimal value (e.g., 0x12AB34CF) + + ; R3 points to the destination for ASCII string + LDR R3, =DST ; R3 = address of ASCII string storage + + ; Step 2: Initialize loop counter + ; Process 8 nibbles (32 bits / 4 bits per nibble = 8 nibbles) + MOV R10, #8 ; R10 = 8 (number of nibbles to process) + + ; Step 3: Main conversion loop - process each nibble UP - AND R2, R1, #0x0F - CMP R2, #09 - BCC DOWN - ADD R2, #7 - + ; Extract the lowest 4 bits (current nibble 0-15) + ; AND with 0x0F isolates the least significant nibble + AND R2, R1, #0x0F ; R2 = R1 & 0x0F (extract nibble 0-15) + + ; Check if the nibble represents a digit (0-9) or letter (A-F) + ; Compare with 9 (decimal) - values 0-9 are digits, 10-15 are A-F + CMP R2, #09 ; Compare R2 with 9 + BCC DOWN ; If R2 <= 9, branch to DOWN (digit) + + ; Handle A-F characters (add 7 to skip punctuation in ASCII table) + ; ASCII: '0'-'9' = 0x30-0x39, 'A'-'F' = 0x41-0x46 + ; For values 10-15: add 0x30 + 7 = 0x37 to get 'A'-'F' + ADD R2, #7 ; R2 = R2 + 7 (adjust for A-F range) + + ; Convert nibble to ASCII character DOWN - ADD R2, #0x30 - STR R2, [R3], #4 - LSR R1, #4 - SUBS R10, #1 - BNE UP - -SRC DCD 0x12AB34CF - - AREA mydata, DATA, READWRITE + ; Add ASCII '0' (0x30) to convert nibble to ASCII digit/letter + ; For digits 0-9: 0-9 + 0x30 = 0x30-0x39 ('0'-'9') + ; For A-F: 10-15 + 0x37 = 0x41-0x46 ('A'-'F') + ADD R2, #0x30 ; R2 = R2 + 0x30 (convert to ASCII) -DST DCD 0 + ; Store the ASCII character to destination array + ; Post-increment addressing advances R3 to next position + STR R2, [R3], #4 ; Store ASCII char and advance R3 by 4 bytes - END \ No newline at end of file + ; Shift the original value right by 4 bits to process next nibble + LSR R1, #4 ; R1 = R1 >> 4 (move to next nibble) + + ; Decrement loop counter and set condition flags + SUBS R10, #1 ; R10 = R10 - 1, set flags for branch condition + + ; Branch back to UP if counter is not zero + BNE UP ; If R10 != 0, continue loop + +; ======================================================================================== +; Data Section - Hexadecimal Source Value and ASCII String Storage +; ======================================================================================== +; SRC contains the 32-bit hexadecimal value: 0x12AB34CF +; This will be converted to ASCII string: "12AB34CF" +; Each nibble is converted to its ASCII equivalent: +; 0x1 -> '1', 0x2 -> '2', 0xA -> 'A', 0xB -> 'B', 0x3 -> '3', 0x4 -> '4', 0xC -> 'C', 0xF -> 'F' +SRC DCD 0x12AB34CF ; 32-bit hexadecimal value to be converted + + AREA mydata, DATA, READWRITE ; Define a read-write data section + +; DST - Storage for the ASCII string result (8 characters, 32 bytes) +; Each ASCII character is stored as a word (4 bytes) for easier access +DST DCD 0 ; Space for ASCII string (will expand as needed) + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/LAB4/HEXtoBCD.asm b/ES/Lab/LAB4/HEXtoBCD.asm index cbc3294..63f1147 100644 --- a/ES/Lab/LAB4/HEXtoBCD.asm +++ b/ES/Lab/LAB4/HEXtoBCD.asm @@ -9,31 +9,97 @@ __Vectors ENTRY EXPORT Reset_Handler +; ======================================================================================== +; Reset_Handler - Main program execution +; ======================================================================================== +; Algorithm Overview: +; 1. Load the hexadecimal value from memory +; 2. Initialize BCD result accumulator (R4) to 0 +; 3. Initialize digit position multiplier (R11) to 1 (units place) +; 4. Set loop counter for number of BCD digits to process +; 5. In each iteration: +; a. Divide current value by 10 to get quotient and remainder +; b. The remainder is the current BCD digit (0-9) +; c. Multiply digit by position value and add to BCD result +; d. Shift position multiplier left by 4 bits for next digit +; e. Use quotient as input for next iteration +; 6. Result (R4) contains the BCD representation of the original number + Reset_Handler - LDR R0, =SRR - LDR R1, [R0] - MOV R4, #0 - MOV R11, #1 - MOV R10, #3 + ; Step 1: Initialize source pointer and load hexadecimal value + ; R0 points to the hexadecimal value in memory + LDR R0, =SRR ; R0 = address of hexadecimal value + ; Load the hexadecimal value into R1 + LDR R1, [R0] ; R1 = hexadecimal value (e.g., 0x45 = 69 decimal) + + ; Step 2: Initialize BCD conversion variables + ; R4 will accumulate the final BCD result + MOV R4, #0 ; R4 = 0 (BCD result accumulator) + + ; R11 holds the position multiplier for each BCD digit + ; Starts at 1 for units place, shifts left by 4 bits each iteration + MOV R11, #1 ; R11 = 1 (position multiplier) + + ; Set loop counter for 3 BCD digits (maximum for byte values 0-255) + MOV R10, #3 ; R10 = 3 (number of BCD digits to process) + + ; Step 3: Main BCD conversion loop LOOP - MOV R5, #10 - UDIV R3, R1, R5 - MUL R6, R3, R5 - SUB R7, R1, R6 - MUL R8, R7, R11 - ADD R4, R4, R8 - LSL R11, R11, #4 - MOV R1, R3 - SUBS R10, #1 - BNE LOOP + ; Prepare divisor for decimal division + MOV R5, #10 ; R5 = 10 (decimal divisor) + ; Divide current value by 10 to extract least significant digit + ; UDIV performs unsigned division: R3 = R1 / 10 (quotient) + UDIV R3, R1, R5 ; R3 = R1 / 10 (quotient) + + ; Calculate remainder: remainder = dividend - (quotient * divisor) + ; MUL computes: R6 = R3 * R5 = quotient * 10 + MUL R6, R3, R5 ; R6 = R3 * 10 + + ; Calculate remainder: R7 = R1 - R6 = R1 - (quotient * 10) + SUB R7, R1, R6 ; R7 = R1 - R6 (remainder, value 0-9) + + ; Multiply the BCD digit by its position value + ; R8 = R7 * R11 = digit * position_multiplier + MUL R8, R7, R11 ; R8 = digit * position_value + + ; Add the weighted digit to the BCD result accumulator + ADD R4, R4, R8 ; R4 = R4 + R8 (accumulate BCD digit) + + ; Shift position multiplier left by 4 bits for next digit position + ; Units (2^0) -> Tens (2^4) -> Hundreds (2^8) -> Thousands (2^12) + LSL R11, R11, #4 ; R11 = R11 << 4 (next position) + + ; Prepare quotient as input for next iteration + ; The quotient becomes the new dividend for the next digit + MOV R1, R3 ; R1 = R3 (use quotient for next iteration) + + ; Decrement loop counter and set condition flags + SUBS R10, #1 ; R10 = R10 - 1, set flags for branch condition + + ; Branch back to LOOP if counter is not zero + BNE LOOP ; If R10 != 0, continue loop + + ; Step 4: Program termination + ; The BCD result is stored in R4 but never saved to memory STOP - B STOP - -SRR DCD 0x45 + B STOP ; Branch to STOP label (infinite loop) - AREA mydata, DATA, READWRITE -SRC DCD 0x45 - - END \ No newline at end of file +; ======================================================================================== +; Data Section - Hexadecimal Source Value +; ======================================================================================== +; SRR contains the hexadecimal value: 0x45 (69 in decimal) +; This will be converted to BCD format. For value 69: +; 69 / 10 = 6 (quotient) with remainder 9 +; 6 / 10 = 0 (quotient) with remainder 6 +; Result: BCD = 0x69 (6 in tens place, 9 in units place) +; In BCD: 0110 1001 = 0x69 +SRR DCD 0x45 ; Hexadecimal value to be converted to BCD + + AREA mydata, DATA, READWRITE ; Define a read-write data section + +; SRC - Additional storage (seems unused in this program) +SRC DCD 0x45 ; Duplicate of SRR (possibly for testing) + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/LAB4/HEXtoBCD2.asm b/ES/Lab/LAB4/HEXtoBCD2.asm index 10ab8e1..a8fd24b 100644 --- a/ES/Lab/LAB4/HEXtoBCD2.asm +++ b/ES/Lab/LAB4/HEXtoBCD2.asm @@ -9,35 +9,104 @@ __Vectors ENTRY EXPORT Reset_Handler +; ======================================================================================== +; Reset_Handler - Main program execution +; ======================================================================================== +; Algorithm Overview: +; 1. Load the hexadecimal value from memory +; 2. Initialize BCD result accumulator (R4) to 0 +; 3. Initialize digit position multiplier (R11) to 1 (units place) +; 4. Set loop counter for number of BCD digits to process +; 5. In each iteration: +; a. Perform manual division by 10 using repeated subtraction +; b. The remainder (R2) is the current BCD digit (0-9) +; c. The quotient (R3) becomes the input for the next iteration +; d. Multiply digit by position value and add to BCD result +; e. Shift position multiplier left by 4 bits for next digit +; 6. This version uses software division instead of hardware UDIV instruction + Reset_Handler - LDR R0, =SRR - LDR R1, [R0] - MOV R4, #0 - MOV R11, #1 - MOV R10, #3 + ; Step 1: Initialize source pointer and load hexadecimal value + ; R0 points to the hexadecimal value in memory + LDR R0, =SRR ; R0 = address of hexadecimal value + ; Load the hexadecimal value into R1 + LDR R1, [R0] ; R1 = hexadecimal value (e.g., 0x45 = 69 decimal) + + ; Step 2: Initialize BCD conversion variables + ; R4 will accumulate the final BCD result + MOV R4, #0 ; R4 = 0 (BCD result accumulator) + + ; R11 holds the position multiplier for each BCD digit + ; Starts at 1 for units place, shifts left by 4 bits each iteration + MOV R11, #1 ; R11 = 1 (position multiplier) + + ; Set loop counter for 3 BCD digits (maximum for byte values 0-255) + MOV R10, #3 ; R10 = 3 (number of BCD digits to process) + + ; Step 3: Main BCD conversion loop LOOP - MOV R2, R1 - MOV R3, #0 - + ; Prepare current value for division (copy R1 to R2) + MOV R2, R1 ; R2 = R1 (dividend for this iteration) + + ; Initialize quotient accumulator to 0 + MOV R3, #0 ; R3 = 0 (quotient accumulator) + + ; Step 4: Manual division by repeated subtraction DIV_LOOP - CMP R2, #10 - BLT DIV_DONE - SUB R2, R2, #10 - ADD R3, R3, #1 - B DIV_LOOP - + ; Compare dividend with divisor (10) + CMP R2, #10 ; Compare R2 with 10 + + ; If dividend < 10, division is complete + BLT DIV_DONE ; Branch if R2 < 10 + + ; Subtract 10 from dividend (equivalent to R2 = R2 - 10) + SUB R2, R2, #10 ; R2 = R2 - 10 + + ; Increment quotient (equivalent to quotient = quotient + 1) + ADD R3, R3, #1 ; R3 = R3 + 1 + + ; Continue division loop + B DIV_LOOP ; Branch back to DIV_LOOP + + ; Step 5: Division complete - R2 = remainder (0-9), R3 = quotient DIV_DONE - MUL R8, R2, R11 - ADD R4, R4, R8 - LSL R11, R11, #4 - MOV R1, R3 - SUBS R10, R10, #1 - BNE LOOP + ; Multiply the BCD digit (remainder) by its position value + ; R8 = R2 * R11 = digit * position_multiplier + MUL R8, R2, R11 ; R8 = digit * position_value + ; Add the weighted digit to the BCD result accumulator + ADD R4, R4, R8 ; R4 = R4 + R8 (accumulate BCD digit) + + ; Shift position multiplier left by 4 bits for next digit position + ; Units (2^0) -> Tens (2^4) -> Hundreds (2^8) -> Thousands (2^12) + LSL R11, R11, #4 ; R11 = R11 << 4 (next position) + + ; Prepare quotient as input for next iteration + ; The quotient becomes the new dividend for the next digit + MOV R1, R3 ; R1 = R3 (use quotient for next iteration) + + ; Decrement loop counter and set condition flags + SUBS R10, R10, #1 ; R10 = R10 - 1, set flags for branch condition + + ; Branch back to LOOP if counter is not zero + BNE LOOP ; If R10 != 0, continue loop + + ; Step 6: Program termination + ; The BCD result is stored in R4 but never saved to memory STOP - B STOP + B STOP ; Branch to STOP label (infinite loop) -SRR DCD 0x45 +; ======================================================================================== +; Data Section - Hexadecimal Source Value +; ======================================================================================== +; SRR contains the hexadecimal value: 0x45 (69 in decimal) +; This will be converted to BCD format using manual division by repeated subtraction. +; For value 69: +; First iteration: 69 / 10 = 6 (quotient) with remainder 9 +; Second iteration: 6 / 10 = 0 (quotient) with remainder 6 +; Result: BCD = 0x69 (6 in tens place, 9 in units place) +; In BCD: 0110 1001 = 0x69 +SRR DCD 0x45 ; Hexadecimal value to be converted to BCD - END \ No newline at end of file + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/LAB5/FACTORIAL.asm b/ES/Lab/LAB5/FACTORIAL.asm index 6bcbb2a..951cb84 100644 --- a/ES/Lab/LAB5/FACTORIAL.asm +++ b/ES/Lab/LAB5/FACTORIAL.asm @@ -9,29 +9,104 @@ __Vectors ENTRY EXPORT Reset_Handler +; ======================================================================================== +; FACTORIAL.asm - Recursive Factorial Calculation +; ======================================================================================== +; This program demonstrates recursive factorial computation using ARM assembly. +; The factorial of a number n (n!) is the product of all positive integers less +; than or equal to n. This implementation uses the call stack to handle recursion. + +; ======================================================================================== +; Reset_Handler - Main program execution +; ======================================================================================== +; Algorithm Overview: +; 1. Load the input number (5) into R1 +; 2. Call the recursive factorial function (fact) +; 3. Store the result from R0 to memory location 0x10001000 +; 4. Enter infinite loop for program termination + Reset_Handler - LDR R1, =5 - BL fact - LDR R12, =0x10001000 - STR R0, [R12] - + ; Step 1: Load input parameter and call factorial function + ; Load the number for which to calculate factorial (n = 5) + LDR R1, =5 ; R1 = 5 (input parameter for factorial) + + ; Call the recursive factorial function + ; BL (Branch with Link) saves return address in LR and jumps to fact + BL fact ; Call factorial function, result returned in R0 + + ; Step 2: Store the result + ; Load destination address into R12 + LDR R12, =0x10001000 ; R12 = 0x10001000 (result storage address) + + ; Store the factorial result (in R0) to memory + STR R0, [R12] ; Store factorial result to [R12] + + ; Step 3: Program termination STOP - B STOP + B STOP ; Branch to STOP label (infinite loop) + +; ======================================================================================== +; fact - Recursive Factorial Function +; ======================================================================================== +; Recursive algorithm for calculating n! +; Base case: if n <= 1, return 1 +; Recursive case: return n * factorial(n-1) +; Parameters: R1 = n (input) +; Returns: R0 = n! (result) +; Uses: R2 (temporary register for stack operations) fact - CMP R1, #1 - BLE base_case - - PUSH{R1, LR} - SUB R1, R1, #1 - BL fact - - POP{R2, LR} - MUL R0, R0, R2 - BX LR + ; Step 1: Check base case + ; Compare input parameter with 1 + CMP R1, #1 ; Compare R1 with 1 + + ; If R1 <= 1, branch to base_case + BLE base_case ; If R1 <= 1, return 1 + + ; Step 2: Recursive case - prepare for recursive call + ; Save current R1 and LR (return address) on the stack + PUSH{R1, LR} ; Push R1 and LR onto stack + + ; Decrement n for recursive call: R1 = R1 - 1 + SUB R1, R1, #1 ; R1 = R1 - 1 + + ; Recursive call to factorial function + BL fact ; Call fact(R1-1), result in R0 + + ; Step 3: Return from recursion - multiply result + ; Restore saved R1 (original n) and LR from stack + POP{R2, LR} ; Pop LR and original R1 into R2 + + ; Multiply current result (R0) by original n (R2) + ; R0 = R0 * R2 = factorial(n-1) * n + MUL R0, R0, R2 ; R0 = R0 * R2 + + ; Return from function (branch to address in LR) + BX LR ; Return to caller + +; ======================================================================================== +; base_case - Base case handler for factorial +; ======================================================================================== +; When n <= 1, factorial = 1 +; Returns: R0 = 1 base_case - MOV R0, #1 - BX LR + ; Set return value to 1 (base case for factorial) + MOV R0, #1 ; R0 = 1 (factorial of 0 or 1 is 1) - END \ No newline at end of file + ; Return from function + BX LR ; Return to caller + +; ======================================================================================== +; Execution Example: +; ======================================================================================== +; Calculating factorial(5): +; fact(5) -> calls fact(4) -> calls fact(3) -> calls fact(2) -> calls fact(1) +; fact(1) returns 1 +; fact(2) returns 2 * 1 = 2 +; fact(3) returns 3 * 2 = 6 +; fact(4) returns 4 * 6 = 24 +; fact(5) returns 5 * 24 = 120 +; Final result: 120 is stored at memory location 0x10001000 + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab1/Init/DATATRANSFER.asm b/ES/Lab/Lab1/Init/DATATRANSFER.asm index e49fa2a..cf07475 100644 --- a/ES/Lab/Lab1/Init/DATATRANSFER.asm +++ b/ES/Lab/Lab1/Init/DATATRANSFER.asm @@ -1,20 +1,53 @@ - AREA RESET,DATA,READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 ; - DCD Reset_Handler ; - ALIGN - AREA mycode, CODE, READONLY - ENTRY - EXPORT Reset_Handler -Reset_Handler - MOV R0,#10 - MOV R1, #0x10 - MOV R3, #2_1010 - MOV R4, #5_34 - MOV R5, #-8 - +; ======================================================================================== +; DATATRANSFER.asm - Basic Data Transfer Operations Demonstration +; ======================================================================================== +; This program demonstrates various ways to transfer immediate data into ARM registers +; using different number formats (decimal, hexadecimal, binary, and negative values). +; The program loads different types of immediate values into registers R0-R5 to show +; the flexibility of ARM's MOV instruction for data transfer operations. + + 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 starts here + ; Step 1: Load decimal immediate value + ; MOV instruction transfers immediate data (value 10) into register R0 + ; This demonstrates basic decimal number loading + MOV R0,#10 ; R0 = 10 (decimal) + + ; Step 2: Load hexadecimal immediate value + ; Load hexadecimal value 0x10 (16 decimal) into register R1 + ; Shows how hexadecimal values are represented in assembly + MOV R1, #0x10 ; R1 = 0x10 (16 decimal) + + ; Step 3: Load binary immediate value + ; Load binary value 1010 (which equals 10 decimal) into register R3 + ; The '2_' prefix indicates binary notation in ARM assembly + MOV R3, #2_1010 ; R3 = 2_1010 (10 decimal in binary) + + ; Step 4: Load another decimal immediate value + ; Load decimal value 34 into register R4 + ; The '5_' prefix indicates decimal notation (though redundant here) + MOV R4, #5_34 ; R4 = 34 (decimal) + + ; Step 5: Load negative immediate value + ; Load negative value -8 into register R5 + ; Demonstrates how negative numbers are handled in immediate loads + MOV R5, #-8 ; R5 = -8 (negative value) + + ; Step 6: Program termination + ; Create an infinite loop to stop program execution + ; This prevents the processor from executing undefined instructions STOP - B STOP - - END; \ No newline at end of file + B STOP ; Branch to STOP label (infinite loop) + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab1/Init2/DATATRANSFER.asm b/ES/Lab/Lab1/Init2/DATATRANSFER.asm index 29874c6..c82971e 100644 --- a/ES/Lab/Lab1/Init2/DATATRANSFER.asm +++ b/ES/Lab/Lab1/Init2/DATATRANSFER.asm @@ -1,25 +1,60 @@ - AREA RESET,DATA,READONLY - EXPORT __Vectors +; ======================================================================================== +; DATATRANSFER.asm - Memory-Based Data Transfer Operations Demonstration +; ======================================================================================== +; This program demonstrates how to transfer data from memory locations to registers +; using ARM load instructions. It shows the difference between loading an address +; and loading the actual data stored at that address. -__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 +; ======================================================================================== +; This function demonstrates memory data transfer operations: +; 1. Load the address of a data array into a register +; 2. Load the actual data from that memory location +; 3. Load an immediate negative value for comparison Reset_Handler - LDR R0, =SRC ; gives memory address of SRC array - LDR R1, [R0] ; writes from first location of SRC array - MOV R5, -8; - + ; Step 1: Load memory address into register + ; LDR with = syntax loads the address of the SRC array into R0 + ; This gives us a pointer to the beginning of the data array + LDR R0, =SRC ; R0 = address of SRC array + + ; Step 2: Load data from memory location + ; LDR with [] syntax loads the actual data stored at the address in R0 + ; This reads the first 32-bit word from the SRC array (0x12345678) + LDR R1, [R0] ; R1 = data at address in R0 (first element of SRC) + + ; Step 3: Load immediate negative value + ; MOV instruction loads the immediate negative value -8 into R5 + ; This demonstrates mixing memory loads with immediate loads + MOV R5, -8 ; R5 = -8 (immediate negative value) + + ; Step 4: Program termination + ; Create an infinite loop to stop program execution STOP - B STOP - ALIGN; - + B STOP ; Branch to STOP label (infinite loop) + + ALIGN ; Ensure proper alignment for data section + +; ======================================================================================== +; Data Section - Source data array +; ======================================================================================== +; SRC array contains three 32-bit words in hexadecimal format: +; - First word: 0x12345678 (305419896 in decimal) +; - Second word: 0xABCDEF55 (2882400341 in decimal) +; - Third word: 0x55 (85 in decimal) SRC DCD 0x12345678, 0xABCDEF55, 0x55 - END ; + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab2/array_reversal.asm b/ES/Lab/Lab2/array_reversal.asm index 94337b6..4528453 100644 --- a/ES/Lab/Lab2/array_reversal.asm +++ b/ES/Lab/Lab2/array_reversal.asm @@ -1,28 +1,85 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - AREA mycode,CODE,READONLY - ENTRY - EXPORT Reset_Handler - -Reset_Handler - MOV R2, #5 - LDR R0, =SRC - LDR R1, =SRC + 36 -Loop - LDR R3, [R0] - LDR R4, [R1] - STR R3, [R1], #-4 - STR R4, [R0], #4 - SUBS R2, #1 - - BNE Loop +; ======================================================================================== +; 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 - AREA mydate, DATA, READWRITE + 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 \ No newline at end of file + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab2/swap/LOOP.asm b/ES/Lab/Lab2/swap/LOOP.asm index 1a5afde..64a536d 100644 --- a/ES/Lab/Lab2/swap/LOOP.asm +++ b/ES/Lab/Lab2/swap/LOOP.asm @@ -1,26 +1,79 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - AREA mycode,CODE,READONLY - ENTRY - EXPORT Reset_Handler -Reset_Handler - LDR R0, =SRC - LDR R1, =DST - MOV R2,#10 -BACK - LDR R3,[R0],#4; - STR R3,[R1],#04; - SUBS R2,#1 - BNE BACK +; ======================================================================================== +; LOOP.asm - Efficient Array Copy Using Loop Construct +; ======================================================================================== +; This program demonstrates an efficient way to copy data from one array to another +; using a loop construct. This approach is much more scalable and maintainable +; compared to individual copy operations, especially for larger arrays. + 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 array pointers (R0 = SRC address, R1 = DST address) +; 2. Set loop counter R2 to the number of elements to copy (10) +; 3. In each iteration of the loop: +; a. Load element from SRC array using post-increment addressing +; b. Store element to DST array using post-increment addressing +; c. Decrement counter and check if more iterations are needed +; 4. This approach is much more efficient than individual operations for large arrays +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: Initialize loop counter + ; R2 holds the number of elements to copy (10 elements total) + MOV R2,#10 ; R2 = 10 (number of elements to copy) + + ; Step 3: Main copy loop +BACK + ; Load from SRC array and advance pointer + ; LDR R3, [R0], #4 means: R3 = [R0], then R0 = R0 + 4 + LDR R3,[R0],#4 ; Load next element from SRC and advance R0 + + ; Store to DST array and advance pointer + ; STR R3, [R1], #4 means: [R1] = R3, then R1 = R1 + 4 + STR R3,[R1],#04 ; Store element to DST and advance R1 + + ; Decrement loop counter and set condition flags + SUBS R2,#1 ; R2 = R2 - 1 (set flags for branch condition) + + ; Branch back to BACK if R2 is not zero + BNE BACK ; If R2 != 0, continue loop + + ; Step 4: Program termination + ; Create an infinite loop to stop program execution STOP - B STOP - ALIGN + 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 + + 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. The loop will copy all 10 elements from SRC to DST DST DCD 0 - END \ No newline at end of file + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab2/swap/LOOP2.asm b/ES/Lab/Lab2/swap/LOOP2.asm index 78ff4d8..08d6825 100644 --- a/ES/Lab/Lab2/swap/LOOP2.asm +++ b/ES/Lab/Lab2/swap/LOOP2.asm @@ -1,27 +1,83 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - AREA mycode,CODE,READONLY - ENTRY - EXPORT Reset_Handler +; ======================================================================================== +; LOOP2.asm - Array Copy Using Loop with Proper Destination Array Size +; ======================================================================================== +; This program demonstrates the same array copy algorithm as LOOP.asm but with +; two key improvements: +; 1. Uses R12 as the loop counter (R12 is typically used for intra-procedure calls) +; 2. Pre-allocates the correct size for the destination array (10 elements) +; This version is more complete and avoids potential memory issues. + + 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 array pointers (R0 = SRC address, R1 = DST address) +; 2. Set loop counter R12 to the number of elements to copy (10) +; 3. In each iteration of the loop: +; a. Load element from SRC array using post-increment addressing +; b. Store element to DST array using post-increment addressing +; c. Decrement counter and check if more iterations are needed +; 4. This version properly allocates destination array space beforehand Reset_Handler - LDR R0, =SRC - LDR R1, =DST - MOV R12, #10 + ; 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: Initialize loop counter + ; R12 holds the number of elements to copy (10 elements total) + ; Using R12 is a common convention for loop counters in ARM assembly + MOV R12, #10 ; R12 = 10 (number of elements to copy) + + ; Step 3: Main copy loop Loop - LDR R2, [R0], #4 - STR R2, [R1], #4 - SUBS R12, R12, #1 - BNE Loop + ; Load from SRC array and advance pointer + ; LDR R2, [R0], #4 means: R2 = [R0], then R0 = R0 + 4 + LDR R2, [R0], #4 ; Load next element from SRC and advance R0 + ; Store to DST array and advance pointer + ; STR R2, [R1], #4 means: [R1] = R2, then R1 = R1 + 4 + STR R2, [R1], #4 ; Store element to DST and advance R1 + + ; Decrement loop counter and set condition flags + ; SUBS R12, R12, #1 is equivalent to SUBS R12, #1 + SUBS R12, R12, #1 ; R12 = R12 - 1 (set flags for branch condition) + + ; Branch back to Loop if R12 is not zero + BNE Loop ; If R12 != 0, continue loop + + ; Step 4: Program termination + ; Create an infinite loop to stop program execution STOP - B STOP - ALIGN + 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 + + AREA mydate, DATA, READWRITE ; Define a read-write data section + +; DST array - properly pre-allocated with 10 elements (all initialized to 0) +; This ensures there is enough space for all copied elements DST DCD 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - END \ No newline at end of file + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab2/swap/MULTINDEX.asm b/ES/Lab/Lab2/swap/MULTINDEX.asm index 931e4d3..a3a48d4 100644 --- a/ES/Lab/Lab2/swap/MULTINDEX.asm +++ b/ES/Lab/Lab2/swap/MULTINDEX.asm @@ -1,40 +1,99 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - AREA mycode,CODE,READONLY - ENTRY - EXPORT Reset_Handler -Reset_Handler - LDR R0, =SRC - LDR R1, =DST - LDR R2,[R0] - STR R2,[R1] - LDR R3,[R0,#4]! - STR R3,[R1,#4]! - LDR R4,[R0,#4]! - STR R4,[R1,#4]! - LDR R5,[R0,#4]! - STR R5,[R1,#4]! - LDR R6,[R0,#4] - STR R6,[R1,#4] - LDR R7,[R0,#4] - STR R7,[R1,#4] - LDR R8,[R0,#4] - STR R8,[R1,#4] - LDR R9,[R0],#4 - STR R9,[R1],#4 - LDR R10,[R0],#4 - STR R10,[R1],#4 - LDR R11,[R0],#4 - STR R11,[R1],#4 +; ======================================================================================== +; 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 - ALIGN + 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 + + 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 \ No newline at end of file + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab2/swap/SWAP.asm b/ES/Lab/Lab2/swap/SWAP.asm index d931e11..2185e0a 100644 --- a/ES/Lab/Lab2/swap/SWAP.asm +++ b/ES/Lab/Lab2/swap/SWAP.asm @@ -1,40 +1,93 @@ - AREA RESET, DATA, READONLY - EXPORT __Vectors -__Vectors - DCD 0x10001000 - DCD Reset_Handler - ALIGN - AREA mycode,CODE,READONLY - ENTRY - EXPORT Reset_Handler -Reset_Handler - LDR R0, =SRC - LDR R1, =DST - LDR R2, [R0] - STR R2, [R1] - LDR R3, [R0,#4]! - STR R3, [R1,#4]! - LDR R4, [R0,#4]! - STR R4, [R1,#4]! - LDR R5, [R0,#4]! - STR R5, [R1,#4]! - LDR R6, [R0,#4]! - STR R6, [R1,#4]! - LDR R7, [R0,#4]! - STR R7, [R1,#4]! - LDR R8, [R0,#4]! - STR R8, [R1,#4]! - LDR R9, [R0,#4]! - STR R9, [R1,#4]! - LDR R10, [R0,#4]! - STR R10, [R1,#4]! - LDR R11, [R0,#4]! - STR R11, [R1,#4]! +; ======================================================================================== +; 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 - ALIGN + 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 + + 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 \ No newline at end of file + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab3/ADDER.asm b/ES/Lab/Lab3/ADDER.asm index abd58e3..9e5fbff 100644 --- a/ES/Lab/Lab3/ADDER.asm +++ b/ES/Lab/Lab3/ADDER.asm @@ -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 \ No newline at end of file + DCD 0 ; Space for lower 32 bits of sum + + END ; End of the assembly program \ No newline at end of file diff --git a/ES/Lab/Lab3/add128bit.asm b/ES/Lab/Lab3/add128bit.asm index 6ac4247..cb229a1 100644 --- a/ES/Lab/Lab3/add128bit.asm +++ b/ES/Lab/Lab3/add128bit.asm @@ -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 \ No newline at end of file + END ; End of the assembly program \ No newline at end of file