Compilers

This is a discussion on Compilers within the Tech Board forums, part of the Community Boards category; Well.... ok, here is the deal. The machine code is portable across operating systems since it is just the way ...

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well.... ok, here is the deal. The machine code is portable across operating systems since it is just the way the CPU operates commands. However, the machine code that runs on a MIPS processor is vastly different than that which runs on a 8088. However, executable files are different across different files. They embed different information within headers and the body of an executable. The sections that contain machine code are going to be the only similarity. So again, no. Assembler is just about the least portable langauge you can use.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lruc View Post
    Is machine code more or less portable than asm? I would imagine asm is more portable among operating systems but machine code is more portable among hardware.
    Assembler and machine code have a 1:1 relationship. One instruction [as long as we stay with one processor architecture, e.g. X86] will produce one set of instruction bytes. There are some instructions that have synonyms (that is, there are more than one way to form a valid instruction that does this thing), but the output from the assembler will have the exact same meaning for either of those synonyms - it may be a byte or two longer, but other than that, it's the same result.

    Sure, NASM, MASM and CPP + GAS have slightly differnet syntax for Macros and other pseudo-operations [pseudo-ops are things that aren't actual machine-code instructions, but rather instructions to the assembler as to what you want done, where you want it [e.g. .data, .code are instrucitons to the assembler that "what comes next should go in the data section or the code section].

    Also, if you use gas on windows, the code will work on Windows, and as long as you don't call the OS, you can assemble the same thing in Linux and get the same result.

    There is absolutely no meaning in remembering that 8B is MOV, 75 is JNZ, 90 is NOP, EA is a JMP, CC is INT3, etc. [But if you want to check those out, I think I got most of them about right]. That is of course x86. On 68000 the instructions have somewhat differnet names, and completely different opcodes.

    The following code should assemble, and make an executable file that works, for both Linux and Windows:
    Code:
    	.text
    
    	.globl	_main
    _main:	
    	mov	$hello, %eax
    	push	%eax
    
    	call	_puts
    	add	$4, %esp
    	
    	xor	%eax, %eax
    
    	ret
    
    	.data
    
    hello:	.string	"Hello, World!\n"
    
    	.end
    Code:
    as -o asm.o asm.s
    gcc -o asm.exe asm.o
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think the more important issue he is somewhat not fully comprehending is that even though the opcodes are going to remain the same outside of operating system constraint, the underlying hardware is one thing that you cannot necessarily count on. Unless you are programming for something that doesn't allow any sort of variation on the underlying hardware. Such as a gaming console or DVR or something like that.

  4. #19
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    So it's like your portable among hardware, or your portable among operating systems, but without a portable library you can't be portable among both. Am I getting this strait?

  5. #20
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Also, matsp. Your code will not assemble correctly. I keep on getting an error message stating entry point _start can't be found and that _puts is an undefined reference. Any help?

  6. #21
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Change _main: to _start: and link to the c standard library....

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That sounds dangerous. gcc uses the entry symbol _start to call initialization, and initializing glibc is part of that, I think. Shouldn't you keep using main, but link in GCC's startup files?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c compilers
    By kumarangopi in forum C Programming
    Replies: 2
    Last Post: 11-22-2006, 11:12 AM
  2. C++ Builder Comparison
    By ryanlcs in forum Tech Board
    Replies: 14
    Last Post: 08-20-2006, 09:56 AM
  3. Is It Possible To Install Multiple Compilers?
    By mishna_toreh in forum C Programming
    Replies: 3
    Last Post: 05-13-2005, 07:32 AM
  4. Compilers for Windows
    By LegendsEnd in forum Windows Programming
    Replies: 2
    Last Post: 03-26-2004, 07:03 AM
  5. Compilers, Compilers, Compilers
    By Stan100 in forum C++ Programming
    Replies: 11
    Last Post: 11-08-2002, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21