100 lines
		
	
	
		
			No EOL
		
	
	
		
			4.7 KiB
		
	
	
	
		
			NASM
		
	
	
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			No EOL
		
	
	
		
			4.7 KiB
		
	
	
	
		
			NASM
		
	
	
	
	
	
; ========================================================================================
 | 
						|
; 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
 | 
						|
	; 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
 | 
						|
	; 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
 | 
						|
	; 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)
 | 
						|
 | 
						|
	; 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)
 | 
						|
 | 
						|
	; OR the new 4-bit value into the result
 | 
						|
	ORR R1, R1, R2                    ; R1 = R1 | R2 (insert new 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
 | 
						|
 | 
						|
	; 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 |