Page 1 of 1

Error: operand type mismatch for `mov'

Posted: Thu Aug 04, 2016 4:17 am
by osdever
I've encountered a problem with inline assembly. Maybe this is very nooby question, but why I'm getting this error:

Code: Select all

/tmp/ccgqP61A.s: Assembler messages:
/tmp/ccgqP61A.s:255: Error: operand type mismatch for `mov'
/tmp/ccgqP61A.s:255: Error: operand type mismatch for `mov'
in this code:

Code: Select all

void switch_task(struct task *task)
{
	asm("mov eax, %0;"
		"mov ebx, %1;"
		"mov ecx, %2;"
		"mov edx, %3;"
		"mov esi, %4;"
		"mov edi, %5;"
		"push %6;"
		"popf;"
		"mov esp, %7;"
		"mov cr0, %8;"
		"mov cr3, %9;"
		"jmp %10;" :
		"=g" (task->eax),
		"=g" (task->ebx),
		"=g" (task->ecx),
		"=g" (task->edx),
		"=g" (task->esi),
		"=g" (task->edi),
		"=g" (task->eflags),
		"=g" (task->stack),
		"=g" (task->cr0),
		"=g" (task->cr3),
		"=g" (task->eip));
}
? Tell me if you want some other info.

Re: Error: operand type mismatch for `mov'

Posted: Thu Aug 04, 2016 4:25 am
by heat
I'm afraid you wrote inline assembly using the Intel syntax, instead of the gcc-standard AT&T syntax.

Re: Error: operand type mismatch for `mov'

Posted: Thu Aug 04, 2016 5:02 am
by Octocontrabass
Any tasks that use EBP will have some trouble.

Clobbering registers without telling GCC is a bad idea.

It's impossible for GCC to generate valid code here - you're clobbering all of the registers it needs in order to access your task struct.

GCC uses AT&T syntax by default. If you want Intel syntax, make sure you're using the right command-line options.

The "g" constraint allows memory and immediate operands, but MOV with a control register as the destination only allows general purpose registers as the source.

Re: Error: operand type mismatch for `mov'

Posted: Thu Aug 04, 2016 10:43 am
by osdever
Octocontrabass wrote:GCC uses AT&T syntax by default. If you want Intel syntax, make sure you're using the right command-line options.
I've used -masm=intel in CFLAGS.
Didn't known about EBP and control registers' behaivor. Maybe I will remove them fully?

Re: Error: operand type mismatch for `mov'

Posted: Thu Aug 04, 2016 11:02 am
by Ch4ozz
Push all values from the struct onto the stack and it should work just fine.
Set EBP last and thats it.

Since you have to change all registers this isnt as easy as it seems because the data has to be stored atleast into one register so you can work with it.

Re: Error: operand type mismatch for `mov'

Posted: Thu Aug 04, 2016 12:42 pm
by iansjack
Personally, I would write this as a separate function in assembler rather than using in-line assembler.