Sie sind auf Seite 1von 8

http://www.experts-exchange.com/Programming/Languages/Assembly/Q_23380113.

html

http://www.syntax-example.com/Code/convert-decimal-number-hexadecimal-1771.aspx
compilar

http://www.experts-exchange.com/Programming/Languages/Assembly/Q_23380113.html

http://www.syntax-example.com/Code/convert-decimal-number-hexadecimal-1771.aspx

Convert from dec to hex and binary by
assembly language
Asked by albusaidi in Assembly Programming Language
Tags: hex, assembly, binary, convert, language
Hi experts,
I dont know whats the error in this code,please help me to convert the numbers

data segment
count DW 8
BIN DW 8 dup(0)
HEX DB '0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F$'
msg0 DB 'Enter number $'
msg1 DB 'Decima1 is $'
msg2 DB 'Hexadecimal is $'
msg3 DB 'Sorry,Wrong number!$'

data ends

working_storage segment stack
dw 100h dup(?)
working_storage ends



code segment
assume cs:code,ds:data,ss:working_storage
extrn putdec:proc,putcrlf:proc,getdec:proc,getchar:proc,putchar:proc

start:
mov ax,data
mov ds,ax


go: mov Dx,offset msg0
mov ah,9h
int 21h
call putcrlf
call getdec
cmp ax,255
jae finish
mov bx,ax

mov cx,2
mov dx,offset msg1
mov ah,9h
int 21h

mov count,8
mov si,0

repeat:
mov ax,bx
mov dx,0
div cx
cmp ax,0
jz done
mov BIN[si],dx
inc si
inc si
dec count
jnz repeat
done: mov si,14
mov BIN[si],dx
do: mov ax,BIN[si]
call putdec
dec si
dec si
jnz do



next: mov ax,bx
mov cx,16
mov si,16
again:div cx
mov HEX[si],dx
cmp ax,0
jnz again
dec si
jnz again

yes:call putcrlf
mov dx,offset msg2
mov ah,9h
INT 21h
mov ax,HEX[si]
call putdec
call putcrlf
jmp go









finish:mov dx,offset msg3
mov ah,9h
INT 21h

mov ax,4c00h
int 21h



code ends
end start


Write a Program to Convert Decimal number to Hexadecimal number.


Code for Program to Convert Decimal number to
Hexadecimal number in Assembly Language
prnstr macro msg
mov ah, 09h
lea dx, msg
int 21h
endm

data segment
buf1 db "Enter a decimal number : $"
buf2 db 0ah, "Invalid Decimal Number...$"
buf3 db 0ah, "Equivalent hexadecimal number is : $"
buf4 db 6
db 0
db 6 dup(0)
multiplier db 0ah
data ends

code segment
assume cs:code, ds:data
start :
mov ax, data
mov ds, ax
mov es, ax

prnstr buf1

mov ah, 0ah
lea dx, buf4
int 21h

mov si, offset buf4 + 2
mov cl, byte ptr [si-1]
mov ch, 00h
subtract :
mov al, byte ptr [si]
cmp al, 30h
jnb cont1
prnstr buf2
jmp stop
cont1 :
cmp al, 3ah
jb cont2
prnstr buf2
jmp stop
cont2 :
sub al, 30h
mov byte ptr [si], al

inc si
loop subtract

mov si, offset buf4 + 2
mov cl, byte ptr [si-1]
mov ch, 00h
mov ax, 0000h
calc :
mul multiplier
mov bl, byte ptr [si]
mov bh, 00h
add ax, bx
inc si
loop calc

mov si, offset buf4 + 2
mov bx, ax
mov dx, 0000h
mov ax, 1000h
convert :
mov cx, 0000h
conv :
cmp bx, ax
jb cont3
sub bx, ax
inc cx
jmp conv
cont3 :
cmp cx, 0ah
jb cont4
add cl, 37h
jmp cont5
cont4 :
add cl, 30h
cont5 :
mov byte ptr [si], cl
inc si
mov cx, 0010h
div cx
cmp ax, 0000h
jnz convert

mov byte ptr [si], '$'
prnstr buf3
prnstr buf4+2
stop :
mov ax, 4c00h
int 21h
code ends
end start




mismo


Write a Program to Convert Decimal number to Hexadecimal number.


Code for Program to Convert Decimal number to
Hexadecimal number in Assembly Language
prnstr macro msg
mov ah, 09h
lea dx, msg
int 21h
endm

data segment
buf1 db "Enter a decimal number : $"
buf2 db 0ah, "Invalid Decimal Number...$"
buf3 db 0ah, "Equivalent hexadecimal number is : $"
buf4 db 6
db 0
db 6 dup(0)
multiplier db 0ah
data ends

code segment
assume cs:code, ds:data
start :
mov ax, data
mov ds, ax
mov es, ax

prnstr buf1

mov ah, 0ah
lea dx, buf4
int 21h

mov si, offset buf4 + 2
mov cl, byte ptr [si-1]
mov ch, 00h
subtract :
mov al, byte ptr [si]
cmp al, 30h
jnb cont1
prnstr buf2
jmp stop
cont1 :
cmp al, 3ah
jb cont2
prnstr buf2
jmp stop
cont2 :
sub al, 30h
mov byte ptr [si], al

inc si
loop subtract

mov si, offset buf4 + 2
mov cl, byte ptr [si-1]
mov ch, 00h
mov ax, 0000h
calc :
mul multiplier
mov bl, byte ptr [si]
mov bh, 00h
add ax, bx
inc si
loop calc

mov si, offset buf4 + 2
mov bx, ax
mov dx, 0000h
mov ax, 1000h
convert :
mov cx, 0000h
conv :
cmp bx, ax
jb cont3
sub bx, ax
inc cx
jmp conv
cont3 :
cmp cx, 0ah
jb cont4
add cl, 37h
jmp cont5
cont4 :
add cl, 30h
cont5 :
mov byte ptr [si], cl
inc si
mov cx, 0010h
div cx
cmp ax, 0000h
jnz convert

mov byte ptr [si], '$'
prnstr buf3
prnstr buf4+2
stop :
mov ax, 4c00h
int 21h
code ends
end start
-*-

Das könnte Ihnen auch gefallen