An assembly program to accept a decimal number and print it’s 2′s complement binary and hexadecimal – IGNOU MCA Assignment 2013

By | April 15, 2014

MASTER OF COMPUTER APPLICATIONS
Course Code : MCSL-017
Course Title : C and Assembly Language Programming
Assignment Number : MCA(1)/L017/Assign/2013 

 

Write an assembly language program to accept a decimal number and display it’s two’s complement representation in binary and hexadecimal formats

 

DATA SEGMENT
    DIGIT1 DB ?
    DIGIT2 DB ?
    HEXNUM DB ?
    HEXADEC DB 3 DUP (‘$’)
    TWOSCOMP DB 10 DUP (‘$’)
    MSG1 DB “ENTER NUMBER : $”
    MSG2 DB 10,13,”HEXADECIMAL FORM IS : $”
    MSG3 DB 10,13,”TWO’S COMPLEMENT FORM IS : $”
DATA ENDS

CODE SEGMENT
        ASSUME DS:DATA,CS:CODE
START:       
    MOV AX,DATA
    MOV DS,AX
   
    LEA DX,MSG1
    MOV AH,9
    INT 21H
   
    MOV AH,1
    INT 21H
    SUB AL,30H
    MOV DIGIT1,AL
   
    MOV AH,1
    INT 21H
    SUB AL,30H
    MOV DIGIT2,AL
   
    MOV AH,0
    MOV AL,DIGIT1
   
    MOV CL,10
    MUL CL
   
    ADD AL,DIGIT2
    MOV HEXNUM,AL
   
    NOT AL
    ADD AL,1

    LEA SI,TWOSCOMP
    CALL HEX2BIN
   
    MOV AL,HEXNUM
    MOV BL,AL
    AND AL,0F0H
    AND BL,0FH
     
    MOV CL,04H
    ROL AL,CL
     
    LEA DI,HEXADEC
    MOV [DI],AL
    MOV [DI+1],BL
   
       LEA SI,HEXADEC        
       MOV CL,2
       MOV CH,0       
CHECK:
       CMP [SI],9
       JG ALPHA
   
NUM:   ADD [SI],30H
       JMP DONE
      
ALPHA: ADD [SI],37H 
            
DONE:       
       INC SI
       LOOP CHECK
   
      
    LEA DX,MSG2
    MOV AH,9
    INT 21H
      
    LEA DX,HEXADEC
    MOV AH,9
    INT 21H
   
    LEA DX,MSG3
    MOV AH,9
    INT 21H
      
    LEA DX,TWOSCOMP
    MOV AH,9
    INT 21H
            
    MOV AH,4CH
    INT 21H        
CODE ENDS  

HEX2BIN PROC NEAR
    MOV CX,0
    MOV BX,2
   
LOOP1: MOV DX,0
       DIV BX
       ADD DL,30H
       PUSH DX
       INC CX
       CMP AX,1
       JG LOOP1
     
       ADD AL,30H
       MOV [SI],AL
     
LOOP2: POP AX
       INC SI
       MOV [SI],AL
       LOOP LOOP2
       RET
HEX2BIN ENDP           
   
END START

 

program code :

  1. DATA SEGMENT
  2.     DIGIT1 DB ?
  3.     DIGIT2 DB ?
  4.     HEXNUM DB ?
  5.     HEXADEC DB 3 DUP ('$')
  6.     TWOSCOMP DB 10 DUP ('$')
  7.     MSG1 DB "ENTER NUMBER : $"
  8.     MSG2 DB 10,13,"HEXADECIMAL FORM IS : $"
  9.     MSG3 DB 10,13,"TWO'S COMPLEMENT FORM IS : $"
  10. DATA ENDS
  11. CODE SEGMENT
  12.         ASSUME DS:DATA,CS:CODE
  13. START:       
  14.     MOV AX,DATA
  15.     MOV DS,AX
  16.    
  17.     LEA DX,MSG1
  18.     MOV AH,9
  19.     INT 21H
  20.    
  21.     MOV AH,1
  22.     INT 21H
  23.     SUB AL,30H
  24.     MOV DIGIT1,AL
  25.    
  26.     MOV AH,1
  27.     INT 21H
  28.     SUB AL,30H
  29.     MOV DIGIT2,AL
  30.    
  31.     MOV AH,0
  32.     MOV AL,DIGIT1
  33.    
  34.     MOV CL,10
  35.     MUL CL
  36.    
  37.     ADD AL,DIGIT2
  38.     MOV HEXNUM,AL
  39.    
  40.     NOT AL
  41.     ADD AL,1
  42.     LEA SI,TWOSCOMP
  43.     CALL HEX2BIN
  44.    
  45.     MOV AL,HEXNUM
  46.     MOV BL,AL
  47.     AND AL,0F0H
  48.     AND BL,0FH
  49.      
  50.     MOV CL,04H
  51.     ROL AL,CL
  52.      
  53.     LEA DI,HEXADEC
  54.     MOV [DI],AL
  55.     MOV [DI+1],BL
  56.    
  57.        LEA SI,HEXADEC        
  58.        MOV CL,2
  59.        MOV CH,0       
  60. CHECK:
  61.        CMP [SI],9
  62.        JG ALPHA
  63.    
  64. NUM:   ADD [SI],30H
  65.        JMP DONE
  66.       
  67. ALPHA: ADD [SI],37H 
  68.             
  69. DONE:       
  70.        INC SI
  71.        LOOP CHECK
  72.    
  73.       
  74.     LEA DX,MSG2
  75.     MOV AH,9
  76.     INT 21H
  77.       
  78.     LEA DX,HEXADEC
  79.     MOV AH,9
  80.     INT 21H
  81.    
  82.     LEA DX,MSG3
  83.     MOV AH,9
  84.     INT 21H
  85.       
  86.     LEA DX,TWOSCOMP
  87.     MOV AH,9
  88.     INT 21H
  89.             
  90.     MOV AH,4CH
  91.     INT 21H        
  92. CODE ENDS  
  93. HEX2BIN PROC NEAR
  94.     MOV CX,0
  95.     MOV BX,2
  96.    
  97. LOOP1: MOV DX,0
  98.        DIV BX
  99.        ADD DL,30H
  100.        PUSH DX
  101.        INC CX
  102.        CMP AX,1
  103.        JG LOOP1
  104.      
  105.        ADD AL,30H
  106.        MOV [SI],AL
  107.      
  108. LOOP2: POP AX
  109.        INC SI
  110.        MOV [SI],AL
  111.        LOOP LOOP2
  112.        RET
  113. HEX2BIN ENDP           
  114.    
  115. END START

Screen shots :-

Asm_program_Dec_to_2sCompBin_Hexa

Outputs :-

Asm_program_Dec_to_2sCompBin_Hexa_Output1

Asm_program_Dec_to_2sCompBin_Hexa_Output2

Leave a Reply