Shop OBEX P1 Docs P2 Docs Learn Events
Help! Odd cog behavior — Parallax Forums

Help! Odd cog behavior

This program starts up a cog that doubles a number and writes it to hub memory. The output on the serial terminal is "246" followed by exclamation points, one per second. Not very interesting but at least it's what I expect.

What I don't understand is why, if I change the jmp destination to #wait1, the terminal shows "24@" and no exclamation points. I don't understand why it should make any difference. Can someone explain what's going on?
CON
        _clkmode = xtal1 + pll16x
        _xinfreq = 5_000_000
OBJ
  serial        : "FullDuplexSerial"
pub Main
  serial.start(31, 30, %0000, 115_200)
  waitcnt(cnt + clkfreq * 2)
  serial.dec(Start(123))
  repeat
    waitcnt(cnt + clkfreq * 1)
    serial.tx("!")
   
pub Start(m)
  n := m
  cognew(@entry, @result)
  waitcnt(cnt + clkfreq * 1)

dat
              org
entry         mov               ptr, par
              add               n, n
wait1         wrlong            n, ptr
wait2         jmp               #wait2          ' change to #wait1

n             long              0
ptr           res               1

Comments

  • Michael,

    It's because result goes out of context after Start returns, but your wrlong is still writing to the stack.

    -Phil
  • mparkmpark Posts: 1,305
    Thank you, Phil! I am rusty!
  • The serial.dec() method clobbers the result variable returned by the Start method, and vice versa. I tried it and see the same 24@ repeated ad infinitum in a sea of nulls.

    It's safer to do
    cognew(@entry, @n)
    referring to the global variable n, and then,
    serial.dec(n)

Sign In or Register to comment.