I'm on Windows 7, and I installed MinGW just the other day. Unless I'm mistaken, the following syntax would be the same:


Code:
C:\>gcc hello.c -o hello.exe
C:\>hello.exe



Code:
C:\>gcc -S hello.c
C:\>as hello.s -o hello.exe
C:\>hello.exe


given hello.c:

Code:
#include <stdio.h>
main() {
printf("Hello");
}


Don't get my wrong, I'm not ruling out the idea of my being mistaken that those two CMD commands are equivalent. Anyways, the first one just compiles the C code using GCC, and the second one eats C, poops ASM, and then tries to assemble the ASM. However, the second one doesn't work - it runs but doesn't display "Hi" like the C one does.

EDIT:
OK, I run that second command again, and trying to run the executable, a pop-up comes up and says,

Code:
C:\Windows\system32\cmd.exe - hello.exe
the NTVDM CPU has encountered an illegal instruction
CS:0e25 IP:001d OP:ff ff ff ff ff Choose 'Close' to terminate the application.


And here's my hello.s file:

Code:
   .file   "hello.c"
   .def   ___main;   .scl   2;   .type   32;   .endef
   .section .rdata,"dr"
LC0:
   .ascii "Hi\0"
   .text
   .globl   _main
   .def   _main;   .scl   2;   .type   32;   .endef
_main:
LFB6:
   .cfi_startproc
   pushl   %ebp
   .cfi_def_cfa_offset 8
   .cfi_offset 5, -8
   movl   %esp, %ebp
   .cfi_def_cfa_register 5
   andl   $-16, %esp
   subl   $16, %esp
   call   ___main
   movl   $LC0, (%esp)
   call   _printf
   leave
   .cfi_restore 5
   .cfi_def_cfa 4, 4
   ret
   .cfi_endproc
LFE6:
   .def   _printf;   .scl   2;   .type   32;   .endef
Why are you passing hello.c back into as? I assume it'll be whatever gcc spit out, which probably isn't hello.c.
My bad, that's not what I did. Fixed.
My understanding is that as doesn't spit out executables, it spits out object files. From the man page:
Quote:
as is primarily intended to assemble the output of the GNU C compiler "gcc" for use by the linker "ld".

So you need to use ld on it, or something else to link it. Really, though, that's a lot of work, because you have to link in all the libraries it depends on. Googling around would've given you a few hits, including this:
http://stackoverflow.com/questions/5307462/how-to-make-executable-from-assembly-languagein-emacs
I did Google around, but I guess not with the right keywords. Anyways, thanks, I'll try that out.
The proper internal process is parser -> code generation -> assembling -> linking. The parser and code generation turn the .c file into a .s file, complete with necessary optimizations. The assembler assembles the assembly into an object file, and the linker turns that object into a properly-formatted executable. True story.
gcc -v is your friend. It emits details on what gcc (which is simply the driver that invokes the compiler, linker, etc) is doing:

Code:
[tari@Kirishima]$ gcc -v -o hello hello.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/src/gcc-4.7.1/configure --prefix=/usr --libdir=/usr/lib                 --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-b                ugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,ob                jc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__                cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdc                xx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-                id --with-ppl --enable-cloog-backend=isl --enable-lto --enable-gold --enable-ld=                default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --                disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-posts                tage1-with-cxx --enable-checking=release
Thread model: posix
gcc version 4.7.1 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/cc1 -quiet -v hello.c -quiet -dumpb                ase hello.c -mtune=generic -march=x86-64 -auxbase hello -version -o /tmp/ccIftlA                E.s
GNU C (GCC) version 4.7.1 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 4.7.1, GMP version 5.0.5, MPFR version 3.1.0-p                10, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../.                ./../../x86_64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/include-fixed
 /usr/include
End of search list.
GNU C (GCC) version 4.7.1 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 4.7.1, GMP version 5.0.5, MPFR version 3.1.0-p                10, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f76242892378161d678bf98157bd23ba
COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccuQjRHL.o /tmp/ccIftlAE.s
GNU assembler version 2.22.0 (x86_64-unknown-linux-gnu) using BFD version (GNU B                inutils) 2.22.0.20120323
COMPILER_PATH=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/:/usr/lib/gcc/x86_64-u                nknown-linux-gnu/4.7.1/:/usr/lib/gcc/x86_64-unknown-linux-gnu/:/usr/lib/gcc/x86_                64-unknown-linux-gnu/4.7.1/:/usr/lib/gcc/x86_64-unknown-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/:/usr/lib/gcc/x86_64-un                known-linux-gnu/4.7.1/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gc                c/x86_64-unknown-linux-gnu/4.7.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/collect2 --build-id --eh-frame-hdr                 --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib/ld-linux-x86-64.so.2 -o hell                o /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../lib/crt1.o /usr/lib/gc                c/x86_64-unknown-linux-gnu/4.7.1/../../../../lib/crti.o /usr/lib/gcc/x86_64-unkn                own-linux-gnu/4.7.1/crtbegin.o -L/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1 -L/                usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../lib -L/lib/../lib -L/usr/                lib/../lib -L/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../.. /tmp/ccuQjRHL.                o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as                -needed /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/crtend.o /usr/lib/gcc/x86_64                -unknown-linux-gnu/4.7.1/../../../../lib/crtn.o
That's a lot of output to wade through, but we can see that it invokes (in order): cc1 (the C compiler), as (the assembler), collect2 (the actual linker, which ld is a wrapper for).
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement