; ======================================================================================== ; 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 ; 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 ; 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