Pelles C forum

Assembly language => Assembly discussions => Topic started by: CommonTater on January 05, 2012, 10:18:54 AM

Title: A couple of Inline ASM questions...
Post by: CommonTater on January 05, 2012, 10:18:54 AM
Hi guys...
 
Messing with a little inline ASM and ran into a couple of questions...
 
1) If I am working with registers and the stack, I understand that I have to leave it as I found it or the return call won't work from the function I'm in.  However, I need to do something rather odd... I need to leave a pointer address on the stack at the beginning of a function, to be found by another function (always called next, but not called by my code) ... is there a way to do this without trashing the stack?
 
2) Can someone please explain __declspec(naked) for me?  I get that it's not using any entry or exit processing but, the concept is a bit unclear.  Is this a calling convention of it's own or is it to be used with other calling conventions... eg: __declspec(naked) __cdecl MyFunction ....
 
Thanks!
 
 
Title: Re: A couple of Inline ASM questions...
Post by: czerny on January 05, 2012, 01:45:34 PM
Hallo Commontater,

maybe I misunderstood your first question. But what about the following:

1. the first function pushes the pointer on the stack.
2. it swaps the pointer and his own ret-adress and returns.
3. the  second function swaps his ret-adress and the ponter.
4. and  pops the pointer.

czerny
Title: Re: A couple of Inline ASM questions...
Post by: CommonTater on January 05, 2012, 03:17:44 PM
Hallo Commontater,

maybe I misunderstood your first question. But what about the following:

1. the first function pushes the pointer on the stack.
2. it swaps the pointer and his own ret-adress and returns.
3. the  second function swaps his ret-adress and the ponter.
4. and  pops the pointer.

czerny


Thanks for the suggestion...

It's just a data pointer I have to get between two functions but one is not calling the other because there's a few lines of C code between them.  I don't think I need to flip return addresses... but then being new to x86 ASM I'm not totally sure of anything at this point :D

Title: Re: A couple of Inline ASM questions...
Post by: czerny on January 05, 2012, 05:00:34 PM
It is a little bit difficult to understand why you have to go this way. Are both functions written by you, or is the second a foreign function. How is it called (stdcall, cdecl, other)?

czerny
Title: Re: A couple of Inline ASM questions...
Post by: Vortex on January 05, 2012, 08:28:14 PM
Hi CommonTater,

A function specified with the  __declspec(naked) convention does not have a stack frame created automatically by the compiler. You need to manipulate yourself the stack to access parameters. Here is a quick example :

Code: [Select]

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

__declspec(naked) char* UpperCase(char* szText) /* Turn off framing to get
a smaller sized function */
{
__asm{
mov eax,[esp+4] /* get the address of the string to be converted */
sub eax,1
__repeat:
add eax,1
movzx ecx,BYTE PTR [eax]
test ecx,ecx
je __end
cmp ecx,97 /* if ASCII(ecx) < 97 then ignore the charater */
jb __repeat
cmp ecx,122 /* if ASCII(ecx) > 122 then ignore the charater */
ja __repeat
sub BYTE PTR [eax],32 /* convert lowecase to uppercase */
jmp __repeat
__end:
mov eax,[esp+4]
ret 4 /* balance manually the stack */
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
char msg[]="inline assembly programming";
MessageBox(0,UpperCase(msg),"Hello!",MB_OK);
return 0;
}

EDIT : Manual stack balancing with RET 4
Title: Re: A couple of Inline ASM questions...
Post by: CommonTater on January 05, 2012, 11:33:03 PM
@Vortex... thanks for the example, it answered the question perfectly.

It is a little bit difficult to understand why you have to go this way. Are both functions written by you, or is the second a foreign function. How is it called (stdcall, cdecl, other)?

czerny

That's ok... I don't fully understand this yet, either...
 
Picture a function... lets call it int DeGarbel(int a, int b) ... As part of the process I'm using 2 pieces of inline assembly.  I have a choice... I can put them into the DeGarbel function itself, but since they can be used in other places in this code, that would mean writing them several times.  It struck me as being easier if I make them into functions and call them.  The problem is that the first one creates data the second one has to find.... This leaves me a couple of choices, I could use a C variable (which is on the stack anyway) or I could "invisibly" push something onto the stack, execute the C code in between then pop it off from the other one.  The problem was that the way I was doing it was disturbing the stack causing some very interesting side effects...

The closest I got was with __declspec(naked) but I wasn't understanding how that worked well enough to finalize it.

Thing is I'm new enough at ASM that it's making me dizzy :D  ... rather steep learning curve, this.

Title: Re: A couple of Inline ASM questions...
Post by: Vortex on January 06, 2012, 08:02:22 PM
Quote
Thing is I'm new enough at ASM that it's making me dizzy   ... rather steep learning curve, this

Hi CommonTater,

No any problem. Coding with asm is fun. At the beginning, it can be a bit difficult but after you will enjoy it a lot. There are very good sources to learn Win32 asm. One of the best is Iczelion's site :

http://win32assembly.online.fr

You can use Pelle's Poasm for your asm projects. It provides HLL constructs and has a powerful macro engine.
Title: Re: A couple of Inline ASM questions...
Post by: CommonTater on January 07, 2012, 01:57:21 AM
Thank you Vortex... I already discovered Iczelion's site... and, I thought I recognized one of the contributors :D

I'm not going to get too deeply into ASM at this point.  I used to use it on the Z-80 machines and quite thoroughly enjoyed it... but that was 30 years ago and I was 30 years brighter back then... these days I'm quite happy to mess with experimental code in C...

Your help was appreciated, thanks again.