| ??? 08/18/04 08:50 Read: times Msg Score: 0 -1 Gimmee Code +1 Underrated |
#76086 - RE: convert binary number to Hexa Responding to: ???'s previous message |
Giri,
As you might expect, radix conversion is a fundamental problem that has many possible solutions. Not surprisingly, there is a great deal of information available on the web generally and at this site in particular. In any event, carefully framing your question will both avoid prickly responses and get you at least half way to finding the answer yourself. Presumably your binary number is the internal representation in the CPU and the hexadecimal number you require is to be emitted as ASCII characters. I am not going to give you an answer here, but this is a cracking good excuse for me to post a couple of code snippets (in a Keil shell, but functionally all assembler). You can put these together to do your own conversion function. There are MUCH more straightforward ways of implementing these functions, but these are particularly quick.
//
// Fast Long Nibble Rotate Right
//
// Author: Graham Cole
//
// Input: l - 32-bit word R4/R5/R6/R7.
//
// Output: l - 32-bit word R4/R5/R6/R7
//
// Function: l is rolled four bits to the left.
//
// Notes: Very useful for ASCII-Hexadecimal conversions etc.
//
#pragma ASM
$REGUSE _fast_long_nibble_rotate_right( A, R0, R4, R5, R6, R7 )
#pragma ENDASM
unsigned long int fast_long_nibble_rotate_right( unsigned long int l )
{
#pragma ASM
;
MOV A,PSW ;Get the base address of
ANL A,#0x18 ; the current register bank.
ORL A,#0x07 ;Point to register R7.
MOV R0,A ;R0 is a pointer to R7.
;
;rotate value 4 bits left.
;
;Acc R4R5R6R7
;xx ABCDEFGH
XCH A,R4 ;AB xxCDEFGH
SWAP A ;ba xxCDEFGH
XCH A,R5 ;CD xxbaEFGH
SWAP A ;dc xxbaEFGH
XCH A,R6 ;EF xxbadcGH
SWAP A ;fe xxbadcGH
XCH A,R7 ;GH xxbadcfe
SWAP A ;hg xxbadcfe
XCHD A,@R0 ;he xxbadcfg - R0 points to R7
DEC R0 ;
XCHD A,@R0 ;hc xxbadefg - R0 points to R6
DEC R0 ;
XCHD A,@R0 ;ha xxbcdefg - R0 points to R5
XCH A,R4 ;xx habcdefg
#pragma ENDASM
return( l );
}
//
// Compact Binary To Uppercase ASCII Hexadcimal.
//
// Author: Graham Cole
//
// This function converts a value (in LS nibble of R7) in the range 0...15 to
// the equivalent ASCII hexadacimal character in the range:
//
// '0'-'9' or 'A'-'F'
//
// The returned hexadecimal character is returned in the accumulator and, for
// Keil compatibility, in the register R7.
//
// An alternative entry point is provided for calls with the value in the LS
// nibble of the accumulator.
//
// The first line of C code will suppress the UNUSED variable warning and the
// Keil C51 compiler will optimise this code out. The return() statement is
// compiled to the RET instruction.
//
// The trick of using a combination of ADD and DA instructions to perform this
// conversion is an old one that I belive predates the 8051 and, as far as I
// know, the original author is lost to history. The technique is compact and
// only slightly slower than the table look-up method.
//
#pragma ASM
$REGUSE _compact_binary_to_uppercase_ascii_hexadcimal( A, PSW, R7 )
#pragma ENDASM
char compact_binary_to_uppercase_ascii_hexadcimal( char value )
{
value = value; // Supress UNUSED warning.
#pragma ASM
MOV A,R7 ;
compact_binary_to_uppercase_ascii_hexadcimal:
ANL A,#0x0F ;
ADD A,#0x90 ;
DA A ;
ADDC A,#0x40 ;
DA A ;
MOV R7,A ;
#pragma ENDASM
return( value );
}
|
| Topic | Author | Date |
| convert binary number to Hexa | 01/01/70 00:00 | |
| RE: convert binary number to Hexa | 01/01/70 00:00 | |
| Presentation - not Conversion | 01/01/70 00:00 | |
| RE: convert binary number to Hexa | 01/01/70 00:00 | |
| RE: convert binary number to Hexa | 01/01/70 00:00 | |
RE: convert binary number to Hexa | 01/01/70 00:00 |



