Is there any way I can store the position of the current instruction so that I can branch back to it at some point in the future.
I am trying to implement some GOSUB like functionality.
Now the Branch instruction takes an argument representing the number of bytes relative to the current position to jump to. Its easy enough to store a label to where I want to go to and then branch to it. If I could store the current position just before branching, when it comes to the return I c开发者_JAVA百科ould take the new current position - the stored position and pass that to Branch..
But I can't work out how.. Any ideas?
Afaik (and I've done a good amount of IL), no this isn't possible as presented, and even if it were it would almost certainly lead to unverifiable IL (not good), as there would be no way to prove the stack is balanced. Also remember that the offset is part of the IL instruction, not the stack - so you couldn't store it in a local (or on the stack) and reload it etc.
Just structure the code more cleanly; use calls where necessary to call into sub methods, and maybe a jump-table ("switch" essentially).
精彩评论