LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Assembly - little problem with ret (http://www.linuxquestions.org/questions/programming-9/assembly-little-problem-with-ret-552947/)

cdog 05-10-2007 02:05 PM

Assembly - little problem with ret
 
Hei guys, I'm writting a small assembly problem that has to deal with a string received by value - I mean the entire string is in the stack. Everything works well until I have to return because I have to remove the string from the stack, but before I don't know from the start what's it's size in order to execute : ret size (where size=strlen*4). Is there a way to make a variable act as a constant for ret or maybe a workaround?
Thanks

leosgb 05-10-2007 11:12 PM

Hi, can you try to make your message clearer? Maybe some pseudo code could help.

If you are going to process yout string in the routine then you dont need to worry about its size. You can have the string size passed thru a variable and later you loop decrementing it until you are done and you pop the values from the stack.

In the end you will have your return address in the stack so you just have to return.

Is this what you are looking for?

Alien_Hominid 05-11-2007 12:26 AM

Maybe this:
pop value from stack
cmp it to what should be in the string
repeat it while they are different with loopnz
when different push value back to stack
now ret

Dox Systems - Brian 05-11-2007 12:26 PM

I'm curious as to what is passing you a string on the stack and not via a ptr! :-)

cdog 05-11-2007 12:33 PM

Quote:

Originally Posted by leosgb
Hi, can you try to make your message clearer? Maybe some pseudo code could help.

If you are going to process yout string in the routine then you dont need to worry about its size. You can have the string size passed thru a variable and later you loop decrementing it until you are done and you pop the values from the stack.

In the end you will have your return address in the stack so you just have to return.

Is this what you are looking for?

Not exactly. I cannot pop anything because the EIP is the last in the stack when the call to the function is made and "pop EIP" thriws an error: undefined symbol.

Alien_Hominid, as I said I cannot pop because of the EIP.
But I think I can save the last pushed dword into a register and after all the pops to push it back.

leosgb 05-11-2007 01:41 PM

I see, so in that case you just pop it first and store it in a register. Then you have all your string on the stack so you can pop it one by one. After you are done you should push your EIP back to the stack because ret will pop it automatically from there.

Maybe this is what you want:

pop Some_register // will get your return address stored in a register
while (not end of string)
{
pop AX // bring current char to AX for processing
process_data_AX
}
push Some_register // return your desired address to stack
ret // will fetch your correct return address.

Does it make sense? If you could copy and paste this portion of your code it would make it easier for people to help you. Guessing is hard :) Good luck.

Dox Systems - Brian 05-11-2007 03:35 PM

That seems a little dangerous. Wouldn't be easier to just say, move esp into ebp and look at values based on ebp-"x" (or is it "+"? Stack grows down, right?)? then you don't have to mess around with popping the ret address, only to have to store it and push it back on right before the ret...


All times are GMT -5. The time now is 04:30 AM.