943,746 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 43258
  • C++ RSS
Mar 28th, 2004
0

intergrating ASM to C++

Expand Post »
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


C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Mar 28th, 2004
0

Re: intergrating ASM to C++

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.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 30th, 2004
0

Re: intergrating ASM to C++

Quote 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:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. int* Store;
  2. AddTwoNumbers(Store, 1, 3);
  3. AddThreeNumbers(Store, 4, 7);

Hope that helped :o
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Aaerox is offline Offline
8 posts
since Mar 2004
Mar 30th, 2004
0

Re: intergrating ASM to C++

thanks
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Mar 31st, 2004
0

Re: intergrating ASM to C++

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. :]
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Aaerox is offline Offline
8 posts
since Mar 2004
Jul 14th, 2004
0

Re: intergrating ASM to C++

how i using assembly in c(microdoft visual c/c++)
i need inline assembly basics
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomato is offline Offline
1 posts
since Jul 2004
Nov 9th, 2008
0

Re: intergrating ASM to C++

hey just try this in the Turboc++ v3
the following is a code to link asm with the c++ compiler
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
naradasu74 is offline Offline
1 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ GUI help
Next Thread in C++ Forum Timeline: Encoder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC