Advertisement
Danger_Asif_2018

DEC TO BIN IN ASM

Jan 13th, 2021
1,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .MODEL TINY
  2.  
  3. .CODE
  4.  
  5. MAIN PROC
  6.    
  7.     CALL INDEC
  8.     PUSH AX
  9.     CALL NEWLINE
  10.     CALL NEWLINE
  11.     POP AX
  12.     CALL OUTDEC
  13.    
  14.    
  15. EXIT:
  16.     MOV AH,4CH
  17.     INT 21H
  18.    
  19. MAIN ENDP
  20.      
  21. CALL INDEC
  22. CALL OUTDEC
  23.  
  24.  
  25. NEWLINE PROC
  26.     MOV AH,2
  27.     MOV DL,10
  28.     INT 21H
  29.     MOV DL,13
  30.     INT 21H
  31.     RET
  32. NEWLINE ENDP  
  33.  
  34. OUTDEC PROC
  35.  
  36.        PUSH AX              ;save registers
  37.        PUSH BX
  38.        PUSH CX
  39.        PUSH DX  
  40.  
  41.        
  42.        
  43. @END_IF1:
  44. ;get decimal digits
  45.        XOR CX,CX            ;CX counts digits
  46.        MOV BX,2D           ;BX has divisor
  47. @REPEAT1:
  48.        XOR DX,DX            ;prepare high word of dividend
  49.        DIV BX               ;AX = quotient, DX = remainder
  50.        PUSH DX              ;save ramainder on stack
  51.        INC CX               ;count = count + 1
  52. ;until
  53.        OR AX,AX             ;quotient = 0?              
  54.        JNE @REPEAT1         ;no keep going
  55. ;converts digits to characters and print
  56.        MOV AH,2             ;print char function  
  57. ;for count times do
  58. @PRINT_LOOP:
  59.        POP DX               ;digit in DL
  60.        OR DL,30H            ;convert to character
  61.        INT 21H              ;print digit
  62.        LOOP @PRINT_LOOP     ;loop until done
  63. ;end_for
  64.        POP DX               ;restore registers
  65.        POP CX
  66.        POP BX  
  67.        POP AX  
  68.        RET
  69. OUTDEC ENDP    
  70.                  
  71.                  
  72.                  
  73. INDEC PROC
  74.  
  75.       PUSH BX                 ;save registers used
  76.       PUSH CX
  77.       PUSH DX  
  78. ;print prompt
  79. @BEGIN:
  80.       MOV AH,2
  81.       MOV DL,'?'
  82.       INT 21H                 ;print '?'        
  83. ;total=0
  84.       XOR BX,BX               ;BX holds total
  85. ;negative = false
  86.       XOR CX,CX               ;CX holds sign
  87. ;read a character
  88.       MOV AH,1                
  89.       INT 21H                 ;character in AL    
  90. ;end_case
  91. @REPEAT2:
  92. ;if character is between '0' and '9'
  93.       CMP AL,'0'              ;character >= '0'?
  94.       JNGE @NOT_DIGIT         ;illegal character
  95.       CMP AL,'9'              ;character <= '9'?
  96.       JNLE @NOT_DIGIT         ;no, illegal character
  97. ;then convert character to a digit
  98.       AND AX,000FH            ;convert to digit
  99.       PUSH AX                 ;save on stack
  100. ;total = total * 10 + digit
  101.       MOV AX,10               ;get 10
  102.       MUL BX                  ;AX = total * 10
  103.       POP BX                  ;retrieve digit
  104.       ADD BX,AX               ;total = total * 10 + digit
  105. ;read a character
  106.       MOV AH,1                
  107.       INT 21H
  108.       CMP AL,0DH              ;carriage return?
  109.       JNE @REPEAT2            ;no, keep going
  110. ;until CR
  111.       MOV AX,BX               ;store number in AX
  112. ;if negative
  113.       OR CX,CX                ;negative number
  114.       JE @EXIT                ;no, exit
  115. ;then
  116.       NEG AX                  ;yes, negate
  117. ;end_if
  118. @EXIT:
  119.       POP DX                  ;restore registers
  120.       POP CX
  121.       POP BX
  122.       RET                     ;and return
  123. ;here if illegal character entered
  124. @NOT_DIGIT:
  125.       MOV AH,2                ;move cursor to a new line
  126.       MOV DL,0DH
  127.       INT 21H
  128.       MOV DL,0AH
  129.       INT 21H
  130.       JMP @BEGIN              ;go to beginning
  131. INDEC ENDP
  132.  
  133. END MAIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement