Inline assembly in C: Dynamic registers?

Up vote 1 down vote favorite share g+ share fb share tw.

I'm trying to inline some assembly code in my C code: __asm { mov reg,val }; The problem: I want to define the register and value dynamically. I know the 'val' can be a variable written in the C code, but I don't know how can I choose the register dynamically (i. E decide according to user input- register 'dh' or 'dl').

Any suggestions? C assembly inline-assembly link|improve this question edited Nov 14 '09 at 9:15starblue19.7k22258 asked Oct 1 '09 at 9:45Eldad260213 91% accept rate.

Use an enum and switch in the C-code: typedef enum { R_AL, R_AH, R_AX, R_EAX, ... } REGS; ... REGS nReg; ... switch (nReg) { case R_AL: __asm { mov al,val } break; case R_AH: __asm { mov ah,val } break; case R_AX: __asm { mov ax,val } break; ... }.

Well ... That would require you to modify the code at run-time. The __asm { } construct happens all at compile-time, so you can't affect its contents later. Of course, self-modifying code is not exactly what modern operating systems are set up to do most easy, so you're going to have to jump through a few hoops (cache flushing, code being in non-writable segments, and so on).

Update: Of course you might be able to use slashmais's technique and switch between a set of pre-compiled versions, but I'd be scared about mixing code at that level (register clobbering comes to mind).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions