Inline assembly with Pelle's C

Started by Vortex, January 08, 2005, 10:49:16 AM

Previous topic - Next topic

Vortex

Hi friends,

Here is an example of using inline assembly with Pelle's C. The original source code of szUpper comes with Hutch's masm32 package.


#define WIN32_LEAN_AND_MEAN
#include <windows.h>

char *szUpper(char *text)
{
__asm{
mov eax, text
dec eax
@repeat:
add eax, 1
cmp BYTE PTR [eax], 0
je @end
cmp BYTE PTR [eax], "a"
jb @repeat
cmp BYTE PTR [eax], "z"
ja @repeat
sub BYTE PTR [eax], 32
jmp @repeat
@end:
}
return text;
}


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

petezl

Hi Vortex,
Thanks for another interesting forum. ..
Having done some minimal in-line assembly with Digital Mars. I'm looking forward to having a bash with Pelle'sC.
I did a major re-organisation of my HD so will be a while before i get it all installed again. I also lost my password for Pelle's forum, will have to get back there soon to thank him for some links he gave me.
Peter.
Cats and women do as they please
Dogs and men should realise it.

Vortex

Hi Peter,

I hope you will recover successfully your system.

Glad to learn that you will rejoin Pelle's forum :U


Vortex

Here is another method, the C source code:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

extern char *szUpper(char *text);

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


Pelle's C compiler, pocc.exe accepts asm source files based on a variant of NASM syntax:

[cpu 486]
[global _szUpper]
[section .text]
[function _szUpper]
_szUpper:
push ebp
mov ebp,esp
mov eax,[EBP+8]
dec eax
@restart:
add eax,1
cmp byte [eax],0
je near @end
cmp byte [eax],97
jb near @restart
cmp byte [eax],122
ja near @restart
sub byte [eax],32
jmp near @restart
@end:
mov eax,dword [ebp+8]
mov esp,ebp
pop ebp
ret

[attachment deleted by admin]

petezl

Vortex,
That code you posted shows the way to completing my original aim of creating an asm project for the PPC. A nice addition to Pelles soft. All I have to do now is learn a bit about NASM.
Peter.
Cats and women do as they please
Dogs and men should realise it.

Farabi

How to debuging it if I want to see the code generated?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

GregL

Farabi,

If you don't already have Pelle's C, you need to get it. Open the project. Do a debug build. Start the debugger. Right click on the source code and select Show assembly.

Or, you could use OllyDbg (with no debug information).


PellesC

...or possibly add an "int 3" instruction somewhere, which will give the debugger a reason to break...

(int 3 = interrupt 3 -- trap to debugger = opcode 0xCC)

Pelle

Farabi

Quote from: Greg on September 24, 2005, 04:21:15 AM
Farabi,

If you don't already have Pelle's C, you need to get it. Open the project. Do a debug build. Start the debugger. Right click on the source code and select Show assembly.

Or, you could use OllyDbg (with no debug information).




I have it. But dont know how to start. But now I know how to use it after see a few example.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Vortex

Hi Onan,

Debugging is easy with PellesC, here is the debug version of my inline assembly example.

[attachment deleted by admin]

noman9607

Vortex,
Thanks for the debug project, I just double clicked on the  project file and Pelle's ide came up then I clicked on debug and I was good to go in about 2 sec. One thing about the microsoft debugger I like when f8 or single stepping is that the register(s) that change state in that step are in red so you can catch something you are not specially looking for.

GregL

While trying out Pelles C 4.50, I updated Vortex's example posted above (Inline2.zip) to work with the new version. I had to rewrite the asm files, they now need to be in POASM format.



[attachment deleted by admin]

Vortex

Hi Greg,

Thanks for updating the example, nice work :U

OzzY

QuotePelle's C compiler, pocc.exe accepts asm source files based on a variant of NASM syntax
I didn't know that. So , why Pelle is writting PoASM?

Vortex

Hi Ozzy,

Pelle released Poasm towards the end of 2005. He coded it to support his software development package. Poasm didn't exist when I created this topic.