forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Inline asm

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
Kojima



Joined: 26 Jun 2006
Posts: 275

PostPosted: Thu Jul 06, 2006 9:50 pm    Post subject: Inline asm Reply with quote

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:


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
Back to top
View user's profile Send private message
Kojima



Joined: 26 Jun 2006
Posts: 275

PostPosted: Fri Jul 07, 2006 12:43 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Kojima



Joined: 26 Jun 2006
Posts: 275

PostPosted: Fri Jul 07, 2006 2:23 am    Post subject: Reply with quote

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:

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:

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);
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group