Shop OBEX P1 Docs P2 Docs Learn Events
ASM: Senior moments with CNT — Parallax Forums

ASM: Senior moments with CNT

Nick MuellerNick Mueller Posts: 815
edited 2007-12-04 09:22 in Propeller 1
Hi!

It seems, I do have senior moments with doing some math with CNT. Or the problem is somewhere else.

I do have a variable (lets call it OnCNT) that is -if some conditions are true- loaded with CNT (plus some offset we can ignore for now). right after that, an other variable (called OffCNT) is set with OnCNT + some offset.
In a loop, the variables OnCNT and OffCnt are checked and compared to CNT. If the CNT has passed bejond (or equal) OnCNT, some action is taken. If CNT has passed bejond (or equal) OffCNT, some other action.

Snipplett:
  mov :OnCNT, CNT
  mov :OffCNT, :OnCNT
  adds :OffCNT, #SomeValue



Hopefully so far so good.

Now to the comparison:
  subs CNT, :OnCNT WZ, WC, WR
  if_b jmp #:doSomethingElse




What's wrong with that? Is it wrong? And where?
I'm aware, that CNT and OnCNT/OffCNT will wrap over. The distance between CNT and OnCNT/OffCNT can only be much less than 1 second, so that can't be the problem.

The code I had for prototyping in SPIN worked and looked like this:
  OnCNT:= CNT
  OffCNT:= OnCNT + SomeValue
  ...
  if (CNT - OnCNT => 0)
    DoSomethingThatIsUsefull




Thanks!

Nick

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!

The DIY Digital-Readout for mills, lathes etc.:
YADRO

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-12-04 00:14
    CNT is a read only register, this means it must be accessed in the source field of an instruction, your line "subs CNT, :OnCNT WZ, WC, WR" has the CNT register in the destination field. This is likely where you are experiencing problems. If you perform "subs :OnCNT, CNT WZ, WC, WR" instead and inverse your logic from "if below" to "if above or equal" you should get the code operating the way you expect it to (though I haven't analyzed what happens in the event of a roll-over)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 12/4/2007 12:28:30 AM GMT
  • Nick MuellerNick Mueller Posts: 815
    edited 2007-12-04 00:25
    > CNT is a read only register, this means it must be accessed in the source field of an instruction,

    And the "WR" doesn't change that? I mean, it shouldn't try to write, I only need the flags. Do I?

    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-04 00:29
    What happens is that, if you use a read-only register in the destination field, you will access the "hidden" RAM location corresponding to the address of the special register (like CNT, INA, INB, PAR). The WR flag will indeed apply to that "hidden" location. Chip used these 4 locations for some extra longs in one of his super-duper video drivers where he had otherwise run out of memory.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-12-04 00:31
    The rule about read only registers applies regardless of whether the result is written, what your code is doing is accessing CNT's shadow register which will read as $0 unless you explictly write to the CNT register (writing to a read only register places the information in the shadow register, since this is decoupled from the mapped in value for reads it has no effect on the value read.)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Nick MuellerNick Mueller Posts: 815
    edited 2007-12-04 00:47
    Changed it to:
      mov :localCNT, CNT
      subs :localCNT, :OnCNT WZ, WC
    if_b jmp #:somewhere
    
    



    but didn't make it better.
    If someone can confirm that this code is right, I'll have to chase bugs somewhere else ...

    EDIT:
    I think that adding the offset with "ADDS :OffCNT, #SomeValue" is wrong. I need the wrap-around.
    I wonder how it worked in SPIN.
    I'll have a sleep, it's 2 o'clock in the morning.8-/


    Thanks,
    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO

    Post Edited (Nick Mueller) : 12/4/2007 1:18:32 AM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-04 09:22
    Nick,
    CNT is "unsigned" - don't use SUBS until you understand the finer meaning of its carry smile.gif SUB will do in all cases, followed by a CMP.

    For "shadow registers": read my contributions... I think I explained it in detail (and many times...)
Sign In or Register to comment.