GCC and NASM co-compiled

Source: Internet
Author: User

In order to learn the smooth progress, today try to review the C and assembly language Joint compilation. The code is simple:

/* main.c */extern void Print1 (), char strformat[] = "x =%d, y =%d, x + y =%d"; int main () {Print1 (); return 0;}

; Proc.nasextern printfextern strformatglobal print1[section. DATA]NX DD 14nY dd 23[section. Text]print1:mov eax, [NX]mov EB x, [Ny]mov ecx, Eaxadd eax, ebx; Note the parameter in the stack order: right-to-left push DWORD eax; NX + nypush DWORD ebx; Nypush DWORD Ecx; Nxpush DWORD Strformatcall printfadd ESP, byte 4 * 4ret

#MakeFile nasm-felf32 proc.nas gcc-m32-o main MAIN.C PROC.O

Still using GCC + NASM, can be compiled, but the link to execute the file how all pass, has been error "undefined function"!

Try various operating systems (WinXP 32-bit, Win7 64-bit), a variety of compiler version (MinGW 32-bit, 64-bit), a variety of compilation parameters, engaged in the majority of days, die and die do not let me!

In the fast despair, suddenly remembered the previously browsed C library functions, vaguely remember the function name preceded by an underscore "_"! Is that the reason? Try it ...

extern _printfextern _strformatglobal _print1[section. Data]nx DW 14nY DW 23[section. Text]_print1:mov eax, [NX]mov ebx, [ Ny]mov ecx, eaxadd eax, ebx; Note the parameter in the stack order: right-to-left push DWORD eax; NX + nypush DWORD ebx; Nypush DWORD Ecx; Nxpush DWORD _strformatcall _printfadd ESP, byte 4 * 4ret

Actually, the compilation passed!

Summing up, assembly language (NASM) and C language (GCC) joint compilation ( under the Windows system ), assembly code and C language generated a mixed relationship of variables and functions, preceded by an underscore "_"! The C language code does not add.


GCC and NASM co-compiled

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.