Including asm in C source

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

Including asm in C source

Post by Derek8588 »

I believe its called inline asm. But anyway. In my source code, main() calls and stores a function at a specific memory address. Further within my program, I want to jump to this functions memory adr to execute it.
so how would I set this up?

asm __volatile__("\n\
j $Address\
");

I know thats wrong because the compiler throws errors at me. Whats the correct way to do this, besides calling the function by its name..? Thx
User avatar
Lukasz
Posts: 248
Joined: Mon Jan 19, 2004 8:37 pm
Location: Denmark
Contact:

Post by Lukasz »

http://www.ibiblio.org/gferg/ldp/GCC-In ... HOWTO.html

If you want to call a function you need to use the instruction "jal" instead of "j", so the return address is stored in a register and check the link above for GCC inline syntax.

I'd recommend compiling some C source and using ee-objdump to disassemble the code and see what asm GCC generates and try to understand it, instead of creating new threads and asking trivial questions such as these, as you will get ignored eventually.

As for a EE instruction manual, use this: http://lukasz.dk/files/tx79architecture.pdf

Spend alot studying these things yourself, this way you will learn more and be able to solve problems on your own.
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

I appreciate it. I know it was useless to make a thread to ask such a simple question. I spent approx 1-2 hrs google searching for the syntax of a j and jal op in asm. I have a basic understand of MIPS and its op codes. I'm just not to sure as of how to write it with the correct syntax in a C source file. All the ones that I have seen dont give an immediate value, but rather a variable.

jal some_value

I have tried preloading a variable pointer with an addr and attempt to jump to it. It says "undefined reference to the some_value variable." Even though it was just previously declared.. But anyway. Thanks for your help. I wish there was a IRC channel that actually had dev's on it.
#PS2DEV at efnet never has anyone on it. I would have asked in this mannor, but I couldnt. So my last resort was to turn here to the forums. As you can see I am rather new to PS2 programming. I took it up as a hobby until I go back to college...

Well I will read the docs you gave me and try the obj-dump in gcc. If all else fails, I will figure out some way to impliment this. Thanks
ragnarok2040
Posts: 202
Joined: Wed Aug 09, 2006 1:00 am

Post by ragnarok2040 »

http://ps2dev.ofcode.com/modules/wordpress/?page_id=37

That site has a ton of documents about the EE as well including opcodes and registers and etc. Maybe it'll be of some help.
Mihawk
Posts: 29
Joined: Tue Apr 03, 2007 2:04 am

Post by Mihawk »

I'm not sure, but are you looking for something like this?

Code: Select all

void* ptr = specificMemoryAddress;
asm __volatile__(
	"jalr	$ra,%0\n"
	"nop\n"
	:
	: "r"(ptr)
	:
);
Derek8588
Posts: 62
Joined: Fri Oct 22, 2004 2:14 pm

Post by Derek8588 »

Thanks for all your help. Carefully reading through the Inline Assembly Tutorial that Lukasz pointed me to, and 20 min of fiddling around. I finally figured it out. Heres what I was trying to do.

Code: Select all

asm __volatile__(
   "jal\t 0x01bdea28\n\t"
   );
So simple eh? Heh. One of my previous roadblocks was that I was trying to prefix the address to be jumped to with a $ and not 0x. I even forgot the "" around each line. And another occured when I was using variable names with other opcodes, such as v0. I never including the proper header (as_reg_compat.h) Stupid me :( Once again, thanks for having patience with me.
Post Reply