You are on page 1of 6

Question

Write 8086 ALP to convert:

i.  Four digit hexadecimal number to decimal number

ii. Four digit octal number to decimal number

Answer
Step 1

For both the conversions, the basic algorithms remain the same

For hexa, we need to divide the number by 16, and for octal by 8, to and proceed with the
conversion

Step 2

//i) Code

.model small

.stack 100h

.data

msg1 db 10,13,'ENTER A HEX DIGIT:$'

msg2 db 10,13,'IN DECIMAL IS IT:$'

msg3 db 10,13,'DO YOU WANT TO DO IT AGAIN?$'

msg4 db 10,13,'ILLEGAL CHARACTER- ENTER 0-9 OR A-F:$'

.code   

again:

   mov ax,@data

   mov ds,ax

   lea dx,msg1

   mov ah,9

   int 21h

   

   mov ah,1

   int 21h

   

   mov bl,al 

   

   

   jmp go

   

   

go:   

   

   cmp bl,'9'

   ja hex

   jb num

   je num

   

       

 hex:

    cmp bl,'F'

    ja illegal  

    

    

   lea dx,msg2 

   mov ah,9

   int 21h

   

    mov dl,49d

    mov ah,2

    int 21h

    

    sub bl,17d

    mov dl,bl

    mov ah,2

    int 21h

    

    jmp inp

    

     

    

    

 inp:

    

    lea dx,msg3

    mov ah,9

    int 21h

    

    mov ah,1

    int 21h

    

    mov cl,al

    cmp cl,'y'

    je again

    cmp cl,'Y'

    je again

    jmp exit

    

   

 num:

   

   cmp bl,'0'

   jb illegal

   

   lea dx,msg2 

   mov ah,9

   int 21h 

   

    

   mov dl,bl

   mov ah,2

   int 21h

   

   jmp inp

   

   

   

   

 illegal:

g
   

       lea dx,msg4

       mov ah,9

       int 21h

       

       mov ah,1

       int 21h

       

       mov bl,al 

       

       jmp go

       

   

exit:

Step 3

ii) code

 .MODEL SMALL

 .STACK 100H

 .DATA

  d1 dw 16

 .CODE

  MAIN PROC FAR

   MOV AX,

 @DATA

  MOV DS,

 AX

;load the value stored;

in variable d1

 mov ax,

 d1

;convert the value to octal;

print the value

 CALL PRINT

;interrupt to exit

; p
 MOV AH,

 4CH INT 21H

 MAIN ENDP

  PRINT PROC

;initialize count

 mov cx,

 0 mov dx, 0 label1:;

if

 ax is zero

  cmp ax,

  0 je print1

;initialize bx to 8 mov bx, 8

;divide it by 8;

to convert it to octal

 div bx

;push it in the stack

 push dx

;increment the count

 inc cx

;set dx to 0

 xor dx,

 dx

  jmp label1

   print1:

;check if count

;is greater than zero

 cmp cx,

 0 je exit

;pop the top of stack

 pop dx

;add 48 so that it

;represents the ASCII


;value of digits

 add dx,

,
 48

;interrupt to print a

;character

 mov ah,

 02h int 21h

;decrease the count

 dec cx

  jmp print1

   exit : ret

     PRINT ENDP

      END MAIN

upvote pl

You might also like