Friday 3 February 2012

Binary to Hexadecimal converter-Masm program


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm

multiply macro num1
mov ax,0001
mov dh,02
mov dl,00
l10:
cmp dl,num1
jz l9
mul dh
inc dl
jmp l10
l9:
endm

data segment
num dw ?
num1 db 0

array db 10 dup(0)

cr equ 0dh
msg1 db "Enter a binary number$"
msg2 db 0ah,0dh,"Hex Equivalant$"
a db "A$"
b db "B$"
c db "C$"
d db "D$"
e db "E$"
f db "F$"
msg4 db 0ah,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov cl,00


printmsg msg1
lea si,array
mov bx,si

printmsg msg4

l2:mov ah,01h
int 21h
cmp al,cr
je l1
sub al,30h
mov [si],al
inc si
inc cl
jmp l2

l1:
printmsg msg2
mov bx,0000
mov si,offset array

l5:

cmp cl,00
jne l6
jmp l7
l6:

multiply cl
mov dh,[si]
mul dh
add bx,ax
inc si
dec cl
jmp l5

l7:
printmsg msg4

mov ax,bx

mov dh,02
 div dh
mov ah,00

mov cl,00
mov si,offset array
mov dh,16
p1:

div dh

mov [si],ah
inc si
inc cl
cmp al,00
je p2
mov ah,00
jmp p1
p2:
dec si

b1:
mov al,[si]
cmp al,10
je a1
jmp a2
a1:
printmsg a
dec si
dec cl
r4:cmp cl,00
jne b1

jmp t1

a2:cmp al,11
je c1
jmp a3
c1:
printmsg b
dec si
dec cl
r3:cmp cl,00
jne r4

jmp t1

a3:cmp al,12
je c2
jmp a4
c2:printmsg c
dec si
dec cl
r2:cmp cl,00
jne r3

jmp t1

a4:
cmp al,13
je c3
jmp a5
c3:printmsg d
dec si
dec cl
r1:cmp cl,00
jne r2

jmp t1

a5:cmp al,14
je c4
jmp a6
c4:printmsg e
dec si
dec cl
z2:cmp cl,00
jne r1

jmp t1

a6:cmp al,15
je c5
jmp a7
c5:printmsg f
dec si
dec cl
z1:cmp cl,00
jne z2

jmp t1

a7:
printnum al
dec si
dec cl
cmp cl,00
jne z1



jmp t1

t1:mov ah,4ch
int 21h
code ends
end start






0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More