kernelthread.com

lang5

Introduction

Here is a piece of code that is valid (compile-able, and consequently executable) in five different languages, and produces the same output - the ubiquitous and unequivocal message: "Hello, World!". I must say that in my humble opinion, I am not overdoing this - "Hello" has truly been instrumental in shaping the face of Computing. Imagine how many programming texts include a "hello-producing" piece of code in their initial chapters!

And of course, GNU has even got a package on it! The number of would-be (wannabe, have-been, never-will-be, never-were ...) programmers that would have written code to produce this remarkable string is enormous.

The Languages

Well then, the code below can be made to produce " Hello, World!" in five different languages as described below.

C

Good old C. Plain C. One C-sin committed by this code is declaring a variable implicitly as an integer. GCC should do fine as a compiler The code was last tested with gcc-2.8.1.

FORTRAN

Good (?) Old (sure) FORTRAN, but please use the GNU compiler, for it allows the use of #define statements within FORTRAN code. The code was last tested with egcs-2.90.27 980315.

Moreover, this is hardly rocket-science, so it is interesting to note that this code does work with FORTRAN-77 and FORTRAN-90 (as tested using Intel's FORTRAN compiler on the x86).

Perl

The All-round workhorse. The duct tape of the Internet. The infinite-layered onion. (Nauseating enough). Perl is good enough to seek for its code embedded in a larger file if the '-x' switch is passed to the interpreter. The code was last tested with Perl 5.005_02.

/bin/sh

The code will act as a shell script producing " Hello, World!" on the standard output. Note the previous statement carefully! The code was last tested under bash 1.14.7.

x86 Assembly Language

This is funny. You have to use TASM ((formerly) Borland's Turbo Assembler v2.51) because of an assembler macro that it provides (other assemblers such as Nasm would work, but then I have to rework the code). To link the object file and to get a .COM file, you have to use the TLINK utility (comes with TASM and also with (formerly) Borland's C/C++ compilers.

Build Instructions

C

% cp lang5.x lang5.c % gcc -o lang5-c lang5.c lang5.c:1: warning: data definition has no type or storage class % ./lang5-c Hello, World! %

FORTRAN

% cp lang5.x lang5.f % g77 -o lang5-f lang5.f % ./lang5-f Hello, World! %

Perl

% perl -x lang5.x Hello, World! %

/bin/sh

% /bin/sh lang5.x Hello, World %

x86 Assembly Language

Since this particular implementation of the "Assembly" facet of the code needs TASM, you would need DOS (it could be MS-DOS or a variant/clone), either running natively, or on an emulator. I used DOSEMU, the DOS emulator.

C:> copy lang5.x lang5.asm C:> tasm lang5.asm C:> tlink /t lang5.obj C:> lang5.com Hello, World! C:>

Source

C=0;/* 2>/dev/null;echo " Hello, World!";exit; COMMENT Z *0:;*/main(){/* print*,'Hello, World!' end #define # C #! perl C: print " Hello, World!\n"; C: __END__; Z CD segment para 'code' C1:assume CS:CD,DS:CD,SS:CD,ES:CD C2:org 100H C3:jmp C5 C4 DB ' Hello, World!','$' C5 proc near C6:mov DX,offset C4 C7:mov AH,9H C8:int 21H C9:mov AH,0H CA:int 21H C5 endp CD ends CC:end C3 CZ:; */puts(" Hello, World!");}

Download

lang5.x