Find the answer to your Linux question:
Results 1 to 4 of 4
Could I get some separation on the two of what makes them different? What part of a compiler changes the code from assembly to machine? Thanks in advance!...
  1. #1
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    Assemblers and Compilers Definition of

    Could I get some separation on the two of what makes them different? What part of a compiler changes the code from assembly to machine?

    Thanks in advance!

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Assembly and machine code are pretty closely interrelated. I'm not sure of the exact definition, but I tend to think of them this way:

    Assembly language is a very simple, human-readable programming language that maps 1-to-1 to machine language. The following is an example of the MIPS assembly language:
    Code:
    li $s0, 5 ; Set the $s0 register = 5
    addi $s1, $s0, 10 ; Add 10 to the $s0 register and store the result in $s1
    move $a0, $s1 ; Copy the value in $s1 into the $a0 register
    jal foo ; Call the foo function
    This is a relatively simple program, and it may not make much sense if you don't know assembly language, but it is quite understandable to someone who understands assembly language, particularly someone who knows MIPS.

    However, if I run this code through an assembler, it might produce the following file:
    Code:
    01111100001000000000000000000000
    01110110000000010000000000000101
    00100010001100000000000000001010
    00100000100100010000000000000000
    00001100010101011100000101000000
    These two files say the same thing, and it is well documented how to turn the command
    Code:
    $addi $s1, $s0, 10
    into
    Code:
    00100010001100000000000000001010
    There is also only one "machine code" that the assembly can be turned into, and the assembly can only represent one command. This machine code is easier for the computer to understand, but it is obviously much harder for a human.

    So that's what I consider to be the difference between "assembly language" and "machine language".

    As for the compiler, it depends. If the original code was written in assembly language, you can use an assembler (such as spim for MIPS) to simply translate the code and execute it. It's very easy.

    If the original code was written in a more complicated language such as C, the compiler has a number of steps. Once the compiler understands the C code, it will eventually translate the code into something called an "intermediate representation". The intermediate representation is essentially an abstract assembly language. This intermediate representation, instead of using specific registers, will likely refer simply to fake registers, where every variable uses its own register.

    Later, the intermediate representation will be converted to the target assembly language, still with the fake registers. Finally, the compiler will eventually do "register allocation", where it figures out conflicts in the various stages of the program and assigns real registers to each of the fake ones.

    This is a very brief overview of how compilers work, and whole college courses are dedicated to the subject of compilers. If you're seriously interested in compilers, I used this textbook to learn about them:
    Amazon.com: Modern Compiler Implementation in ML (9780521607643): Andrew W. Appel: Books

    This book is also very popular:
    Amazon.com: Compilers: Principles, Techniques, and Tools (2nd Edition) (9780321486813): Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman: Books
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    Thanks for the great reply!

    I seem to be learning more about Linux by digging into this type of subject. I'm reading about linkers and loaders right now. Thanks for the great response and the tip on the book!

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Wikipedia is very useful for factual information of this kind, for example:

    Assembly language - Wikipedia, the free encyclopedia

    High-level programming language - Wikipedia, the free encyclopedia

    and links therein ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...