Author Topic: On chaining hooks  (Read 4666 times)

0 Members and 1 Guest are viewing this topic.

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
On chaining hooks
« on: April 19, 2017, 06:50:17 pm »
As the title suggests, I am struggling with chaining hooks. So far, I have successfully created a Raw Key hook in ram. I have been poking around chaining and have had no luck yet. (only some interesting crashes) Here is What I understand I need to do: save the 3 bytes that hold the pointer and page of the hook that already exists, install the hook and quit. The hook runs my code then switches the page at $4000 (I assume the existing hook is an app) and calls the saved address. It then switches the page back to what it was originally and returns.
Am I missing something here?
I'm still around... kind of.

Offline Geekboy1011

  • The Oneironaut
  • Donator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2031
  • Rating: +119/-2
  • Dream that Awakening dream
    • View Profile
Re: On chaining hooks
« Reply #1 on: April 19, 2017, 06:59:13 pm »
Can we get some code with that please :D

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: On chaining hooks
« Reply #2 on: April 19, 2017, 07:16:56 pm »
Here is my code. It assumes a lot of things.
Code: [Select]
save=plotSScreen+200
;don't judge my save area - I will find a better one later
;It is safe as long as I don't pull up the graph since the homescreen does not affect it
set 7,(iy+28h)
ld hl,($9B84)
ld (save),hl
ld lh,($9B84+2)
ld (save+2),hl
ld hl,hook
ld de,save+4
push de
ld bc,HookEnd-Hook
ldir
pop hl
ld a,1
BCALL($4F66)    ;sets the hook
ret
Hook:
db $83
push af
push af
ld hl,(save)
ld a,(save+2)
out (6),a
pop af
ld e,0
call CallHL
ld a,32    ;I did some experimentation and that seems to be the default value
out (6),a
pop af
ret
CallHL:
jp (hl)
HookEnd:
That is it. I'm sorry for the weird format. I copied it by hand from Mimas.
All this code is for testing. I realize how poorly it is written.
I'm still around... kind of.

Offline Geekboy1011

  • The Oneironaut
  • Donator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2031
  • Rating: +119/-2
  • Dream that Awakening dream
    • View Profile
Re: On chaining hooks
« Reply #3 on: April 19, 2017, 07:41:13 pm »
So what does the code currently do. Just crash?

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: On chaining hooks
« Reply #4 on: April 19, 2017, 07:46:55 pm »
So what does the code currently do. Just crash?
It installs the hook successfully, but as soon as I press any key it makes a weird staircase pattern on the homescreen and freezes.

Edit: I fixed it!
It turns out that the push af at the beginning that was paired with the pop af at the very end was the problem. After I removed that, it worked perfectly!

Edit2: here is the code. I can't get the cp statement to work
Code: [Select]
save=plotSScreen+200
set 7,(iy+28h)
ld hl,($9B84)
ld (save),hl
ld lh,($9B84+2)
ld (save+2),hl
ld hl,hook
ld de,save+4
push de
ld bc,HookEnd-Hook
ldir
pop hl
ld a,1
BCALL($4F66)    ;sets the hook
ret
Hook:
db $83
push af
push af
bcall putC
pop af
cp 10
jr nz,_+
pop af
xor a
ret
_:
ld hl,(save)
ls a,(save+2)
out (6),a
pop af
ld e,0
jp (hl)
« Last Edit: April 19, 2017, 08:44:19 pm by E37 »
I'm still around... kind of.