【C语言常识】mdk:Inline assembler rules for compiler keywords __asm and asm

出处:官方编译器手册

The following rules apply to the __asm and asm keywords:

  • Multiple instructions on the same line must be separated with a semicolon (;).

  • If an instruction requires more than one line, line continuation must be specified with the backslash character (\).

  • For the multiple line format, C and C++ comments are permitted anywhere in the inline assembly language block. However, comments cannot be embedded in a line that contains multiple instructions.

  • The comma (,) is used as a separator in assembly language, so C expressions with the comma operator must be enclosed in parentheses to distinguish them:

    __asm
    {
        ADD x, y, (f(), z)
    }
    
  • Labels must be followed by a colon, :, like C and C++ labels.

  • An asm statement must be inside a C++ function. An asm statement can be used anywhere a C++ statement is expected.

  • Register names in the inline assembler are treated as C or C++ variables. They do not necessarily relate to the physical register of the same name. If the register is not declared as a C or C++ variable, the compiler generates a warning.

  • Registers must not be saved and restored in inline assembler. The compiler does this for you. Also, the inline assembler does not provide direct access to the physical registers. However, indirect access is provided through variables that act as virtual registers.

    If registers other than CPSR and SPSR are read without being written to, an error message is issued. For example:

    int f(int x)
    {
        __asm
        {
            STMFD sp!, {r0}    // save r0 - illegal: read before write
            ADD r0, x, 1
            EOR x, r0, x
            LDMFD sp!, {r0}    // restore r0 - not needed.
        }
        return x;
    }
    

    The function must be written as:

    int f(int x)
    {
        int r0;
        __asm
        {
            ADD r0, x, 1
            EOR x, r0, x
        }
        return x;
    }
    

展开阅读全文

c to asm!!!

10-19

请高手给我解释一下几个c语言到汇编原代码中间我觉得迷惑的地方,谢谢。我的解释和问题我写在<>中,而且编号rnrnrnrn--- E:\term3\mas.c ----------------------------------------------------------------------------------------------------rn1: int ff();rn2: int main()rn3: rn0040B4F0 push ebp rn0040B4F1 mov ebp,esp 〈设定新的基地址 问题1:为什么用esp替换ebp??>rn0040B4F3 sub esp,50h rn0040B4F6 push ebx rn0040B4F7 push esi rn0040B4F8 push edi rn0040B4F9 lea edi,[ebp-50h] rn0040B4FC mov ecx,14h rn0040B501 mov eax,0CCCCCCCCh rn0040B506 rep stos dword ptr [edi] rn4: char *f;rn5: int x,y,z;rn6: f=(char*)malloc(32*sizeof(char));rn0040B508 push 20h rn0040B50A call malloc (004026d0) rn0040B50F add esp,4 rn0040B512 mov dword ptr [ebp-4],eaxrn7: f[20]='a';rn0040B515 mov eax,dword ptr [ebp-4] rn0040B518 mov byte ptr [eax+14h],61h rn8: z=ff();rn0040B51C call @ILT+5(_ff) (0040100a)rn0040B521 mov dword ptr [ebp-10h],eax rn9: if(z>0)rn0040B524 cmp dword ptr [ebp-10h],0 rn0040B528 jle main+3Ch (0040b52c) rn10: goto test;rn0040B52A jmp test (0040b539) rn11: x=10;rn0040B52C mov dword ptr [ebp-8],0Ah rn12: y=x;rn0040B533 mov ecx,dword ptr [ebp-8] rn0040B536 mov dword ptr [ebp-0Ch],ecx rn13: test:rn14: free(f);rn0040B539 mov edx,dword ptr [ebp-4] rn0040B53C push edx rn0040B53D call free (00403110) rn0040B542 add esp,4 rn15: return 0;rn0040B545 xor eax,eax rn16: rn0040B547 pop edi rn0040B548 pop esi rn0040B549 pop ebx rn0040B54A add esp,50h rn0040B54D cmp ebp,esp rn0040B54F call __chkesp (0040b4b0)rn0040B554 mov esp,ebp rn0040B556 pop ebp rn0040B557 retrn--- No source file ------rnrnrn里面f()具体内容我没贴上来,不知道我的分析有什么不对的地方,我的麻烦大家了. 论坛

asm to c

10-25

以下程序用VC6.0 CL编译器编译rn编译参数 : cl test.c /link kernel32.librnrn程序在Win95下测试通过,运行结果:打开一个资源管理器rn简要说明:rn机器码在程序函数test中buffer里rn在test中通过rnpush addr buffer ; push ebp-80rnret 0 rn跳入机器码rnrn这段代码如下工作rnpush addr string1 ; string1 : "shell32.dll"rncall LoadLibraryrnrninvoke ShellExecute NULL,addr string2,0,0,0,5 ; string2 : "open" 打开资源管理器rnrnpush addr ExitProcessrnret 0 ;结束进程rnrn#includern#includernint test()rnrn// Follow Addr Tested on Windows95rn//LoadLibrary Addr: 0BFF775F3hrn//GetProcAddress Addr: 0BFF76DD8hrn//ShellExecute Addr: 07FDE9211hrn//ExitProcess Addr: 0bff8b191h rnrn char buffer[80]=rn rnrn 0xEB,0x1E, //jmp @callrn 0x5D, //pop ebp @jmrn 0x55, //push ebprn 0xFF,0x55,0x11, //call dword ptr [ebp+11h]rn 0x6A,0x05, //push 5rn 0x6A,0x00, //push 0rn 0x6A,0x00, //push 0rn 0x6A,0x00, //push 0rn 0x8B,0xC5, //mov eax,ebprn 0x83,0xC0,0x0C, //add eax,0Chrn 0x50, //push eaxrn 0x6A,0x00, //push 0rn 0xFF,0x55,0x15, //call dword ptr [ebp+15h]rn 0x68,0x91,0xB1,0xF8,0xBF, //push 0BFF8B191h ExitProcess 地址rn 0xC3, //retrn 0xE8,0xDD,0xFF,0xFF,0xFF, //call @jm @callrn 's','h','e','l','l','3','2','.','d','l','l',0,rn 'o','p','e','n',0,rn 0xf3,0x75,0xf7,0xbf, //LoadLibrary 地址rn 0x11,0x92,0xde,0x7f //ShellExecute 地址rn ;rn _asmrn rn pop edirn pop esirn pop ebxrn rn sub ebp,80rn push ebprn ret 0rnrn rnrnvoid main()rnrn rnrn HINSTANCE hw = LoadLibrary("user32.dll");rn// FARPROC fp = GetProcAddress(hw,"ShellExecute");rn// ShellExecute(0,"open",0,0,0,5);rn// printf("ShellExecute addr :%p",ShellExecute);rn/* printf("LoadLibrary Addr :%p\n",LoadLibrary);rn fp = GetProcAddress(hw,"GetProcAddress");rn printf("GetProcAddress Addr: %p\n",fp);rn printf("GetProcAddress Addr: %p\n",GetProcAddress);rn*/rn printf("%i\n",test());rnrnrn----------------------------------------------------------------rn我想把汇编的那一段改写成C语言,该如何写啊~~~rn开头的那个kernel32.lib又是什么东西? rnHELP!!!~rn rnrn 论坛

没有更多推荐了,返回首页