0

I'm using the HC12 chip.

We have instructions SUBA and DECA. The way you use SUBA is by subtracting some value (either in memory or a value you specify) from register A. DECA however takes no parameters and just subtracts $01 from register A. So my question is what is the difference between the instructions SUBA #01 and DECA? My guess was that they behaved differently on negative numbers, but I am not entirely sure. The question I'm dealing with:

      LDAA #230 ; 8 bit system, so this number is technically -26 in 2s complement. Register A is 8 bits, with LDAA loading into register A
LOOP: SUBA #01
      BGT LOOP
; rest of code

Would the code above work differently if SUBA #01 was replaced with DECA?

arpanet101
  • 183
  • 1
  • 11
  • 1
    That's not x86 code. I think you mean 68hc12. Also, `#230` fits in a 16-bit integer. Did you mean *8-bit* where `230 == -26` in 2's complement? – Peter Cordes Dec 17 '19 at 21:18
  • Yes that's what I meant - the hc12 chip's D register is A reg + B reg, with A and B being 8 bits each, which is why I said 16 bit. – arpanet101 Dec 18 '19 at 00:01
  • 1
    Ok, but `ldaa` loads the 8-bit A register. If you consider the whole B:A = D register, the upper half is unset. Or if it's zero, then you have `230`, *not* `-26`. You'd only get `-26` from sign-extending 8-bit `230` into 16-bit D which your code doesn't do. You're only working with 8-bit A with your load-immediate and `suba`. – Peter Cordes Dec 18 '19 at 00:06
  • I apologize for messing up - should I be changing the tag? I'll edit the question accordingly. – arpanet101 Dec 18 '19 at 00:08
  • 1
    I already changed the tag for you from x86 to 68hc12 – Peter Cordes Dec 18 '19 at 00:10
  • That's great - thanks! – arpanet101 Dec 18 '19 at 00:11
  • Except for the instruction size difference, `INCx` and `DECx` do not affect the CCR[C] flag (carry) whereas `ADDx` and `SUBx` do. – tonypdmtr Dec 23 '19 at 15:29

1 Answers1

3

DECA is a very common operation and thus the ability to encode it in a smaller instruction was deemed worth its redundancy.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • @cyber101: the same reasoning as this HC12 answer also applies to x86-16, except that x86 `dec` leaves CF unmodified, making it useful in `adc` / `sbb` or other loops where you want CF to survive a loop iteration. – Peter Cordes Dec 17 '19 at 21:17