Motorolla MC68HC11 Microcontroller-to convert a Hex digit.

Thread Starter

NiCeBoY

Joined Aug 20, 2008
59
Hello,
I am require to write a program module to convert a Hex digit passed to it in the lower nibble of Accumulator A to its ASCII character code. The upper nibble should be treated as "don't care".
Return the ASCII code in Accumulator A and save it in a suitable memory location.
All other CPU register values are to remain unchanged.


Thanks in advance for your help...

Regards...
 

RiJoRI

Joined Aug 15, 2007
536
"Unchanged" is not the same as "unused." Push & pop any registers you need to use for indexing.

OR, study the ASCII values for '0'..'9', 'A'..'F', especially how $9 and $A are related to '9' and 'A'.

--Rich
 

Thread Starter

NiCeBoY

Joined Aug 20, 2008
59
yeah i got the steps to proceed :

First of all, clear the upper bits by doing an AND with immediate data 0x0F.
Then compare the remaining number with 9. If it's less than or equal to 9, then do an OR with 0x30H and that is the ASCII code.

If the number is greater than 9, then subtract 9 and then do an OR with 0x40H to get the ASCII code.


now i am working on the codes...
 

Thread Starter

NiCeBoY

Joined Aug 20, 2008
59
here is my code:

ORG $2000
ANDA #$0F ; AND to clear the upper nibble
SUBA 9 ; to compare if it’s the same or lower

BLE LOWSAME ; branch if 9, that is if after subtracting 9 the result it 0
BMI LOWSAME ; branch if after subtracting 9 the result is negative(minus) that is the number is lower that 9.
BGT GREATER ; Branch if the number after subtracting 9 is greater than zero. That is the number is greater than 9.

LOWSAME ADDA 9 ; Add 9 to A to get the original number
ORAA #$30 ; OR A with #$30 to get the ASCII
STAA $2060 ; STORE in memory location $2060

BRA SELF

GREATER ORAA #$40 ; Or the result aft4er SUB9 with #$40 to get the ASCII
STAA $2060 ; STORE in memory location $2060

SELF BRA SELF
END
The problem is that I must use the instruction "CMPA" to compare instead of doing the substraction..


Can anyone help me out of this please?

the instruction set can be found @:
http://www.ele.uri.edu/Courses/ele205/6811-Instructions/index.html


Thanks
 

AlexR

Joined Jan 16, 2008
732
Its many many years since I've had anything to do with the 68 series of processors so don't expect working code, but the obvious way (at least to me ) would be to compare your input to 9 after masking off the high nibble. If equal to or less than 9, add $30, if greater than 9, add $37.
 

Thread Starter

NiCeBoY

Joined Aug 20, 2008
59
Hello,
why we should add $30 and $37 please?

because someone told me if when comparing the result is less than or equal to 9, then do an OR with $30 and that is the ASCII code.

If the number is greater than 9, then subtract 9 and then do an OR with $40.


Thats a bit confusing me....
thanks..
 

AlexR

Joined Jan 16, 2008
732
Ascii 0 - 9 = $30 - $39 so the masked low nibble plus $30 will give the correct ascii value for numbers 9 or less.
Ascii "A" = $41, Ascii "B" = $42 etc
$0A + $37 = $41
Ascii B = $42
$0B + $37 =$42
etc.

Actually a simpler method of getting the same result would be to mask the high nibble and add $30 (this will give the right result for numbers between 0 and 9).

Next test result of the addition.

If the result of the addition is $39 or less then don't do anything.

If result is higher than $39 it means that the number was more than 9 so add another $07 to the answer to bump it up into the Ascii A to F range. If
 

RiJoRI

Joined Aug 15, 2007
536
yeah i got the steps to proceed :

First of all, clear the upper bits by doing an AND with immediate data 0x0F.
Then compare the remaining number with 9. If it's less than or equal to 9, then do an OR with 0x30H and that is the ASCII code.

If the number is greater than 9, then subtract 9 and then do an OR with 0x40H to get the ASCII code.
Yes, that will work, but you are working too hard. Remember, 'A' - '9' = 7.
So clear the upper bits, and compare the remaining number with 9. If it's greater than 9, add 7. In all cases, add $30, or '0'. ('0' will make the code easier to understand, later.)

--Rich
 

Thread Starter

NiCeBoY

Joined Aug 20, 2008
59
Yeah thats allrite... thanks...


Now i got another question...

I need to write a program module to convert a Hex byte passed to it in AccA to its ASCII coded word.
Return the word in AccD and then save it in a suitable area memory.
NB. This module affects AccA & AccB.
Can anyone help me out with this one, just give me the steps and i will proceed with the code writing......

by the way what is the difference between this one and the first one...


thanx..
 
Top