-3

I was compiling a program to find the maximum of numbers in an array.

Edited

I compiled it as follows:

gcc --save-temps max.c -o max

and then on doing

ls -l max*

the output was:

-rwxrwxr-x 1 tapan tapan  7296 Aug 16 01:45 max
-rw-rw-r-- 1 tapan tapan   233 Aug 16 01:45 max.c
-rw-rw-r-- 1 tapan tapan 17894 Aug 16 01:45 max.i
-rw-rw-r-- 1 tapan tapan  1308 Aug 16 01:45 max.o
-rw-rw-r-- 1 tapan tapan  1507 Aug 16 01:45 max.s

My question is: why is the size of .s assembly file larger than .o binary file?

I thought that .o file has binary strings so it will be larger.

Also the final file max has larger size as expected due to linked libraries. But the size of .o file makes me think that .o file doesn't have binary strings but something else. Please correct me if I am wrong somewhere.

tapananand
  • 4,392
  • 2
  • 19
  • 35
  • 6
    Let's see, one file contains code intended for the CPU to be able to execute efficiently. The other contains code intended to be readable to a human. Why would you expect the latter to be *smaller*? – jalf Aug 15 '13 at 20:30
  • because let's say if ADD = 01001010(opcode) then the opcode has has more characters. – tapananand Aug 15 '13 at 20:34
  • To retort, a variable called 'variable' is composed of 64 bits, but would be a 32 bit memory address in the binary file if compiled on a 32 bit machine (ignoring the overhead of the OP-code that assigns the variable to a register address). Take a compilers course, it's really interesting stuff and makes you appreciate what goes on while you get coffee after running gcc. – gravitas Aug 15 '13 at 20:38
  • 5
    @Tapan: "ADD" is 3 bytes in ASCII: 'A', 'D', and 'D'. `01001010` represents a binary number that can be stored in a single byte. – indiv Aug 15 '13 at 20:39

1 Answers1

3

If by "binary strings" you mean ASCII-encoded (i.e. each 0 or 1 is the ASCII characters 0x30 and 0x31) so you can open the file in a text editor and see 0001011011111101011101100..., then no. Just have a look at with cat or od (if you're on a unix) or a hex editor, it contains the binary data "directly". The opcode 01001010 takes up one byte in a binary file, not 8 ASCII characters which take 1 byte each.

  • So am I right in saying that final file "max" is even larger due to the linker linking all the libraries?? But aren't the libraries dynamically linked at run time?? – tapananand Aug 15 '13 at 20:45
  • @TapanAnand The object files are the *input* to the linker. They haven't been linked with anything yet, they've been output by the assembler (or, in better toolchains, by the compiler directly). –  Aug 15 '13 at 20:47
  • 1
    Dynamic libraries are stored somewhere else in your computer and are not concatenated to the binary file. Static libraries on the other hand are concatenated to your binary and will thus increase the file size. – gravitas Aug 15 '13 at 20:48