Inline asm

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

Moderators: cheriff, Herben

Post Reply
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Inline asm

Post by Kojima »

Hi,

I want to use some inline asm in my raycaster lib to speed up the floors etc, can you clear a few things up for me so I can determine if it's possible?

1, can you use a C++ defined variable/pointer in the asm?

i.e

Code: Select all


int p = 25;
int n = 20;
int * pp = &p;
int * p2 = *p2;
int output=0;
int * op = &p;

inline asm volatile{
"lw $4,pp\n"
"lw $5,p2\n"
"addui $6,$5,$4\n"
"sw $6,op\n"
}

Which shold take pp,p2, add them together and store the result in op.

That's semi-pesudeo code, but what would the real code look like if it's possible? Or do I have to use a preset part of memory and use the memory address to access it? (I take it that's fairly easy on a closed system like the ps2)

Thanks
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

Also, are there any demos out there that use inline asm I could learn from? I had a look and did a site search but had no luck.
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

I worked it out thanks to the site search function and the GCC docs.

Here's an example for anyone looking to get started, it's fairly easy once you get past the wierd syntax(To me it's wierd anyway)

Code: Select all

void AddTest()
{
int num1=5;
int num2=10;
int out=0;
asm __volatile__ ("\n\ 
mult %0,%1,%2\n\
":"=r"(out):"r"(num1),"r"(num2));
printf("Result:%d \n",out);
}
basically "=r" means the variable you specify next will be used as the output. "r" on it's own means the variable is an input. you then use %1/%2 etc to access the register (Which is indetermined) the varible was loaded into. and you direct your output to %0 to output a register/op into the variable.

Not sure if you can handle multiple outputs, I would expect so though.

One thing i'm not sure of though, is how do you directly access a register, is it similar asm like $0 for the all zero register, utpo $30 or can you use the mnemonic names? if so, do I have to format it in any special way?

----

Edit - Can anyone see why this returns the right value, 50 in output 1, yet returns 250 in output 2?

Code: Select all

void AddTest()
{
int num1=5;
int num2=10;
int out=0;
int out2=0;
asm __volatile__ ("\n\ 
mult %0,%2,%3\n\
mult %1,%2,%3\n\
":"=r"(out),"=r"(out2):"r"(num1),"r"(num2));
printf("R1:%d R2:%d \n",out,out2);
}


edit - hmm, even this code results in an output of 250 of a, and out2 which is not even passed is now 5. Why is altering other variables and multiplying in on it's self?
Any ideas why?

void AddTest()
{
int num1=5;
int num2=10;
int out=0;
int out2=0;
asm __volatile__ ("\n\
mult %0,%1,%2\n\
mult %0,%1,%2\n\
":"=r"(out):"r"(num1),"r"(num2));
printf("R1:%d R2:%d \n",out,out2);
}
Post Reply