|
2009-11-20
, 11:16
|
Posts: 3,841 |
Thanked: 1,079 times |
Joined on Nov 2006
|
#32
|
Can someone clarify the difference between source code and binary code?
#include <stdio.h> int main (void) { printf ("Hello, World\n"); return (0); }
.file "hello.c" .section .rodata .LC0: .string "Hello, World" .text .globl main .type main, @function main: .LFB2: pushq %rbp .LCFI0: movq %rsp, %rbp .LCFI1: movl $.LC0, %edi call puts movl $0, %eax leave ret
.cpu arm10tdmi .fpu softvfp .file "hello.c" .section .rodata .align 2 .LC0: .ascii "Hello, World\n\000" .text .align 2 .global main .type main, %function main: @ args = 0, pretend = 0, frame = 0 @ frame_needed = 1, uses_anonymous_args = 0 mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 ldr r0, .L2 bl printf mov r0, #0 ldmfd sp, {fp, sp, pc}
000520: 48 89 6C 24 D8 4C 89 64 24 E0 48 8D 2D 93 01 20 H.l$.L.d$.H.-.. 000530: 00 4C 8D 25 8C 01 20 00 4C 89 6C 24 E8 4C 89 74 .L.%.. .L.l$.L.t 000540: 24 F0 4C 89 7C 24 F8 48 89 5C 24 D0 48 83 EC 38 $.L.|$.H.\$.H..8 000550: 4C 29 E5 41 89 FD 49 89 F6 48 C1 FD 03 49 89 D7 L).A..I..H...I.. 000560: E8 53 FE FF FF 48 85 ED 74 1C 31 DB 0F 1F 40 00 .S...H..t.1...@. 000570: 4C 89 FA 4C 89 F6 44 89 EF 41 FF 14 DC 48 83 C3 L..L..D..A...H.. 000580: 01 48 39 EB 72 EA 48 8B 5C 24 08 48 8B 6C 24 10 .H9.r.H.\$.H.l$. 000590: 4C 8B 64 24 18 4C 8B 6C 24 20 4C 8B 74 24 28 4C L.d$.L.l$ L.t$(L 0005a0: 8B 7C 24 30 48 83 C4 38 C3 90 90 90 90 90 90 90 .|$0H..8........ 0005b0: 55 48 89 E5 53 48 83 EC 08 48 8B 05 08 01 20 00 UH..SH...H.... . 0005c0: 48 83 F8 FF 74 19 BB C8 06 60 00 0F 1F 44 00 00 H...t....`...D.. 0005d0: 48 83 EB 08 FF D0 48 8B 03 48 83 F8 FF 75 F1 48 H.....H..H...u.H 0005e0: 83 C4 08 5B C9 C3 90 90 48 83 EC 08 E8 5F FE FF ...[....H...._.. 0005f0: FF 48 83 C4 08 C3 00 00 01 00 02 00 48 65 6C 6C .H..........Hell 000600: 6F 2C 20 57 6F 72 6C 64 00 00 00 00 01 1B 03 3B o, World.......;
|
2010-01-06
, 23:38
|
Posts: 1 |
Thanked: 0 times |
Joined on Jan 2010
|
#33
|
Source code:
Binary code: Use a C compiler and tell it to translate the above source code into binary code, aka machine code.Code:#include <stdio.h> int main (void) { printf ("Hello, World\n"); return (0); }
Internally it will first create an assembly representation, that is, machine code written in human readable form (this is not even all of it, just a snippet):
The 'movl', 'call', 'movq' etc. are assembly code for the Intel x86 CPU (processor). (Lines starting with a dot, e.g. .type, are so-called assembler directives and not CPU instructions). If I compile the same source code (the first piece of code, with the 'printf ("Hello, World\n")' for ARM (the CPU in our Nokia gadgets) it would look a bit different:Code:.file "hello.c" .section .rodata .LC0: .string "Hello, World" .text .globl main .type main, @function main: .LFB2: pushq %rbp .LCFI0: movq %rsp, %rbp .LCFI1: movl $.LC0, %edi call puts movl $0, %eax leave ret
The ARM CPU instructions are different from the x86 CPU instructions. ARM assembly language uses 'mov', ldmfd', 'bl', etc. which are specific for ARM. Some may do the same thing as an x86 instructions with a different name, but often there is no direct equivalent at all. And in any case the set of registers (r0 is one, above), i.e. the internal storage boxes, are different for different CPU architectures.Code:.cpu arm10tdmi .fpu softvfp .file "hello.c" .section .rodata .align 2 .LC0: .ascii "Hello, World\n\000" .text .align 2 .global main .type main, %function main: @ args = 0, pretend = 0, frame = 0 @ frame_needed = 1, uses_anonymous_args = 0 mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 ldr r0, .L2 bl printf mov r0, #0 ldmfd sp, {fp, sp, pc}
The assembly code is then translated to machine code, which are just numbers. A snippet of the binary (executable) file for x86 looks something like this when inspected with a hex editor (which shows each numeric value of each byte as a hexadecimal number):
When I 'execute' the file with the above unreadable stuff it'll write 'Hello, World' and a linefeed in my terminal window.Code:000520: 48 89 6C 24 D8 4C 89 64 24 E0 48 8D 2D 93 01 20 H.l$.L.d$.H.-.. 000530: 00 4C 8D 25 8C 01 20 00 4C 89 6C 24 E8 4C 89 74 .L.%.. .L.l$.L.t 000540: 24 F0 4C 89 7C 24 F8 48 89 5C 24 D0 48 83 EC 38 $.L.|$.H.\$.H..8 000550: 4C 29 E5 41 89 FD 49 89 F6 48 C1 FD 03 49 89 D7 L).A..I..H...I.. 000560: E8 53 FE FF FF 48 85 ED 74 1C 31 DB 0F 1F 40 00 .S...H..t.1...@. 000570: 4C 89 FA 4C 89 F6 44 89 EF 41 FF 14 DC 48 83 C3 L..L..D..A...H.. 000580: 01 48 39 EB 72 EA 48 8B 5C 24 08 48 8B 6C 24 10 .H9.r.H.\$.H.l$. 000590: 4C 8B 64 24 18 4C 8B 6C 24 20 4C 8B 74 24 28 4C L.d$.L.l$ L.t$(L 0005a0: 8B 7C 24 30 48 83 C4 38 C3 90 90 90 90 90 90 90 .|$0H..8........ 0005b0: 55 48 89 E5 53 48 83 EC 08 48 8B 05 08 01 20 00 UH..SH...H.... . 0005c0: 48 83 F8 FF 74 19 BB C8 06 60 00 0F 1F 44 00 00 H...t....`...D.. 0005d0: 48 83 EB 08 FF D0 48 8B 03 48 83 F8 FF 75 F1 48 H.....H..H...u.H 0005e0: 83 C4 08 5B C9 C3 90 90 48 83 EC 08 E8 5F FE FF ...[....H...._.. 0005f0: FF 48 83 C4 08 C3 00 00 01 00 02 00 48 65 6C 6C .H..........Hell 000600: 6F 2C 20 57 6F 72 6C 64 00 00 00 00 01 1B 03 3B o, World.......;
As you can see from the above, the source code [printf ("Hello, World\n");] can easily be inspected, modified, copied to, and compiled to run on another computer architecture (from a mainframe to an Intel CPU to an ARM CPU or the CPU that runs in your Panasonic TV, or anything).
The assembly code will only be usable for the target architecture only, but you could still go in and modify it somewhat (print something different, or print it twice, for example).
The pure machine code (the binary code) will only be usable on the target architecture and it would be very difficult to modify it to do something else. Not impossible, because almost nothing is, but still.
Airplay on the other hand provides "binary compatibility", which means that there is no need to even recompile the source code to run applications any platforms that Airplay supports.
Another example of a system that supports binary compatibility is java. In this case the claim is that .class files are not platforms specific and should run on any platform with a JVM.