用VC写的程序,因为效率的问题对几个函数希望用汇编改写。
请问可不可以直接在VC编译器中实现,还是必须用汇编写库供VC调用?

解决方案 »

  1.   

    _asm
    {
     code here
    }
      

  2.   

    use ASM and CPP together:
    1. create an ASM file
    ;;;;;;;;;;;;; asmsrc.asm:
    .386
    .model flat, stdcall
    option casemap :none
    .codemyasmproc proc dw1:DWORD,dw2:DWORD
    mov eax,dw1
    add eax,dw2
    ret
    myasmproc endp
    end
    ;;;;;;;;;;;;end of asmsrc.asm2. create a VC project name: useasm, type console application, A "Hello World" application3. move the asm file to your project directory, then in VC project menu->Add to Project...->Files...
    Files of type change to "all files", then you can select the asmsrc.asm, and click OK4.in workspace window, FileView tab, select asmsrc.asm, right click to select "settings..." menu, custom build tab, put the following in commands edit box :
    d:\masm32\bin\ml.exe /nologo /coff /Zf /c /Sa $(InputName).asm
    put the following in Outputs edit box:
    $(InputName).obj5.edit your useasm.cpp as the following:
    //////////////////////useasm.cpp///////////////////////////////
    #include "stdafx.h"
    #include <windows.h>
    extern "C" int __stdcall  myasmproc(DWORD d1,DWORD d2);
    int main(int argc, char* argv[])
    {
    printf("test of using cpp and asm together, if it works, it is done by masterz,otherwise I don't know who write this^_^\n");
    int ret=myasmproc(22,33);
    printf("ASM result:%d\n",ret);
    return 0;
    }//////////////////////end of useasm.cpp///////////////////////////////6. build the project and run it, it works.
      

  3.   

    多谢。
    好象用_asm{}要方便一点点,但在{}中的汇编代码的写法和MASM中的写法是一致的吗,有没有需要特别注意的地方?
      

  4.   

    一样的,其实在vc中加汇编,和c中加入汇编是一样的
      

  5.   

    看MSDN 2003:
    ms-help://MS.MSDNQTR.2002JAN.1033/vclang/html/_core_Assembler_.28.Inline.29_.Topics.htm
    比较全的
      

  6.   

    本人在VC下写的一个画直线的函数,还使用了局部变量,懒得裁了,参考一下吧。
    BOOL CGraphDC::_Line(int x1,int y1,int x2,int y2,BOOL bJoint)
    {
    // if (m_pPicBits==NULL) return FALSE;
    LPBYTE pPic=(LPBYTE)m_pPicBits,pBmp=(LPBYTE)m_pBmpBits,pBuf=(LPBYTE)m_pBufBits;
    int penWidth=int(m_pen.Width);
    WORD penStyle=bJoint?m_penStyle:m_pen.Style;
    BYTE coloridx=m_byPenColor,mask=m_byBackgroud;
        UINT nBytesPerLine=m_nBytesPerLine;
    int tx,ty,inc1,inc2;
    int xmin=0,ymin=0,xmax=m_pView->right-1,ymax=m_pView->bottom-1;
    int nOffset,nMaxOffset; BYTE bDraw=0; __asm
    { PUSHAD
    //裁剪开始 (编码顺序:上下左右)
    MOV BX,0000h
    MOV EAX,x1
    CMP EAX,xmin
    JNL code_x1
    OR BH,02h
    JMP code_y1
    code_x1:
    CMP EAX,xmax
    JNG code_y1
    OR BH,01h
    code_y1:
    MOV EAX,y1
    CMP EAX,ymin
    JNL code_y11
    OR BH,08h
    JMP code_2
    code_y11:
    CMP EAX,ymax
    JNG code_2
    OR BH,04h
    code_2:
    MOV EAX,x2
    CMP EAX,xmin
    JNL code_x2
    OR BL,02h
    JMP code_y2
    code_x2:
    CMP EAX,xmax
    JNG code_y2
    OR BL,01h
    code_y2:
    MOV EAX,y2
    CMP EAX,ymin
    JNL code_y22
    OR BL,08h
    JMP cut
    code_y22:
    CMP EAX,ymax
    JNG cut
    OR BL,04h
    cut:
    TEST BX,0ffffh
    JZ draw
    TEST BH,BL
    JZ cut_sel
    JMP over
    cut_sel:
    MOV CL,BH
    TEST CL,0ffh
    JNZ bh_p
    MOV CL,BL
    bh_p:
    MOV EAX,x2
    SUB EAX,x1
    MOV tx,EAX
    MOV EAX,y2
    SUB EAX,y1
    MOV ty,EAX TEST CL,02h
    JZ right
    MOV EAX,xmin
    SUB EAX,x1
    IMUL ty
    IDIV tx
    ADD EAX,y1
    MOV EDX,xmin
    XCHG EAX,EDX
    JMP decd
    right:
    TEST CL,01h
    JZ top
    MOV EAX,xmax
    SUB EAX,x1
    IMUL ty
    IDIV tx
    ADD EAX,y1
    MOV EDX,xmax
    XCHG EAX,EDX
    JMP decd
    top:
    TEST CL,08h
    JZ bottom
    MOV EAX,ymin
    SUB EAX,y1
    IMUL tx
    IDIV ty
    ADD EAX,x1
    MOV EDX,ymin
    JMP decd
    bottom:
    MOV EAX,ymax
    SUB EAX,y1
    IMUL tx
    IDIV ty
    ADD EAX,x1
    MOV EDX,ymax
    decd:
    CMP CL,BH
    JNZ cd_2
    MOV x1,EAX
    MOV y1,EDX
    XOR BH,BH
    CMP EAX,xmin
    JNL cd_x1
    OR BH,02h
    JMP cd_y1
    cd_x1:
    CMP EAX,xmax
    JNG cd_y1
    OR BH,01h
    cd_y1:
    CMP EDX,ymin
    JNL cd_y11
    OR BH,08h
    JMP code_2
    cd_y11:
    CMP EDX,ymax
    JNG cut
    OR BH,04h
    JMP cut
    cd_2:
    MOV x2,EAX
    MOV y2,EDX
    XOR BL,BL
    CMP EAX,xmin
    JNL cd_x2
    OR BL,02h
    JMP cd_y2
    cd_x2:
    CMP EAX,xmax
    JNG cd_y2
    OR BL,01h
    cd_y2:
    CMP EDX,ymin
    JNL cd_y22
    OR BL,08h
    JMP cut
    cd_y22:
    CMP EDX,ymax
    JNG cut
    OR BL,04h
    JMP cut
    //裁剪结束,开始扫描转换
    draw:
    MOV ESI,1
    MOV EDI,nBytesPerLine
    MOV EAX,ymax
    SUB EAX,y1
    MUL EDI
    ADD EAX,x1
    MOV nOffset,EAX
    MOV EAX,ymax
    MUL EDI
    ADD EAX,xmax
    MOV nMaxOffset,EAX
    MOV EAX,x2
    SUB EAX,x1
    JGE x2_ge_x1
    NEG ESI
    NEG EAX
    x2_ge_x1:
    MOV tx,EAX
    MOV EAX,y1
    SUB EAX,y2
    JGE y1_ge_y2
    NEG EDI
    NEG EAX
    y1_ge_y2:
    MOV ty,EAX
    CMP EAX,tx
    JA ty_a_tx
    //ty_be_tx:
    MOV ECX,tx
    CMP ECX,00h
    JNZ draw_x
    JMP over
    draw_x:
    MOV bDraw,1
    SHL EAX,1
    MOV inc1,EAX
    SUB EAX,ECX
    MOV EDX,EAX
    SUB EAX,ECX
    MOV inc2,EAX
    scan_x:
    ROL penStyle,1
    JNC next_x
    PUSH ECX
    PUSH ESI
    MOV ECX,penWidth
    MOV EAX,ECX
    SHR EAX,1
    MOV EBX,EDX
    MUL EDI
    MOV EDX,EBX
    MOV ESI,nOffset
    ADD ESI,EAX
    MOV AL,coloridx
    pixel_x:
    CMP ESI,0
    JL skip_x
    CMP ESI,nMaxOffset
    JG skip_x
    MOV EBX,pPic
    MOV AH,BYTE PTR [EBX][ESI]
    XOR AH,AL
    MOV BYTE PTR [EBX][ESI],AH
    CMP AH,mask
    JNZ pic_x
    MOV EBX,pBmp
    MOV AH,BYTE PTR [EBX][ESI]
    pic_x:
    MOV EBX,pBuf
    MOV BYTE PTR [EBX][ESI],AH
    skip_x:
    SUB ESI,EDI
    LOOP pixel_x
    POP ESI
    POP ECX
    next_x:
    CMP EDX,00h
    JS d_l_0_x
    ADD EDX,inc2
    ADD nOffset,EDI
    JMP inc_x
    d_l_0_x:
    ADD EDX,inc1
    inc_x:
    ADD nOffset,ESI
    // LOOP scan_x
    DEC ECX
    JNZ scan_x
    JMP ok
    ty_a_tx:
    CMP EAX,0
    JNZ draw_y
    JMP over
    draw_y:
    MOV bDraw,1
    MOV ECX,EAX
    MOV EAX,tx
    SHL EAX,1
    MOV inc1,EAX
    SUB EAX,ECX
    MOV EDX,EAX
    SUB EAX,ECX
    MOV inc2,EAXscan_y:
    ROL penStyle,1
    JNC next_y
    PUSH ECX
    PUSH EDI
    MOV ECX,penWidth
    MOV EAX,ECX
    SHR EAX,1
    MOV EBX,EDX
    MUL ESI
    MOV EDX,EBX
    MOV EDI,nOffset
    ADD EDI,EAX
    MOV AL,coloridx
    pixel_y:
    CMP EDI,0
    JL skip_y
    CMP EDI,nMaxOffset
    JG skip_y
    MOV EBX,pPic
    MOV AH,BYTE PTR [EBX][EDI]
    XOR AH,AL
    MOV BYTE PTR [EBX][EDI],AH
    CMP AH,mask
    JNZ pic_y
    MOV EBX,pBmp
    MOV AH,BYTE PTR [EBX][EDI]
    pic_y:
    MOV EBX,pBuf
    MOV BYTE PTR [EBX][EDI],AH
    skip_y:
    SUB EDI,ESI
    LOOP pixel_y
    POP EDI
    POP ECX
    next_y:
    CMP EDX,00h
    JS d_l_0_y
    ADD EDX,inc2
    ADD nOffset,ESI
    JMP inc_y
    d_l_0_y:
    ADD EDX,inc1
    inc_y:
    ADD nOffset,EDI
    // LOOP scan_y
    DEC ECX
    JNZ scan_yok:
    MOV ECX,penWidth
    SHR ECX,1
    MOV EAX,x1
    CMP EAX,x2
    JNG ok_1
    XCHG EAX,x2
    MOV x1,EAX
    ok_1:
    MOV EAX,y1
    CMP EAX,y2
    JNG ok_2
    XCHG EAX,y2
    MOV y1,EAX
    ok_2:
    CMP ECX,0
    JZ over
    MOV EBX,xmax
    MOV EDX,ymax
    SUB x1,ECX
    JNS ok_3
    MOV DWORD PTR x1,0
    ok_3:
    ADD x2,ECX
    CMP EBX,x2
    JNS ok_4
    MOV x2,EBX
    ok_4:
    SUB y1,ECX
    JNS ok_5
    MOV DWORD PTR y1,0
    ok_5:
    ADD y2,ECX
    CMP EDX,y2
    JNS over
    MOV y2,EDX
    over:
    POPAD
    }
    if (!bDraw) return TRUE; //整条线段都没有画
    m_penStyle=penStyle;
    if (m_pPaint->IsRectEmpty()) m_pPaint->SetRect(x1,y1,x2+1,y2+1);
    else
    {
    if (x1<m_pPaint->left) m_pPaint->left=x1;
    if (x2>=m_pPaint->right) m_pPaint->right=x2+1;
    if (y1<m_pPaint->top) m_pPaint->top=y1;
    if (y2>=m_pPaint->bottom) m_pPaint->bottom=y2+1;
    }
    return TRUE;
    }