Converting ASCII Strings to Integers

;;; Directives PRESERVE8 THUMB ;;; Equates INITIAL_MSP EQU 0x20001000 ; Initial Main Stack Pointer Value Allocating ; 1000 bytes to the stack as it grows down. ; Vector Table Mapped to Address 0 at Reset ; Linker requires __Vectors to be exported AREA RESET, DATA, READONLY EXPORT __Vectors __Vectors DCD INITIAL_MSP ; stack pointer value when stack is empty DCD Reset_Handler ; reset vector ALIGN ;The program ; Linker requires Reset_Handler AREA MYCODE, CODE, READONLY ENTRY EXPORT Reset_Handler ALIGN Reset_Handler PROC ; test the subroutine LDR R9, =0xc0010010 ; load a number to convert BL int_to_ascii ; convert it to a string endloop ; end program in an infinite loop B endloop ENDP ;-------------------------------------------------- ; int_to_ascii function ; converts the integer in R9 to a null-terminated ; string ;-------------------------------------------------- int_to_ascii PROC STMFD SP!, {R4, LR} LDR R10, =0x20000043; load address at end of buffer LDR R0, =0 STRB R0, [R10], #-1; save a zero as the end of the string, move one byte left MOV R1, R9; save the number to convert in R1 CMP R1, #0; see if the number is negative BGE start; if not, skip NEG R1, R1; else, turn number to positive start LDR R2, =10; load a 10 in R2 to make divisions loop UDIV R3, R1, R2; divide number by 10 MUL R4, R3, R2; get remainder by multiplying the result by 10 SUB R4, R1, R4; and subtracting it from the original number ADD R4, R4, #'0'; convert remainder digit to ASCII STRB R4, [R10], #-1; save digit in the string, move one byte left MOVS R1, R3; use the division result as the number for the next cycle BNE loop; continue while the number is not zero CMP R9, #0; see if the original number was negative BGE nosign; if not, do not insert sign LDR R0, ='-'; else, save a sign on the string STRB R0, [R10] B done; go to the end of subroutine nosign LDR R0, =' '; save space on the string STRB R0, [R10] done LDMFD SP!, {R4, PC} ; return ENDP ; Data area AREA MyData, DATA, READWRITE EXPORT buffer SPACE 30 ; padding to make buffer start at 0x2000_0030 buffer SPACE 20 ; allocate space to save converted string END