News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Jumping to Label

Started by LordAdef, April 15, 2018, 03:39:49 PM

Previous topic - Next topic

LordAdef

Guys,

I can do this:

   
lea eax, pLoadLevel     <== this is a procedure address
jmp eax

Fine.

But how could I possibly jump to a label residing inside a procedure? I clearly can get the address of the procedure, so I assume there must be a way to get the address of some label within the code.


SomeProc  proc

    Im_a_noob:
    ret
SomeProc endp

; after some cups of tea...

SomewhereElse proc
   mov SaveJumpAddr, Im_a_noob    <===  this is the idea (the label resides in a different proc)
   ret
SomewhereElse endp


edited to add: Without hardcoding the address offset of the label to the proc entry address?

zedd151

did you try it??

arent labels global?? 

youd have to watch for stack balance going from the middle of one procedure to another.

its not normal code design, imo.


edit== I know there are ways, but Im at a loss at the moment...

zedd151

you could have multiple entry points to the procedure..
2nd entry point being the jump destination. ??



proc1:
some code
jmp jump_dest

more code
stack restore
ret



proc2 :  -entry point

code here

jmp_dest: -2nd entry pt

more code

stack restore
ret



but where would it return to???

forgive the sloppiness, hard to type without full size keys..



zedd151

Would probably be easier and more stable to rethink your code design. Why the need to jump into another procedure?

Maybe the jump could be turned into a call as suggested (2 entry points)

But the trick then would be to return where you expect to be after the (jmp dest) code has executed.

This is giving ME a headache.   :lol:


I miss masm and QE (the editor, not the Queen  :P  )


zedd151

Okay, final thought...

Maybe the code you want to jump to should be made into its own function/procedure. Then be reused by the 2nd procedure??  Would help solve stack issues.

How big a piece of code we talking here?

LordAdef

Hey Zedd!

You know, even if I change the code design, I still want to try this thing for educational purposes.

In fact, the idea is to hold those addresses and call from within the proc where the labels are (this way, it's safe as usual). But doing this I wouldn't need to compare/jump, only jump.

LordAdef

yes, I tried it.

it would be something like this :

theLabelproc
     mov eax, myStruct.labelPtr
     jmp eax

     foo1:
       ;code
       jmp done
     foo2:
       ;code
       jmp done
     foo3:
       ;code

done:
ret
theLabelproc endp

hutch--

I gather you know this in MASM.

  I_Am_A_Label:    ; label in procedure scope.
  Im_A_Global_Lbl::  ; a global scope label.

zedd151

What were the results?

You should code a small test piece, if you haven't already..

Then run it from a debugger like ollydbg stepping through the code to observe if the program flow is what you expect it to be.

And watch the stack, to see if you return where you should, as the second procedure exits.


Wish I was setup with a computer, I'm pretty sure it would work with careful coding.

zedd151

Quote from: hutch-- on April 15, 2018, 05:01:32 PM


  Im_A_Global_Lbl::  ; a global scope label.


lol

I knew I was missing something.    :redface:

Nevermind my posts above.   :icon_redface:

jj2007

Hutch gave you the answer. Now test it for educational purposes, e.g. jmp from one procedure to another one with a different stack setup. Have some fun, and don't forget: Olly is your friend :bgrin:


hutch--

Just be careful with grossly unstandard branching when using any debugger, if you start using truly exotic techniques, the debugger may not be able to follow it. IDA Pro can usually follow stuff like this but even it gets the hiccups on really weird stuff.

LordAdef

Quote from: hutch-- on April 15, 2018, 05:01:32 PM
I gather you know this in MASM.

  I_Am_A_Label:    ; label in procedure scope.
  Im_A_Global_Lbl::  ; a global scope label.



I didn't!!!!!!
Never had to use it. that's why it's sometimes good to try different things. Like this one

LordAdef

Quote from: jj2007 on April 15, 2018, 05:19:00 PM
Hutch gave you the answer. Now test it for educational purposes, e.g. jmp from one procedure to another one with a different stack setup. Have some fun, and don't forget: Olly is your friend :bgrin:

Cheers my friend!