Search code examples
assemblynumbersradix

Assembly Conversion any base to any base


i'm doing a program to input a number and base, and convert this number to any base. In this initial fase, i get from the user the number and base, the question is.. How i can verify if the number is from that base?

NUMBER_INPUT PROC
   jmp @again   
   @ERROR:                        

   LEA DX, ILLEGAL               
   MOV AH, 9
   INT 21H

   @AGAIN:                  
   print num
   MOV CX, 8                   
   XOR BX, BX               
   MOV AH, 1                 

   @INPUT:                      
     INT 21H                    

     CMP AL, 0DH               
     JE @END                    

     CMP AL, 30H               
     JL @ERROR               

     ; CMP AL, 31H              
     ; JG @ERROR                  

     AND AL, 0FH           
     SHL BX, 1                 
     OR  BL, AL                 
     LOOP @INPUT             
   @END:              
   RET                          
NUMBER_INPUT ENDP


BASE_INPUT PROC
   JMP @AGAIN                   

   @ERROR:               

   LEA DX, ILLEGAL               
   MOV AH, 9
   INT 21H

   @AGAIN:                  
   print base
   MOV CX, 2                     
   XOR DX, DX               
   MOV AH, 1              

   @INPUT:                   
     INT 21H                 

     CMP AL, 0DH              
     JE @END               

     CMP AL, 30H              
     JL @ERROR                          

     AND AL, 0FH                 
     SHL DX, 1             
     OR  DL, AL               
     LOOP @INPUT               

   @END:                   
   RET                        
BASE_INPUT ENDP

Solution

  • only way - in case there are no conventions to express the number base as part of the number representation, making the number base unambiguous, is to detect whether numeric value of any digit is greater or same as the intended number base. But even with specifying the number base, you still want to check for legality of all digits, simply to avoid to produce garbage output. Say, digit "A" representing value "10" may not occur in numbers of radix 10. "8" has no place in number bases 8 and lower. And there may be no "G" (to which we'd attribute numeric value 16) is illegal for hex. You may want to limit your converter to radix 36, as you'd be running out of meaningful symbols for representing digits, as radix 36 could employ 0-9,A-Z.