Author Topic: Help understanding basics of call-return in assembly.  (Read 3822 times)

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
I am trying to centre the description texts in the menus (at the moment they all have fixed X position).  I have created some code to do this job (and a function already exists to count the width of all letters in a string).  So the original code is this:

Code: [Select]
Push 2D [X Position of the description text string.  This is the value the new code will change]
call 006F5B03
add esp,14

Code: [Select]
and the new is this:

call 00914237 [Jump to a blank area that I can add my own code to
The 2 instructions below (call 006F5B03 and add esp,14 ) are now nopped]

Code: [Select]
and at 00914237:

push ebp
mov ebp,esp
mov eax,009A7FF0
push eax [Push string address]
call 006F54A2 [Adds all character widths in string]
add esp,04
cdq
sub eax,edx
sar eax,1
mov edx,00000140
sub edx,eax
push edx [pushes new X position of the string]
call 006F5B03 [and now we are back to the original code]
add esp,14
mov esp,ebp
pop ebp
ret

This simply doesn't work (because I lack knowledge on how the stack works), so I am doing something very wrong.  It returns to the completely wrong address.

Questions:

1. When making a call, do you always need to use push ebp mov ebp,esp / mov esp,ebp pop ebp ?

2. Is there a better way of doing the above

3. How do I ensure this call will jump back to the correct address?





« Last Edit: 2013-12-28 07:35:07 by DLPB »

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: Help understanding basics of call-return in assembly.
« Reply #1 on: 2013-12-28 14:53:53 »
Aali has fixed this issue with a workaround way of popping the return address into ebx and then pushing it at the end of function.  But I'd still appreciate anyone explaining how a normal call return works, and why the above doesn't work.

edit.

I understand now.  This thread can be deleted.  :)
« Last Edit: 2013-12-28 16:14:12 by DLPB »

Micky

  • *
  • Posts: 300
    • View Profile
Re: Help understanding basics of call-return in assembly.
« Reply #2 on: 2014-01-02 10:01:41 »
I understand now.  This thread can be deleted.  :)
That is not going to help anyone who comes across this thread from google in the future. ;)

nfitc1

  • *
  • Posts: 3013
  • I just don't know what went wrong.
    • View Profile
    • WM/PrC Blog
Re: Help understanding basics of call-return in assembly.
« Reply #3 on: 2014-01-02 11:56:14 »
That is not going to help anyone who comes across this thread from google in the future. ;)
Yeah. Care to share with the rest of the class?

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: Help understanding basics of call-return in assembly.
« Reply #4 on: 2014-01-02 13:04:03 »
That is not going to help anyone who comes across this thread from google in the future. ;)

That's why I asked for the thread to be deleted.  It's just basic assembly and chances are if you even grasp the basics you will have an answer for why this didn't work.  For a start off, the function added 4 bytes to the stack, then I added a further 4 bytes with the push...  This could never work.  By doing what Aali suggested and simply saving the top of the stack to ebx with a pop, I preserved the stack position and then got the return address at the end (by pushing ebx).  My function is really a jump mechanism. (I couldn't use jump because this needs to be used over 10 times).

Unless someone has a better idea how to make this work?  8)
« Last Edit: 2014-01-02 13:07:34 by DLPB »