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

// 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()
 m_A = m_value; 	// this value was obtained by addrmode() above
   m_S &= 0xFD;
   m_S |= 0x02;
 if(a & 0x80)
   m_S |= 0x80;
   m_S = 0x7F;

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?

12 Years
Discussion Span
Last Post by naradasu74

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:

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.


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:

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

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:

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

Then, to call these functions, you simply just need to do this:

int* Store;
 AddTwoNumbers(Store, 1, 3);
 AddThreeNumbers(Store, 4, 7);

Hope that helped :o


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. :]


how i using assembly in c(microdoft visual c/c++)
i need inline assembly basics


hey just try this in the Turboc++ v3
the following is a code to link asm with the c++ compiler

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;

the o/p will be : 7
This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.