Thread: Linking C++ and Assembly

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    32

    Linking C++ and Assembly

    HELP!

    Hi, I'm trying to link an assembly procedure to my C++ project, I'm using Visual C++ 2005.

    I'm getting an error:
    DecodeB64.obj : error LNK2019: unresolved external symbol _Base64_Decode referenced in function _main
    I added both the .asm and the .cpp files to my project.
    In my cpp file:

    Code:
    #define	DWORD	unsigned int
    
    extern "C" DWORD Base64_Decode(char *, DWORD, char *);
    and in main:

    Code:
    dwDataLen = Base64_Decode(cpInputBuffer, dwFileSize, cpOutputBuffer);
    my .asm file:

    Code:
    public _Base64_Decode
    .386
    .MODEL    flat, stdcall
    
    .CODE
    
    _Base64_Decode    proc
    
    	local  InputLen : dword
    	local  InputBuf : dword
    	local  OutputBuf : dword
    
    	pop    InputBuf
    	pop    InputLen
    	pop    OutputBuf
    
    	pushad
    	
    	mov    ecx,InputLen
    	mov    esi,InputBuf
    
    	ASCII_TO_B64_LOOP:
    
    		mov    ah,[esi]
    		cmp    ah,61h    ;compare to 'a'
    		jl     CMP_CAPS
    		sub    ah,47h
    		jmp    NEXT
    	    
    		CMP_CAPS:
    		cmp    ah,41h    ;compare to 'A'
    		jl     CMP_PAD
    		sub    ah,41h
    		jmp    NEXT
    	    
    		CMP_PAD:
    		cmp    ah,3D     ;compare to '='
    		jne    CMP_NUMERAL
    		mov    ah,0h
    		jmp    NEXT
    	    
    		CMP_NUMERAL:
    		cmp    ah,30h    ;compare to '0'
    		jl     SYMBOL
    		add    ah,4h
    		jmp    NEXT
    	    
    		SYMBOL:
    		cmp    ah,2Fh    ;compare to '/'
    		jl     PLUS
    		mov    ah,3Fh
    		jmp    NEXT
    	    
    		PLUS:            ;it's '+'     
    		mov    ah,3Eh
    		jmp    NEXT
    	    
    		NEXT:
    		inc    esi
    	                  
    	loopw  ASCII_TO_B64_LOOP
    
    	mov    edi,OutputBuf
    	mov    esi,InputBuf
    	mov    ecx,InputLen
    	xor    edx,edx
    	mov    eax,4h
    	div    ecx
    	mov    ecx,eax
    
    	DECODING_LOOP:
    
    		mov    ah,[esi]
    		mov    al,[esi+1]
    		shl    al,2
    		mov    bl,ah
    		mov    dh,al
    		shld   bx,dx,2
    		mov    ah,bl
    		mov    [edi],ah
    		inc    edi
    		inc    esi
    	    
    		mov    ah,[esi]
    		mov    al,[esi+1]
    		shl    al,2
    		mov    bl,ah
    		mov    dh,al
    		shld   bx,dx,4
    		mov    ah,bl
    		mov    [edi],ah
    		inc    edi
    		inc    esi
    
    		mov    ah,[esi]
    		mov    al,[esi+1]
    		shl    al,2
    		mov    bl,ah
    		mov    dh,al
    		shld   bx,dx,6
    		mov    ah,bl
    		mov    [edi],ah
    		inc    edi
    		inc    esi
    
    		inc    esi
    
    	loopd  DECODING_LOOP
    
    	popad
    	
    	mov    dx,0h
    	mov    ax,sizeof OutputBuf
    
    	ret
    
    _Base64_Decode  endp
    
    end

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Hmm. Are you sure the assembler isn't prepending the underscore automatically? I'm not sure, but if you declare a function as "stdcall" it might do that for you. You might try removing the leading underscore from the assembly source file and see if that will link.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    32

    Yes

    I've tried that, should I remove the underscore from all three places where it says _Bit64_Decode? Or just from the first place?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Warlax View Post
    I've tried that, should I remove the underscore from all three places where it says _Bit64_Decode? Or just from the first place?
    I'd try removing it everywhere from the .asm file and see if that helps. Failing that, try dumping the contents of the .asm object file and see what symbols are getting put in there.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    32
    Quote Originally Posted by brewbuck View Post
    I'd try removing it everywhere from the .asm file and see if that helps. Failing that, try dumping the contents of the .asm object file and see what symbols are getting put in there.
    Well, I tried removing the underscore in the public statement but not in the procedure name, it failed, I tried the opposite, it failed, it tried without an underscore on all three, it failed and also with an underscore on everything it failed...

    What do you mean by dumping?

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Use a program called objdump to list the exports of a compiled assembly object file. objdump /EXPORTS blah.o

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    32

    OK

    I found how to use objdump - and it seems the compiler IS adding '_' automatically, but now that I know that, how do I solve the external linking problem?????

Popular pages Recent additions subscribe to a feed