Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dec to Bin 2

Status
Not open for further replies.

TipGiver

Programmer
Sep 1, 2005
1,863
Hello,

I want some help on converting a signed dec to bin.
I'll use the div and not the idiv. The registers for the div are:
AH AL / 2 . After this the mod is in the AH and AL has either 0 or 1

How can I know that the entered dec by the keyboard (using the INT 21H, AH=07H) is positive or negative? Let's say that i'll enter a 2-digit dec so min values is -99 and max +99. The problem is that i want to get the number; the user can enter e.g +60 or 60 which are the same.


Thank you in advance for any tip, or any other way to do this
 
I could use a little of your help at this.

I want to accept only 0 to 9. It is not working.


MOV AH,01H ;read with echo

NOTNUM: INT 21H ;read it
CMP AL,"0" ;do <ascii> - 30h
JB NOTNUM ;jump if below

CMP AL,"9" ;do <ascii> - 39h
JA NOTNUM ;jump if above


I use the Carry flag (ja and jb) but they both do not work.
Any ideas?
 
I've just type the following into debug and it woked ok (or at least seemed to)

Code:
C:\>debug
-a
0C96:0100 mov   ah, 1
0C96:0102 int   21
0C96:0104 cmp   al, 30
0C96:0106 jb    102
0C96:0108 cmp   al, 39
0C96:010A ja    102
0C96:010C mov   cl, al
0C96:010E mov   ah, 2
0C96:0110 mov   dl, 0d
0C96:0112 int   21
0C96:0114 mov   dl, 0a
0C96:0116 int   21
0C96:0118 mov   dl, cl
0C96:011A int   21
0C96:011C int   20
0C96:011E
-g
gbsgbshsh2
2
Program terminated normally
-

Hope this helps.

[vampire][bat]
 
The program runs without errors, but with the two CMP i wanted to allow typing only numbers. I figured out that it will not work good if i enter ascii from 80h to ffh. These are negative, and there will be problem with the flags after the CMP.

Do you suggest an other way to accept only numbers?
 
I think you are confusing the ASCII representation of numbers and the numbers themselves.

cmp al, 30h

compares the ASCII character for 0 not 0 itself.

If you look at my earlier code you will see how I handled negative numbers.

Hope this helps.

[vampire][bat]
 
Here is my working attempt. E&F requested for a little non-laughing; I'd request much more!
Here it is for anyone interested.

Code:
;Convert a 2-digit Dec number to Bin

.MODEL SMALL
.STACK
.DATA      
  table     DB ?
  msg1      DB "Enter a 2-digit decimal:",13,10,"$"
  msg2      DB 13,10,"Binary (signed):",13,10,"$"
.CODE
.STARTUP
   
MOV AX, @DATA     ; Define the DATA segment
MOV DS,AX         ;
          
MOV DH,"?"        ; Sign is undefined

;;; MESSAGE ;
MOV DX,OFFSET msg1
MOV AH,09H
INT 21H
; MESSAGE ;;;
  
REREADSIGN:     CALL READSIGN
                CALL READNUMBER
STORE1ST:       MOV table[0],AL  ; Treated as tenths   
                CALL READNUMBER     
                MOV table[1],AL  ; Treated as units

MOV BL,0

SHL table[0],1       ; Our number should be... 
ADD BL,table[0]      ; 10 * tenths + 1 * units...
SHL table[0],2       ; but it is binary, or pseudo-decimal
ADD BL,table[0]      ;
ADD BL,table[1]      ;

CMP DH,"-"           ; Check the sign of the...
JE NEGATIVE          ; inputed number
JMP POSITIVE         ;
NEGATIVE:   NEG BL   ;
POSITIVE:            ;

;;; MESSAGE ;
MOV DX,OFFSET msg2
MOV AH,09H
INT 21H
; MESSAGE ;;;

CALL SHOWBINARY

.EXIT 0


          
          
          

READNUMBER PROC NEAR
    ; READ A NUMBER. OUTPUT=AL

    NOTNUM:     CMP DH,"?"
                JE REREADSIGN
                MOV AH,07H
                INT 21H
                AND AL,7FH   ; 0111.1111B -> Remove the sign...
                CMP AL,30H   ; else trouble with flags at CMP...
    JC          NOTNUM       ; negative numbers after 80h     
                CMP AL,39H
    JNC         NOTNUM         
                CALL DISPLAYCHAR
                SUB AL,30H
    RET
READNUMBER ENDP

      
               
SHOWBINARY PROC NEAR
    ; PRINT 1 AND 0 DIGITS. INPUT=BL
    
    MOV AH,02H    ; Function to show the ascii of DL
    MOV CX,08H    ; Loop 8 times (8-bits).

    NEXT:       RCL BL,1    ; Rotate left through Carry Flag
                JNC ISZERO
        
                MOV DL,"1"
                JMP DISP
    ISZERO:     MOV DL,"0"
    DISP:       INT 21H     ; Show it
        
    LOOP        NEXT
    
    RET
SHOWBINARY ENDP
           
           

DISPLAYCHAR PROC NEAR
    ;DISPLAY THE CHAR THAT IS IN DL
    
    MOV AH,02H                 
    MOV DL,AL
    INT 21H 
    
    RET
DISPLAYCHAR ENDP



READSIGN PROC NEAR
    ; SET THE SIGN TO DH
    
    MOV AH,07H                 
    INT 21H
  
    CMP AL,"+"
    JE          SETSIGN
    CMP AL,"-"
    JE          SETSIGN
    
    ; Is number? If yes -> positive
    AND AL,7FH   
    CMP AL,30H   
    JC          ITISNOT          
    CMP AL,39H
    JNC         ITISNOT
    
    MOV DH,"+"               ; Update the sign
    CALL        DISPLAYCHAR
    SUB AL,30H
    JMP         STORE1ST     ; Store it     
    
    SETSIGN:    MOV DH,AL
                CALL DISPLAYCHAR
    RET
    ITISNOT:    JMP REREADSIGN     
READSIGN ENDP
 
You've got the "END" directive missing.

My ancient copy of TASM doesn't like the .EXIT macro.

It also whinges about "SHL table[0],2", will only take 1 as the parameter. (This might be processor related: 8088 vs anything later).

 
I do not use the END. Instead i use the .Exit which is equivalent to:
mov ax,4c00h
int 21h

The SHL gets a number and the times to perform the shift process.
This code runs at least for 80x86
 
Well TASM whinges about END being missing.

It's a directive not a macro.

.EXIT generates code (apparently, except not for me...).

END tells the assembler it's reached the end of the file...

Things may be different these days, but most of the assemblers I use throw a wobbly if END is missing.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top