__asm("text:"); Function. Where is it defined?

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

__asm("text:"); Function. Where is it defined?

Post by Derek8588 »

I have searched high and low but I cannot find a header file that contains the function __asm(""); nor its args. I have seen it used such as this...

if(new_pad & PAD_CROSS)
{
__asm("cross:");

more code..
more code..

Any help is greatly appreciated. Thanks
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

It's not a function, it's a way of putting assembler inline with the C code. It's always compiler specific, and depending what you're compiling, the assembler may not be compatible with ps2.

Jim
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

ahh

Post by Derek8588 »

Oh, so basically __asm(""); allows you to perform assembly operations within your C source code? eh nifty. well so what would this accomplish?

__asm("cross:");
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

That just defines a label called 'cross' which can be the target of a branch or jump instruction. You should refer to the platform specific docs of 'gcc' and '(g)as' to see what else can go there.

Jim
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

ahh ok thanks. Also, do you happen to know what the _RESIDENT_ keyword means when declaring a variable? Im guessing it keeps the variable in memory regardless of what tries to erase it?
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

No idea. I guess it's a macro defined somewhere in one of the headers. Perhaps it defines a calling convention or a storage class.

Jim
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

I never found it, but o well. Sorry for asking so many questions, but where can I find a list of the Directives that the asm compiler supports?

such as...

.set

.ent

etc'
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

ty very much
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

Iv been struggling on this one for two nights. I have been trying to copy a function from one place in memory to another. Heres example code...

Code: Select all


some_function();  /* multiple lines of code within this function */

u32 memory;
u32 *buffer, *new_location;

buffer = &some_function;
memory = sizeof(buffer);

printf(" Some_function occupies %d bytes of memory\n", memory);

memcpy(new_location, buffer, sizeof(buffer));
sizeof returns 4 bytes of memory, and it should return much more. So only the first instruction of the function is copied to the new location in memory. Can anyone help me find the proper way to copy a function from one place to another? Thanks for all your help!

edit: ahh i discovered that its returning the size of the pointer var ( i think lol) Well the only thing I can think to do is create a loop that increments the address of where the function is stored in memory by 4. then check if its value is != 0 and then add 4 to a var. 4 bytes will be added to the var for every address that contains data. then when the end of the function is reached ( is = NULL), the size of the function will be contained in the variable. Ureeka! I am about to test this...lol
radad
Posts: 246
Joined: Wed May 19, 2004 4:54 pm
Location: Melbourne, Australia

Post by radad »

What makes you think the function is terminated with a NULL?
And why are you copying it? You cant guarantee that once it is copied that you can call it.
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

well instead of NULL i could use 0 or 0x00000000 correct?

and once its in memory I will call it with a JAL (addr)

yay! that method described above worked. used the value 0. idk why i said NULL. stupid me.
Mihawk
Posts: 29
Joined: Tue Apr 03, 2007 2:04 am

Post by Mihawk »

What makes you think the function is terminated with 0, 0x00000000 or whatever?

0x00000000 as an opcode would be a "nop" IIRC. So if you're lucky and you have a nop somewhere in the middle of your function only half of that function will be copied.

One method I used some time ago when I was doing something similar was to get the label/function after that function, then you just get the difference between those two.

Code: Select all

void theFunctionYouWantToCopy(void) { ... }

void someDummyFunction(void) {}

lenghtOfFunctionYouWantToCopy = (unsigned)someDummyFunction - (unsigned)theFunctionYouWantToCopy;
or something like that.
(But there might be the possibility that functions may be reordered (? I'm not sure)).
radad
Posts: 246
Joined: Wed May 19, 2004 4:54 pm
Location: Melbourne, Australia

Post by radad »

Thats what I meant. There is nothing to mark the end of a function. The best way is to get the address of the next function. That isnt always guaranteed to work but will most of the time.

And due to relative references a copy may not always work.
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

Well I understand what ur saying that if a function has a nop in the middle it will copy half. But I have been using SIOSHELL and insuring that the function that is being copied does not contain any nop's scattered throughout it. the function writes data to specified addresses. So when the nop after the jr ra is reached, the size is returned. I know its a screwy way to do it, but it works :). I will consider using your method described above sometime soon. Oh, heres a question. SIOSHELL runs in the background and waits for the terminal app to call it. How is it possible to do this? Like say I had a program in kernel memory (not cleared) that recieves and writes data to memory without interrupting the program currently running. Basically, like making SIOSHELL not interrupt the current process. Any suggestions? Thanks once again.
Post Reply