I'm designing a little preprocessor for source code files based on a language of my own design.The compiler for the language will be written in C and generates instructions for a "virtual machine" that will execute the instuctions and generate a new source code file for input to whatever compiler/assembler one might be using. Anyway, the language should implement a generic reference type that can point to any data object that can be declared in the program , so I'll implement the generic pointer type as an "int" in C and cast the address of any variable pointed to as the "int" type. To dereference a generic pointer type I wrote the following type of function ( one for each different type that one can dereference to ) :
char dereferenceChar ( int address , int dereferenceLevel )
{
char ch;
_asm{
push eax //save eax
push ecx //save ecx
mov ecx , dereferenceLevel //use ecx to count
mov eax , address //move address to dereference to eax
start:
cmp ecx , 0 //check if the count is 0
je done // no more dereferencing to do
mov eax , dword ptr [ eax ] //dereference eax and place in eax
dec ecx //decrement the count
jmp start //jump to start of loop
done:
mov ch , al //store the character in ch
pop ecx //restore registers
pop eax
}
return ch;
}
Is this a good way to achieve the desired outcome ( my assembler is a little rusty but I'm currently reading "Write Great Code" by Randall Hyde so it should improve :bg ) ? Anyone have a different solution?