Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

inline assembler help

This topic is 2636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


#include <iostream>

int iVariable = 2;
int *piVariable = NULL;

int main()
{

std::cout << "Default Values:" << std::endl;
std::cout << "int iVariable = " << iVariable << std::endl;
std::cout << "piVariable = " << piVariable << std::endl;
std::cout << "*piVariable = " << piVariable << std::endl;


// iVariable += 1;
__asm {
MOV EAX, iVariable
ADD EAX, 1
MOV iVariable, EAX
}

// output
std::cout << "iVariable should be = 3" << std::endl;
std::cout << iVariable << std::endl;


//piVariable = &iVariable
__asm {
LEA EAX, iVariable // EAX = &iVariable
MOV piVariable, EAX // piVariable = EAX
}

// output
std::cout << "pVariable should be = &iVariable" << std::endl;
std::cout << "&iVariable = " << &iVariable << std::endl;
std::cout << "pVariable = " << piVariable << std::endl;
std::cout << "*Pvariable should be 3" << std::endl;
std::cout << "*pVariable = " << *piVariable << std::endl;


//////////////////////////////////////////////////

//*piVariable += 1
__asm {
mov EAX, [piVariable]
add EAX, 1
mov [piVariable], EAX
}


std::cout << "*pVariable should be = 4" << std::endl;
std::cout << "*pVariable = " << *piVariable << std::endl;
std::cout << "iVariable should be = 4" << std::endl;
std::cout << "iVariable = " << iVariable << std::endl;

//////////////////////////////////////////////////////

getchar();
return 0;
}




//////////////////////////////////////////////////

//*piVariable += 1
__asm {
mov EAX, [piVariable]
add EAX, 1
mov [piVariable], EAX
}


std::cout << "*pVariable should be = 4" << std::endl;
std::cout << "*pVariable = " << *piVariable << std::endl;
std::cout << "iVariable should be = 4" << std::endl;
std::cout << "iVariable = " << iVariable << std::endl;

//////////////////////////////////////////////////////


is giving incorrect results... am i performing the assembly wrong? then end results are

*piVariable = 0
iVariable = 3

Share this post


Link to post
Share on other sites
Advertisement
I don't see anything wrong from a glance. Can you post the whole output log from the program? This is VC++? Or you can remove the logging, to simplify compiled output, and step it in disassembly debugger. I remember there was some confusion in syntax last time I did this.

Its good to learn asm. Though in the end the __asm will disable all the optimizations for all code which sees it, and it will be a performance hit. You should try different ways, and see the compiler output to see the impact

EDIT: The simd intrinsics had some inefficiencies too with VC9 but VC10 improved a lot with them. You can do pretty efficient matrix mul with one-line c++ with intrinsics. BTW what are you trying to do?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!