Captain Cozmos Posted August 21, 2022 Share Posted August 21, 2022 In my travels I am coming across two programming statement of which one I understand completely and the 2nd I need help with. I understand Call WRITE_REGISTER, It is self explanatory. However I keep coming across this in many of the older games. JP WRITE_REGISTER. Of course it does the same thing but how do we return from this Jump? I can make an educated guess that at the end of the routine there is a RET command that would what, return it to the last call before the JP command? Is there something in Write Register that adjusts the stack pointer or does it go through the BIOS and find it's way back. I just wanted to know if anyone has encountered this and what did you find out. TIA CC Quote Link to comment Share on other sites More sharing options...
Falonn Posted August 22, 2022 Share Posted August 22, 2022 3 hours ago, Captain Cozmos said: I can make an educated guess that at the end of the routine there is a RET command that would what, return it to the last call before the JP command? This is my understanding. I like to think of JP (and JR, etc.) as being smaller hops inside the same larger/longer function (assuming a mental model of C code) which I loosely consider "everything between the CALL and the RET". But of course in asm you can do things that don't easily map to C code. So if you arrange your CALLs just right and you know a certain jump will always be made during a particular set of circumstances, you can save yourself an extra CALL/RET pair, which is a nice optimization. Quote Link to comment Share on other sites More sharing options...
PkK Posted August 24, 2022 Share Posted August 24, 2022 Using jp instead of call that way is called "tail call optimization". It is a standard optimization done both by assembler programmers and compilers (e.g. SDCC). 1 Quote Link to comment Share on other sites More sharing options...
SiRioKD Posted August 25, 2022 Share Posted August 25, 2022 On 8/21/2022 at 11:01 PM, Captain Cozmos said: In my travels I am coming across two programming statement of which one I understand completely and the 2nd I need help with. I understand Call WRITE_REGISTER, It is self explanatory. However I keep coming across this in many of the older games. JP WRITE_REGISTER. Of course it does the same thing but how do we return from this Jump? I can make an educated guess that at the end of the routine there is a RET command that would what, return it to the last call before the JP command? Is there something in Write Register that adjusts the stack pointer or does it go through the BIOS and find it's way back. I just wanted to know if anyone has encountered this and what did you find out. TIA CC At the end of an asm function you have RET instruction. If you call a subroutine just before you RET, you'll made a double jump (one to call subroutine and one of your RET). To avoid this,you can do a JUMP to subroutine instead CALL it to save clock cycles. There's a lot of optimizations to apply. Another is to use JP instead JR which is 3 bytes long instead 2 but it's faster (JR needs to calculate the relative address, JP not). "OR a" to test the accumulator, "XOR a" to clean it (instead LD a,0) and so on... 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.