102 lines
No EOL
5.1 KiB
NASM
102 lines
No EOL
5.1 KiB
NASM
; ========================================================================================
|
|
; 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
|
|
; 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
|
|
; 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 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)
|
|
|
|
; 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
|
|
|
|
; 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 |