HC08 16bit Subtraction

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

HC08 16bit Subtraction

1,793 Views
Bloodhound
Contributor I
Hi all,
 
Can anybody please provide post some assembler code to subtract an IMM 16bit value from a 16bit value in RAM. Given that the SUB #xx command is limited to an 8bit immediate value I'm not sure how to do this.
Is it a matter of using say the SP and H,X to perform the operation?
 
I need to be able to handle two possibilites:
 
RAM Var = $8000
IMM Val = $2000
$8000 - $2000 = $6000
 
The another scenario is where the RAM value is less than the IMM value...
 
RAM Var = $2000
IMM Val = $3000
$2000 - $3000 = $F000
 
Thanks,
Ross
Labels (1)
0 Kudos
Reply
4 Replies

446 Views
peg
Senior Contributor IV
Hi,

You just do it a byte at a time using SUB for the low byte first and then SBC for the higher bytes.
The examples you chose are not good as they don't produce a carry from the lower byte.

if RAMVar = $2000

LDA RAMVar+1
SUB #$00
STA RESULT+1
LDA RAMVar
SBC #$30
STA RESULT

now
RESULT = $F000

OR to better demonstrate carry:

if RAMVar = $2000

LDA RAMVar+1
SUB #$01
STA RESULT+1
LDA RAMVar
SBC #$30
STA RESULT

now
RESULT = $EFFF



Message Edited by peg on 2007-12-02 10:44 PM
0 Kudos
Reply

446 Views
bigmac
Specialist III
Hello Ross,
 
For assembly programming, the code will often be easier to follow if constants are represented by a label, as a result of using an equate directive.  The following code snippet extends the process described by Peg, when a label is used.
 
IMM_VAL  equ  8192  ; Could be binary, decimal or hex representation
 
 
    lda  RAMvar+1
    sub  #(IMM_VAL%256)  ; Low byte value
    sta  RESULT+1
    lda  RAMvar
    sbc  #(IMM_VAL/256)  ; High byte value
    sta  RESULT
 
This method should work with most assemblers.  Some assemblers have directives to select the low byte or high byte of a word value, and these could alternatively be used.
 
Regards,
Mac
 


Message Edited by bigmac on 2007-12-03 01:23 AM
0 Kudos
Reply

446 Views
Bloodhound
Contributor I
Thanks guys, that worked well, though, for those reading this in the future, remember to put a CLC command before the first sub....might save some head scratching.
 
Cheers,
Ross
 
 
0 Kudos
Reply

446 Views
peg
Senior Contributor IV
Hi Ross,

The CLC is not required!
SUB does not use the carry only sets/clears due to the result
SBC uses the carry in the operation and sets/clears it due to the result.

0 Kudos
Reply