intergrating ASM to C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

intergrating ASM to C++

 
0
  #1
Mar 28th, 2004
well lately I have been working on a CPU core for 6502 assembly, and I have been writing it all in c++. However for obvious reasons I was thinking of remaking the core in assembly. First I was wondering if it would be worth it (speed wise) to write functions in asm (inline?) and then call them in c++? or would there be another way to integrate an assembly to c++ that can tell assembly to run through certain stuff, yet still have access to the rest of the parts of the CPU the core is emulating (flags, registers, memory, etc)

right now when i do stuff like


  1. // addrmode() points to the addressing mode per opcode
  2. // m_A is the accumulator
  3. // m_S is the processor status register
  4. // bits- 1 - zero - first if statement
  5. // 7 - signed - second if statement
  6. void core6502::AND()
  7. {
  8. (this->*addrmode[m_opcode])();
  9. m_A = m_value; // this value was obtained by addrmode() above
  10. if(m_A)
  11. m_S &= 0xFD;
  12. else
  13. m_S |= 0x02;
  14. if(a & 0x80)
  15. m_S |= 0x80;
  16. else
  17. m_S = 0x7F;
  18. }
this is generally what my code looks like for each command, i tried to keep them all void because this function is actually just pointed to in many diferent cases, and addrmode points to one of the addressing functions so i dont have to write a different function for each opcodes addressing modes. Anyways would it truly be worth it to rewrite the asm? Because when I look at it, I couldn't imagine it would be much different either way

Also if inline is the way to go, any good resources on that heh. Like what registers am I free to use? Or can I use them and just restore later?
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: intergrating ASM to C++

 
0
  #2
Mar 28th, 2004
in 99.99% of cases it is much better to learn to write code that your compiler can optimize well. ie, compiling and observering generated code, and trying to fix your C to generate the proper asm.
asm resources:
http://cs.smith.edu/~thiebaut/ArtOfA.../artofasm.html
http://www.geocities.com/SiliconVall.../assembly.html

in short, on IA-32, registers eax, ecx, edx are called calllee save, that means the calling function must save them and you as the function being called are free to overwrite them. registers ebx, edi, esi are caller save, if u funk them up u have to save and restore them.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 8
Reputation: Aaerox is an unknown quantity at this point 
Solved Threads: 1
Aaerox Aaerox is offline Offline
Newbie Poster

Re: intergrating ASM to C++

 
0
  #3
Mar 30th, 2004
Originally Posted by BountyX
Also if inline is the way to go, any good resources on that heh. Like what registers am I free to use? Or can I use them and just restore later?
Basically, in c++, if you wanted to create a pure asm function, using inline, you'd do something similar to this:

  1. void __declspec(naked) AddTwoNumbers(int* storehere, int number1, int number2)
  2. {
  3. _asm {
  4. mov eax, dword ptr ds:[esp+8]
  5. mov ecx, dword ptr ds:[esp+0xC]
  6. add eax, ecx
  7. mov edx, dword ptr ds:[esp+4]
  8. mov dword ptr ds:[edx], eax
  9. retn
  10. }
  11. }

This function would work fine, as it does not modify ebx/edi/esi, as infamous said. However, if you needed to modify them for some reason or another, you could do this:

  1. void __declspec(naked) AddThreeNumbers(int* Storehere, int num1, int num2, int num3)
  2. {
  3. _asm {
  4. push esi
  5. push edi
  6. push ebx
  7. mov esi, dword ptr ds:[esp+8]
  8. mov edi, dword ptr ds:[esp+0xC]
  9. mov ebx, dword ptr ds:[esp+0xF]
  10. add esi, edi
  11. add esi, ebx
  12. mov edi, dword ptr ds:[esp+4]
  13. mov dword ptr ds:[edi], esi
  14. pop ebx
  15. pop edi
  16. pop esi
  17. retn
  18. }
  19. }

Then, to call these functions, you simply just need to do this:
  1. int* Store;
  2. AddTwoNumbers(Store, 1, 3);
  3. AddThreeNumbers(Store, 4, 7);

Hope that helped :o
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: intergrating ASM to C++

 
0
  #4
Mar 30th, 2004
thanks
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 8
Reputation: Aaerox is an unknown quantity at this point 
Solved Threads: 1
Aaerox Aaerox is offline Offline
Newbie Poster

Re: intergrating ASM to C++

 
0
  #5
Mar 31st, 2004
Another note: If you're using inline asm in the middle of a function, you'll want to keep to be using eax, ecx and edx. Some compilers keep registers like esi zeroed all the way through a function to optimize (push NULL takes more opcodes than push esi), therefore if you modified esi you'd be messing up the whole function, which can be suprisingly hard to debug. :]
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1
Reputation: tomato is an unknown quantity at this point 
Solved Threads: 0
tomato tomato is offline Offline
Newbie Poster

Re: intergrating ASM to C++

 
0
  #6
Jul 14th, 2004
how i using assembly in c(microdoft visual c/c++)
i need inline assembly basics
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 1
Reputation: naradasu74 is an unknown quantity at this point 
Solved Threads: 0
naradasu74 naradasu74 is offline Offline
Newbie Poster

Re: intergrating ASM to C++

 
0
  #7
Nov 9th, 2008
hey just try this in the Turboc++ v3
the following is a code to link asm with the c++ compiler
  1. #include<iostream.h>
  2. void main()
  3. {
  4. int i=3,j=4,k;
  5. asm{ //'{' this must be put immediate to the 'asm' key word else code showserrors
  6. mov ax,i;
  7. add ax,j;
  8. mov k,ax;
  9. }
  10. cout<<k;
  11. }
  12.  
  13. the o/p will be : 7
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC