Subtract 10 bit value with QG8

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Subtract 10 bit value with QG8

2,676 Views
admin
Specialist II
Hi,
I have a question is regards to subtracting a 10 bit value in QG8, the reason being I want to use 10 bit ADC sampling mode and display it using 7 segment leds. However, in order to do this I need to compare and subtract the 10 bit sample. The CMP and SUB  instruction use the 8 bit accumulator to do this, does anyone have any idea to achive CMP and SUB in 10 bits?

Thanks!
Labels (1)
0 Kudos
6 Replies

493 Views
bigmac
Specialist III
Hello,
 
Since the 10-bit ADC value is right-aligned, simply use 16-bit calculations.  In assembly, the subtraction should be done in two stages, handling the low byte first.  The following code snippet assumes a constant 16-bit OFFSET value.
 
   lda   ADCRL
   sub   #OFFSET%256  ; Low byte value
   sta   RESULT+1
   lda   ADCRH
   sbc   #OFFSET/256  ; High byte value
   sta   RESULT
 
For a compare operation, using a similar method,
 
   lda   ADCRL
   cmp   #OFFSET%256  ; Low byte value
   lda   ADCRH
   sbc   #OFFSET/256  ; High byte value
 
Alternatively, you might use the H:X register, and the CPHX instruction.
 
Regards,
Mac
 
0 Kudos

493 Views
JimDon
Senior Contributor III
Code:

n1   ds.w  1
n2   ds.w  1
n3   ds.w  1
...
; Save values for testing...                      LDHX #513    ; Get a value to store into n1           STHX  n1     ; Save it           LDHX #257    ; Get a value to store into n2           STHX  n2     ; Save it
; Compare n1 to n2
           LDHX  n2     ; Get n2 into H:X           CPHX  n1     ; Compare to other value.           BEQ   EQUAL  ; They won't be equal in this case.                      ; n3 = n1-n2           LDHX  n1      ; Get the first number to H:X            TXA           ; Put the lower byte in A           PSHH          ; Save the upper byte on the stack           LDHX  n2      ; Get the second number to H:X           PSHX          ; Push the lower byte on the stack           SUB   1,SP    ; Subtract lower bytes.           PSHA          ; Save the lower byte           PSHH          ; Push the upper byte on the stack           PULA          ; Pop it back into A           TSX           ; Transfer the SP to the H:X register.           STA   1,X     ; Save A           LDA   2,X     ; Get the high byte back           SBC   1,X     ; Subtract with carry from the other hi byte           PSHA          ; Push A           PULH          ; Pop it back into H           PULX          ; Get the lo byte back           STHX  n3      ; Save it to answerEQUAL:           

 You can probably fret over the generalized subtraction code and reduce it a bit.

I should also lecture you that this is first year assembly language stuff, and maybe you should get a good book on assembly language or use "C" instead.




Message Edited by JimDon on 2008-02-18 01:43 AM
0 Kudos

493 Views
admin
Specialist II
Thank you so much for the insight fellows, am very grateful to have you guys around for guidance!

regards,
Scott
0 Kudos

493 Views
admin
Specialist II
Hi Jim,
Forgot to ask if you could recommand a good book to learn from for a beginner like me?

Thanks!
0 Kudos

493 Views
JimDon
Senior Contributor III
Well this is the book I learned from Assembly Language Programming
As you can see, you can a copy for 0.20 USD, but I don't really recommend it. I guess my wife was correct when she made pitch my copy sometime in the 90's - never did become collectible.

I am not the right guy to recommend a book like this. For one thing, there are certain maths that apply, that are not too hard to learn if explained well.
 I have not found a book that specifically teaches assembly language programming on the 08, but there could be one. Typically, once you learn on any machine, it is pretty easy to apply it to any machine. So if you find a good book based on x86, you can down load all the tools for free and learn on your PC.

There are many books based on the HC11 and HC12, as universities use this chip.

0 Kudos

493 Views
JimDon
Senior Contributor III
BTW Here is a link to where you can down load the reference manuals for the HCS08 assembler and the instruction reference guide. These are not substitutes for a good book, but if you had already learned assembly language programming, these would let you use that knowledge on the HCS08.
In any event, this is information you do need.

Also this is the book I recommend for HCS12.  It is a great book,  and it does cover assembly language,  and has plenty of examples in both "C" and asm. It also covers what you asked about.
(Section 2.5.4 Multi-precision Subtraction).
It's not cheap, but it is good. I got a copy used for around 75 USD.








Message Edited by JimDon on 2008-02-18 12:45 PM
0 Kudos