Shop OBEX P1 Docs P2 Docs Learn Events
[resolved][puzzle] tight spot - Page 2 — Parallax Forums

[resolved][puzzle] tight spot

2»

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2012-09-23 19:51
    All in all I'm impressed so far. Not as many participants as I'd like but it's a start.
  • Mark_TMark_T Posts: 1,981
    edited 2012-09-24 07:46
    Two steps forward, one step back:
                    neg     ecnt, #29+1
                    call    #blank
                    add     ecnt, #2  wc
                    djnz    ecnt, #blank
    

    The final djnz branch happens when the previous add changes ecnt from -2 to 0, which is the first unsigned carry. The next time round
    the add changes ecnt from -1 to +1 and the djnz falls through.

    [edit: the wc can alternatively be on the djnz instruction]

    [EDIT: Then I had another thought:
    blank           .....
    blank_ret       ret     ' blank routine at a lower address than foo
    
    '-----------------------------
                    mov     ecnt, #29-2
    foo             call    #blank
                    djnz    ecnt, #blank        ' nice tight loop for NC calls
                    jmpret  blank_ret, foo  wc  ' indirect via the source field of the call instruction at foo
    '-----------------------------
    
    Which shortens the inner loop, yay!

    [EDIT AGAIN: I'm wrong about moving the blank routine, the values compared are instructions, so this will do:
    '-----------------------------
                    mov     ecnt, #29-2
    foo             call    #blank  wz         ' WZ sets a bit high in the instruction making it larger in value than a plain ret instruction!
                    djnz    ecnt, #blank        ' nice tight loop for NC calls
                    jmpret  blank_ret, foo  wc  ' indirect via the source field of the call instruction at foo, compare values of the two instructions to set C
    '-----------------------------
    
  • kuronekokuroneko Posts: 3,623
    edited 2012-09-24 16:53
    @Mark_T: Congratulations! That even beats my version in runtime (I didn't exploit the fact the the return address remains set so you can jump directly). As for the last call, that's what I was after. But you don't have to force wz on the call itself. A call insn has its wr bit set while a ret doesn't. This makes it automatically unsigned greater.

    Minor nitpick, it's still mov ecnt, #29-1.
  • Mark_TMark_T Posts: 1,981
    edited 2012-09-24 17:18
    Yes, well spotted. I made a test harness (attached) now with my (corrected) attempts to get the timings and verify. Addictive puzzle that - on the surface so simple...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-09-24 17:30
    kuroneko wrote:
    Minor nitpick, it's still mov ecnt, #29-1.
    I count 29 calls to blank with the 29-2. Are there supposed to be 30?

    -Phil
  • kuronekokuroneko Posts: 3,623
    edited 2012-09-24 17:40
    When ecnt is 1 you get the initial call, not the djnz and the final jmpret which makes it 2 (ecnt + 1). So preset should be 28.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-09-24 18:03
    I must be missing something. The first call happens at foo, which sets up the return address. Then 27 "calls" via the djnz. And finally one call via the jmpret. That's 29.

    -Phil
  • kuronekokuroneko Posts: 3,623
    edited 2012-09-24 18:05
    Then 27 "calls" via the djnz.
    It's a tricky one. djnz only jumps dst-1 times.
  • Mark_TMark_T Posts: 1,981
    edited 2012-09-24 18:09
    kuroneko wrote: »
    It's a tricky one. djnz only jumps dst-1 times.
    Which is why its not generally used for looping zero or more times, since to excute the body zero times it would have to jump -1 times which is a little too quantum for the Prop.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-09-24 18:09
    Aaaah! Smile! :)

    -Phil
  • User NameUser Name Posts: 1,451
    edited 2012-09-25 22:32
    I certainly am impressed with the test harness Mark-T assembled. Great puzzle by the k-man, too.
Sign In or Register to comment.