.NLIST TOC,SYM .TITLE .DD2CT .SBTTL ULBLIB 035 - Convert ASCII to binary .IDENT \V01.00\ .PSECT .LIBC. .ENABL LC,GBL ; Copyright (c) 1998 by Mentec, Inc., Nashua, NH. ; All rights reserved ; ; This software is furnished under a license for use only on a ; single computer system and may be copied only with the ; inclusion of the above copyright notice. This software, or ; any other copies thereof, may not be provided or otherwise ; made available to any other person except for use on such ; system and to one who agrees to these license terms. Title ; to and ownership of the software shall at all times remain ; in Mentec, Inc. ; ; The information in this document is subject to change without ; notice and should not be construed as a commitment by Digital ; Equipment Corporation, or Mentec, Inc. ; ; Digital and Mentec assume no responsibility for the use or ; reliability of its software on equipment which is not supplied ; by Digital or Mentec, and listed in the Software Product ; Description. ;+ ; .DD2CT ; Converts a decimal number to two binary words. Trailing decimal point ; is allowed. ;+ ; .OD2CT ; Converts an octal or decimal number to two binary words. Radix is ; controlled by the presence or absence or a trailing decimal point. ; ; Both routines allow leading "+" or "-", and produce a two's complement ; negative for the latter. ; ; R3 -> two word field to hold converted number ; .WORD high order ; .WORD low order ; R4 = size of input string to convert ; R5 -> character string ; ; JSR PC,.DD2CT ; or ; JSR PC,.OD2CT ; ; C-bit = 0 => successful conversion ; ; C-bit = 1 => illegal character in string ;- .ENABL LSB .DD2CT:: JSR PC,$SAVAL ;Save registers MOV #10.,R1 ;Set up for decimal radix only BR 1$ .OD2CT:: JSR PC,$SAVAL ;Save registers MOV #8.,R1 ;Set up for octal radix 1$: MOV R5,R2 ;Copy the string pointer ADD R4,R2 ;Point to end of string CMPB -(R2),#'. ;Is there a trailing decimal point? BNE 2$ ;Branch if not MOV #10.,R1 ;Else set decimal radix DEC R4 ;Remove "." from string 2$: CLR -(SP) ;Clear negative flag CMPB @R5,#'+ ;Is there a leading "+"? BEQ 3$ ;Branch if so CMPB @R5,#'- ;How about a leading "-"? BNE 4$ ;Branch if not INC @SP ;Set negative flag 3$: INC R5 ;Get rid of leading sign DEC R4 ;Decrement the character count 4$: CLR (R3)+ ;Zero high order 16 bits CLR @R3 ;Zero low order 16 bits 5$: MOVB (R5)+,R2 ;Get next character SUB #'0,R2 ;Convert digit to value BLO 9$ ;Not a legal digit character CMP R2,R1 ;Last chance to be in range BHIS 9$ ;Definitely not legal ASL @R3 ;Multiply current binary number by 2 ROL -(R3) ;Use full 32 bit accuracy MOV (R3)+,-(SP) ;Save high order bits MOV @R3,-(SP) ;Save low order bits ASL @R3 ;Multiply again by 2 ROL -(R3) ;Keep those bits moving ASL 2(R3) ;Make total multiplication by 8 ROL (R3)+ ;Now have (new number) = (old number)*8 CMP R1,#10. ;Check what radix we're in BEQ 6$ ;Equal if decimal CMP (SP)+,(SP)+ ;Octal - throw away (old number)*2 BR 7$ 6$: ADD (SP)+,@R3 ;Add back (old number)*2 ADC -(R3) ;Add any overflow to high order ADD (SP)+,(R3)+ ;Now have (new number) = (old number)*10 7$: ADD R2,@R3 ;Add this digit to make new total ADC -2(R3) ;Make sure overflow is considered DEC R4 ;Count characters BNE 5$ ;Go get next digit TST (SP)+ ;Check negative flag BEQ 8$ ;Positive NEG -(R3) ;Complement high order NEG 2(R3) ;Complement low order SBC @R3 ;And propagate the carry CLC ;Set success 8$: RETURN ; Come to here on an illegal character 9$: TST (SP)+ ;Clean the stack SEC ;Set the carry RETURN .DSABL LSB .END