| | |
intergrating ASM to C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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?
right now when i do stuff like
C++ Syntax (Toggle Plain Text)
// addrmode() points to the addressing mode per opcode // m_A is the accumulator // m_S is the processor status register // bits- 1 - zero - first if statement // 7 - signed - second if statement void core6502::AND() { (this->*addrmode[m_opcode])(); m_A = m_value; // this value was obtained by addrmode() above if(m_A) m_S &= 0xFD; else m_S |= 0x02; if(a & 0x80) m_S |= 0x80; else m_S = 0x7F; }
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
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
•
•
Join Date: Mar 2004
Posts: 77
Reputation:
Solved Threads: 2
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.
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.
•
•
Join Date: Mar 2004
Posts: 8
Reputation:
Solved Threads: 1
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
void __declspec(naked) AddTwoNumbers(int* storehere, int number1, int number2) { _asm { mov eax, dword ptr ds:[esp+8] mov ecx, dword ptr ds:[esp+0xC] add eax, ecx mov edx, dword ptr ds:[esp+4] mov dword ptr ds:[edx], eax retn } }
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)
void __declspec(naked) AddThreeNumbers(int* Storehere, int num1, int num2, int num3) { _asm { push esi push edi push ebx mov esi, dword ptr ds:[esp+8] mov edi, dword ptr ds:[esp+0xC] mov ebx, dword ptr ds:[esp+0xF] add esi, edi add esi, ebx mov edi, dword ptr ds:[esp+4] mov dword ptr ds:[edi], esi pop ebx pop edi pop esi retn } }
Then, to call these functions, you simply just need to do this:
C++ Syntax (Toggle Plain Text)
int* Store; AddTwoNumbers(Store, 1, 3); AddThreeNumbers(Store, 4, 7);
Hope that helped :o
•
•
Join Date: Mar 2004
Posts: 8
Reputation:
Solved Threads: 1
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. :]
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
hey just try this in the Turboc++ v3
the following is a code to link asm with the c++ compiler
the following is a code to link asm with the c++ compiler
C++ Syntax (Toggle Plain Text)
#include<iostream.h> void main() { int i=3,j=4,k; asm{ //'{' this must be put immediate to the 'asm' key word else code showserrors mov ax,i; add ax,j; mov k,ax; } cout<<k; } the o/p will be : 7
![]() |
Similar Threads
- My Sample ASM Code (Assembly)
- asm in C (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ GUI help
- Next Thread: Encoder
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





