Main Menu
Main Page
Forums
New pages
Recent changes
Random page
Help

Glitches
Celebi Egg glitch
SRAM glitch
Pomeg glitch data corruption
Tweaking
Pokémon cloning
Arbitrary code execution
Glitches by Generation
Other major glitches
Other glitch categories

References/Resources
Databases
Disassembly projects
The Big HEX List
Pokémon cheat codes
Pokémon glitch terminology
Useful tools
More

Affiliates
Legendary Star Blob 2 (Hakuda)
Pokémon Speedruns wiki
PRAMA Initiative
Become an affiliate!

Technical
Site Source Code

Search Wiki

 

Search Forums

 

Author Topic: How to do loops in GBZ80 assembly?  (Read 126 times)

0 Members and 1 Guest are viewing this topic.

joshuarpl

  • GCLF Member
  • Offline Offline
  • Gender: Male
  • Oh snap, I destroyed my save file!
    • View Profile
How to do loops in GBZ80 assembly?
« on: January 02, 2019, 02:51:54 pm »
Hi, Recently I've been wondering how I do loops in assembly, because I kinda wanna do ZZAZZ glitch trainer arbitrary code execution, but if the code ends because of ret, It might crash the game!
Could you tell me how to do looping in GBZ80 assembly please?

Also, Happy New Year!
« Last Edit: January 02, 2019, 02:56:30 pm by joshuarpl »
Pikachu says "4 4 is void, kids!"
forgets ret, destroys universe!

Sherkel

  • The first unquiring one to bare arms
  • Staff
  • *****
  • Online Online
  • Gender: Male
  • リリー再び!
    • View Profile
Re: How to do loops in GBZ80 assembly?
« Reply #1 on: January 02, 2019, 03:08:21 pm »
ld a,amount of times
(Instructions)
dec a
jr nz, (pointer to above instructions)
?

I'm obviously not the best one to ask, but I know you can do something along those lines.
« Last Edit: January 02, 2019, 03:10:29 pm by Sherkel »
 
 

ISSOtm

  • The French Lord of Laziness (and a huge The Legend Of Zelda fan)
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • Pewter City (B)rocks !
    • View Profile
    • My Little Website
Re: How to do loops in GBZ80 assembly?
« Reply #2 on: January 02, 2019, 07:19:33 pm »
It's generally a jump back to the loop point. Whatever the condition is, really.
"THOU SHALL NOT PASS !!"  RIVAL's effect, Gandalf.

Proudly glitching Pokémon Red and Yellow on a Black & White GB, Pocket GB, GB Color, GBA SP and new 3DS.

My Twitter (beware, I'm French)
My YouTube (same warning)

Here is an online tool to build 8F setups : GBz80 to Items !

They see me layzin', they ha-tin'...
Heavy contributor of the global augmentation of entropy (my room's is too damn high !)

joshuarpl

  • GCLF Member
  • Offline Offline
  • Gender: Male
  • Oh snap, I destroyed my save file!
    • View Profile
Re: How to do loops in GBZ80 assembly?
« Reply #3 on: January 03, 2019, 01:29:33 pm »
ld a,amount of times
(Instructions)
dec a
jr nz, (pointer to above instructions)
?

I'm obviously not the best one to ask, but I know you can do something along those lines.

A loop address doesn't really seem to help me all that much, is there a way to name a loop like in 6502?
in 6502 it would be
loop:
(whatever the loop does)
JMP loop

But what would that be in GBZ80 assembly?
Pikachu says "4 4 is void, kids!"
forgets ret, destroys universe!

Sherkel

  • The first unquiring one to bare arms
  • Staff
  • *****
  • Online Online
  • Gender: Male
  • リリー再び!
    • View Profile
Re: How to do loops in GBZ80 assembly?
« Reply #4 on: January 03, 2019, 04:25:54 pm »
...No? I mean, you can give a piece of code whatever name you want, but it won't be part of the executed instructions themselves. I don't know what you're referring to about 6502. Maybe I'm missing something. Anyway, as Isso said, just keep jumping back to a given point.
« Last Edit: January 03, 2019, 06:56:47 pm by Sherkel »
 
 

Parzival

  • Buyer beware: House comes with 3 free skeletons in a closet of your choice.
  • GCLF Member
  • *
  • Offline Offline
  • Gender: Male
  • This box intentionally left blank. ...wait...
    • View Profile
    • (null)
Re: How to do loops in GBZ80 assembly?
« Reply #5 on: January 03, 2019, 07:15:25 pm »
ld a,amount of times
(Instructions)
dec a
jr nz, (pointer to above instructions)
?

I'm obviously not the best one to ask, but I know you can do something along those lines.

A loop address doesn't really seem to help me all that much, is there a way to name a loop like in 6502?
in 6502 it would be
loop:
(whatever the loop does)
JMP loop

But what would that be in GBZ80 assembly?
If you want an infinite loop, like the example code implies, you'd have to know where the first opcode is, as (using the name in your example) "loop" isn't an actual place, it's a compiler thing to make loops easier since you wouldn't necessarily know where the code IS until after building.

Say your code starts at A000 (this is an EXAMPLE, it probably won't), you'd do something like this for an infinite loop:
<some code or w/e>
jp A000

or, depending on the code length, you could save one byte of space and a cycle if you use a "jr A000" instead, but the code can't be more than (IIRC) 120 bytes for that.

If you want it to jump somewhere else when some condition is met, you'd do something like this at A000 (this example is for jumping away after a certain number of loops):

ld a,<loopnumber>
<code>
dec a
jr nz,A002
jp <somewhere>

You have to jump back to AFTER the first ld a statement or it'll never finish as you reset the a register every loop.

These are just examples, but feel free to use them, I guess.
Ask me about betrayal.
Ask me about depression.
Ask me about death.
Ask me about destruction.
Ask me about hardship.
I've been through s**t.
If you need to talk to someone, my PM inbox is always open.

ISSOtm

  • The French Lord of Laziness (and a huge The Legend Of Zelda fan)
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • Pewter City (B)rocks !
    • View Profile
    • My Little Website
Re: How to do loops in GBZ80 assembly?
« Reply #6 on: January 03, 2019, 07:33:02 pm »
You can use labels in GBz80:
Code: [Select]
FillMemory: ; Global label
    inc c
    inc b
    jr .skip
.loop ; Local label
    ld [hli], a
.skip
    dec c
    jr nz, .loop
    dec b
    jr nz, .loop
    ret
Note that `jr` is different in the way the target address is specified; `jp` just spells it out, `jr` uses a signed offset instead.
"THOU SHALL NOT PASS !!"  RIVAL's effect, Gandalf.

Proudly glitching Pokémon Red and Yellow on a Black & White GB, Pocket GB, GB Color, GBA SP and new 3DS.

My Twitter (beware, I'm French)
My YouTube (same warning)

Here is an online tool to build 8F setups : GBz80 to Items !

They see me layzin', they ha-tin'...
Heavy contributor of the global augmentation of entropy (my room's is too damn high !)

Parzival

  • Buyer beware: House comes with 3 free skeletons in a closet of your choice.
  • GCLF Member
  • *
  • Offline Offline
  • Gender: Male
  • This box intentionally left blank. ...wait...
    • View Profile
    • (null)
Re: How to do loops in GBZ80 assembly?
« Reply #7 on: January 04, 2019, 01:37:18 am »
You can use labels in GBz80:
Code: [Select]
FillMemory: ; Global label
    inc c
    inc b
    jr .skip
.loop ; Local label
    ld [hli], a
.skip
    dec c
    jr nz, .loop
    dec b
    jr nz, .loop
    ret
Note that `jr` is different in the way the target address is specified; `jp` just spells it out, `jr` uses a signed offset instead.
That only works when compiling, there's no way to specify those with an opcode. That's what we're saying.
Ask me about betrayal.
Ask me about depression.
Ask me about death.
Ask me about destruction.
Ask me about hardship.
I've been through s**t.
If you need to talk to someone, my PM inbox is always open.

ISSOtm

  • The French Lord of Laziness (and a huge The Legend Of Zelda fan)
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • Pewter City (B)rocks !
    • View Profile
    • My Little Website
Re: How to do loops in GBZ80 assembly?
« Reply #8 on: January 04, 2019, 06:33:41 am »
gbz80toitems supports labels, and if you're going for complex code, you should definitely use that.
Unless you want hex output, in which case I don't think there's any better solution that using RGBDS directly and looking at the output binary.
"THOU SHALL NOT PASS !!"  RIVAL's effect, Gandalf.

Proudly glitching Pokémon Red and Yellow on a Black & White GB, Pocket GB, GB Color, GBA SP and new 3DS.

My Twitter (beware, I'm French)
My YouTube (same warning)

Here is an online tool to build 8F setups : GBz80 to Items !

They see me layzin', they ha-tin'...
Heavy contributor of the global augmentation of entropy (my room's is too damn high !)

joshuarpl

  • GCLF Member
  • Offline Offline
  • Gender: Male
  • Oh snap, I destroyed my save file!
    • View Profile
Re: How to do loops in GBZ80 assembly?
« Reply #9 on: January 08, 2019, 10:36:34 am »
ld a,amount of times
(Instructions)
dec a
jr nz, (pointer to above instructions)
?

I'm obviously not the best one to ask, but I know you can do something along those lines.

A loop address doesn't really seem to help me all that much, is there a way to name a loop like in 6502?
in 6502 it would be
loop:
(whatever the loop does)
JMP loop

But what would that be in GBZ80 assembly?
If you want an infinite loop, like the example code implies, you'd have to know where the first opcode is, as (using the name in your example) "loop" isn't an actual place, it's a compiler thing to make loops easier since you wouldn't necessarily know where the code IS until after building.

Say your code starts at A000 (this is an EXAMPLE, it probably won't), you'd do something like this for an infinite loop:
<some code or w/e>
jp A000

or, depending on the code length, you could save one byte of space and a cycle if you use a "jr A000" instead, but the code can't be more than (IIRC) 120 bytes for that.

If you want it to jump somewhere else when some condition is met, you'd do something like this at A000 (this example is for jumping away after a certain number of loops):

ld a,<loopnumber>
<code>
dec a
jr nz,A002
jp <somewhere>

You have to jump back to AFTER the first ld a statement or it'll never finish as you reset the a register every loop.

These are just examples, but feel free to use them, I guess.

Well, I figured it out... kindof! I use the jp instruction, and jump to whatever the instruction I wanna jump to is at!
Pikachu says "4 4 is void, kids!"
forgets ret, destroys universe!

Sherkel

  • The first unquiring one to bare arms
  • Staff
  • *****
  • Online Online
  • Gender: Male
  • リリー再び!
    • View Profile
Re: How to do loops in GBZ80 assembly?
« Reply #10 on: January 08, 2019, 11:41:30 am »
That's what we were all saying, but it's good to hear you figured it out.