Page 1 of 1
What is wrong with this [ASM/C]
Posted: Sun May 28, 2006 3:03 pm
by earlz
I making a bit of a asm library in C but am having some trouble getting SetCS() to work I'm thinking that their is some protection mechanism or something that is causing the triple fault
Code: Select all
inline void SetCS(unsigned short seg,void *where){
x86_tmps=seg;
x86_tmpi=(unsigned int)where;
__asm(".intel_syntax noprefix\n"
"push ss\n"
"push esp\n"
"pushf\n"
"push [_x86_tmps]\n"
"push [_x86_tmpi]\n" //above is for the extra iret stuff
"iret\n"
".att_syntax\n");
}
btw I'm running it in ring0 the whole time
when I use seg as 0 bochs gives the error "return cs is null selector" just as i planned but when i try actually giving it a valid code sector it triple faults
Re:What is wrong with this [ASM/C]
Posted: Sun May 28, 2006 8:18 pm
by bubach
checked with bochs debugger where in mem you end up?
Re:What is wrong with this [ASM/C]
Posted: Mon May 29, 2006 2:05 am
by earlz
debugger just does not really work for me
[at the end register thingy]
it says cs is the same(0x10) and the eip is probably the same place too(its in the same area)
Re:What is wrong with this [ASM/C]
Posted: Mon May 29, 2006 3:07 am
by Ryu
Usually, at least for VC6.0, it will calculate the relative offsets of your C variables however I'm not sure after you instruct a assembly instructions that modifies the stack it will know about it.
"push [_x86_tmps]\n"
"push [_x86_tmpi]\n"
So maybe check this first, if _x86_tmps and _x86_tmpi are pointed in the correct offset of ESP. Another thing I'm wondering why are you doing this, and after doing the jump will it restore the stack?
Re:What is wrong with this [ASM/C]
Posted: Mon May 29, 2006 5:06 am
by Pype.Clicker
i discourage you to use [_tmp_xxx] directly from your inline assembly. For own purpose, the compiler might decide that _tmp_eip should be optimized into a register, for instance, making the meaning of "push [_tmp_eip]" ambiguous at best.
GCC's inline assembly instead allows you to use "parameters" (e.g. %0, %1, ...) on which you specify constraints. See
the FAQ for details:
Re:What is wrong with this [ASM/C]
Posted: Mon May 29, 2006 5:52 am
by Ryu
Yes, I agree with Pype.Clicker. I assume _x86_tmps and _x86_tmpi are in gobal space but I was trying to point out also that where those location would be is ambigous. This is up to the compiler to find exactly where it is, if the compiler decides to use ESP or EBP or etc as it base then its obviously going to point to the wrong memory location.
You should always watch out for two pushes using a memory operand with a base register and relative offset. Where the second push wouldn't be the locating at where the offset is suppose to take you since the base was adjusted.
push [esp+offset _tmpi]
push [esp+offset _tmps]
In most cases for global varibles the compiler may address the push as immediate which would be okay, but still ambigous on differnt circumstances, and you can't assume this even if it does compile this way on this specific function.
push ds:[0x123123] ; address of _tmpi
push ds:[0x321321] ; address of _tmps
Its a bad idea to wrap up something like this in C in my opinion. If your not aware, inlines may also produce a prologue and epilogue and be treated as a normal function.