;; Dec2Bin--translate a decimal character string into its equivalent ;; binary number ;; ;; Input: ;; ds:si points to the first character of the string ;; ;; Output: ;; If there is no error, the carry flag is clear and we have: ;; ax = value translated ;; ds:si points to the first character after the end of the number ;; If an error occurs, carry flag is set, ax is meaningless, and ;; ds:si points to the character after which the error occurred ;; ;; The routine skips over leading white space--blanks and tabs ;; ;; Errors: Overflow (number can't be expressed as 16 bit 2's complement) ;; ;; Library source text from "Assembly Language for the IBM PC Family" by ;; William B. Jones, (c) Copyright 1992, 1997, Scott/Jones Inc. ;; .MODEL SMALL .CODE PUBLIC Dec2Bin Dec2Bin PROC push bx ; Save registers to be used push cx push dx push di pushf ; ...and flags cld ; Set direction flag upwards sub bx, bx ; Accumulated value--start at 0 push bx ; The sign--Guess + = 0 mov cx, 10 ; The multiplier sub di, di ; Count of actual digits found SkipWhiteSpace: lodsb ; Next char --> al and inc si cmp al, ' ' je SkipWhiteSpace cmp al, 9 ; Tab je SkipWhiteSpace cmp al, '+' ; Number has been found; check for sign je NextDig ; Simply skip over a '+' cmp al, '-' jne CheckDig ; Must be a digit pop ax ; pop + = 0, push - = -1 dec ax push ax NextDig: lodsb CheckDig: sub al, '0' ; Is the character a digit? jl Done cmp al, 9 ; NOTE: no quotes around the 9! jg Done inc di ; Count a digit xchg ax, bx ; bx := bx * 10 + (ASCII digit - '0') mul cx sub bh, bh add bx, ax ; multiply set dx to high digits of product adc dx, 0 ; pick up possible overflow into dx test dx, dx ; Test for overflow into dx jz NextDig Error: pop ax ; Pop off sign Error1: popf stc ; Set Carry to indicate error jmp Restore ; Restore regs and exit Done: dec si ; Get back to terminator character test di, di jz Error pop ax ; Get Sign Back test ax, ax jz Positive cmp bx, 8000h ; Now check for overflow into the sign bit je OK ; We actually read -32768 ja Error1 ; Number already neg, meaning overflow neg bx jmp OK Positive: test bh, 80h jnz Error1 ; Overflow OK: mov ax, bx ; result --> ax popf clc ; Clear Carry Flag Restore: pop di pop dx pop cx pop bx ret Dec2Bin ENDP END