Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to convert deciml to hex

Status
Not open for further replies.

yashwanth450

Newbie level 4
Joined
May 10, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,318
plz help me urgent
how to convert decimal value to hex value in 8051
plz send me the assembly code
 

I found this solution from answers.yahoo.com for you posted by jaden404 "Always give credit to the one who desirve it". I hope it will help.

=========================================================================================================

Hex has 16 values, 0 - 15, a - f = 10 through 15.

Let's take a number, say 'ab'. a, being the first digit, gets multiplied by 16. then b is added. In her words, it's as simple as multiplying the first by 16 and adding on the second.

Think of it like this (in general terms):

* Consider a number (say 15482) in decimal system
* This is equivalent to (1 * 10000) + (5 * 1000) + (4 * 100) + (8 * 10) + 2
* This is equivalent to (1 * 10^4) + (5 * 10^3) + (4 * 10^2) + (8 * 10^1) + (2 * 10^0)
where ^ is the power-of operator.

* Now consider a number (say 12F3B) in hexadecimal system
* Using similar logic, this is equivalent to (1 * 16^4) + (2 * 16^3) + (F * 16^2) + (3 * 16^1) + (B * 16^0)

Now all you have to do is compute 16^4, 16^3 etc. and also take F=15 and B=11 and there you have it... the answer in decimal

Note that you can use this logic to convert a number from *ANY* base into decimal.

Does this make sense?

Now, for some code (and I know that this is a different platform, but)...

; decibin - get decimal digits from keyboard
; and convert them to binary in BX register

.model tiny

.code
.startup

;- - - - - - - - - - - -

; main program loop

start:
call read_num
call print_crlf
call print_hex
call print_crlf
jmp start ; loop endlessly


; ****************************************…
; print_crlf
; prints out a carriage return and linefeed
; ****************************************…

print_crlf proc

mov ah, 2h ; print character function
mov dl, 0dh ; carriage return
int 21h ; print it
mov dl, 0ah ; linefeed
int 21h ; print it

ret
print_crlf endp



; ****************************************…
; read_num
; reads a number from keyboard input
; and stores it in BX in binary
; ****************************************…

read_num proc

mov bx, 0h ; clear bx for number

;
; get digit from keyboard and
; convert it to binary
;
newchar:
mov ah, 1h ; keyboard input function
int 21h ; execute ^ with dos
sub al, 30h ; ascii to binary
jl endinpt ; jump if < 0
cmp al, 9d ; is it > 9d?
jg endinpt ; yes, not decimal digit
cbw ; byte in al to word in ax

;
; digit is now in AX
; multiply number in BX by 10d
;
xchg ax, bx ; trade digit and number
mov cx, 10d ; put 10d in CX
mul cx ; number times 10d
xchg ax, bx ; trade number and digit

;
; add digit in AX to number in BX
;
add bx, ax ; add digit to number
jmp newchar ; get next digit

;
; check to see what they entered
; if nothing, exit
; otherwise, return from process
;
endinpt:
cmp bx, 0h ; is it 0?
jne return ; nope, return from process
.exit ; yup, exit program

return:

ret
read_num endp



; ****************************************…
; print_hex
; prints in hex to the screen the contents of
; BX
; ****************************************…

print_hex proc

mov ch, 4h ; number of digits to print

; start with first digit in BX
; register

rotate:
mov cl, 4h ; set count to 4 bits
rol bx, cl ; left digit to right

; convert to number

mov al, bl ; mov to AL
and al, 0fh ; mask off left digit
add al, 30h ; convert hex to ascii
cmp al, 3ah ; is it > 9?
jl printit ; jump if digit =0 to 9
add al, 27h ; digit is A to F

; print the digit in
; the AL register

printit:
mov dl, al ; put ASCII char in DL
mov ah, 2h ; display output function
int 21h ; call DOS
dec ch ; done 4 digits?
jnz rotate ; not yet

; fallthrough and return

ret
print_hex endp


;- - - - - - - - - - - -

end
 

Dear my friend

I am attached two subroutine with assembly to convert bin to BCD one for two digit (0-255)

the other is four digit (0-65535)

Best Regards
MedTronic
 

Attachments

  • BIN2BCD.pdf
    14.3 KB · Views: 74
  • BIN2BCD2.pdf
    14.3 KB · Views: 62

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top