;; GetDec--translate a decimal number read from the keyboard ;; into its equivalent binary number ;; ;; Input: ;; none ;; ;; Output: ;; If there is no error, the carry flag is clear and we have: ;; ax = value translated ;; If an error occurs, carry flag is set, ax is meaningless, and ;; ;; The routine skips over leading white space--blanks and tabs ;; it also handles backspace erasures unless the number gets too long ;; ;; 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. ;; INCLUDE PCMAC.INC .MODEL SMALL .DATA BUFFLEN EQU 20 InBuff DB BUFFLEN+1 DUP (?) .CODE PUBLIC GetDec EXTRN Dec2Bin : NEAR GetDec PROC push ds ; Save registers to be used push es push cx push dx ; Used by _PutCh push si push di pushf ; ...and flags cld ; Set direction flag upwards mov ax, @data mov ds, ax mov es, ax lea di, InBuff ; The sign--Guess + = 0 mov cx, BUFFLEN ; Character count SkipWhiteSpace: _GetCh cmp al, ' ' je SkipWhiteSpace cmp al, 8 ; Backspace jne TryTab DoBack: _PutCh ' ', 8 ; Erase char jmp SkipWhiteSpace TryTab: cmp al, 9 ; Tab je SkipWhiteSpace cmp al, '+' ; Number has been found; check for sign je StoreIt cmp al, '-' jne CheckDig ; Must be a digit jmp StoreIt NextDig: _GetCh cmp al, 8 ; Backspace jne CheckDig inc cx dec di cmp cx, BUFFLEN je DoBack ; We've erased the whole number _PutCh ' ', 8 jmp NextDig CheckDig: cmp al, '0' ; Is the character a digit? jl Done cmp al, '9' jg Done StoreIt: stosb loop NextDig Error: popf stc ; Set Carry to indicate error jmp Restore ; Restore regs and exit Done: stosb cmp al, 13 ; If carriage return ... jne NoLF _PutCh 10 ; ... also display line feed NoLF: popf lea si, InBuff call Dec2Bin Restore: pop di pop si pop dx pop cx pop es pop ds ret GetDec ENDP END