Computer Programming/Hello world

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] 4DOS batch

It should be noted that the 4DOS/4NT batch language is a superset of the MS-DOS batch language.

@echo Hello, world!

[edit] Ingres 4GL

message "Hello, world!" with style = popup;

[edit] ABAP - SAP AG

REPORT ZELLO.
WRITE 'Hello, world!'.

[edit] ABC

WRITE "Hello, world!"

[edit] ActionScript

trace("Hello, world!");

[edit] ActionScript 3

package
{
    import flash.display.Sprite;
    public class HelloWorld extends Sprite
    {
        public function HelloWorld()
        {
            trace("Hello, world!");
        }
    }
}

[edit] Ada

with Ada.Text_IO;
 
procedure Hello is
begin
   Ada.Texto_IO.Put_Line ("Hello, world!");
end Hello;

[edit] ALGOL 68

The popular upper-case stropping convention for bold words:

BEGIN
    print(("Hello, world!", newline))
END

[edit] OR

Using prime stropping suitable for punch cards:

'BEGIN'
    PRINT(("Hello, world!", NEWLINE))
'END'

[edit] OR

( print("Hello, world!") )

[edit] AmigaE

PROC main()
   WriteF('Hello, world!');
ENDPROC

[edit] ANSI C

#include <stdio.h>
 
int main(void) {
    printf("Hello, World!\n");
 
    return 0;
}

[edit] ANSI Common Lisp

(format t "Hello, World!~%")

[edit] APL

    \nabla  \mathrm {R} \leftarrow \mathrm {HW} \Delta\mathrm{PGM}  
\left [ 1 \right ] \mathrm {R}\leftarrow \mathrm {'HELLO} \; \mathrm {WORLD!'} 
    \nabla 
  • The Del on the first line begins function definition for the program named HWΔPGM. It is a niladic function (no parameters, as opposed to monadic or dyadic) and it will return an explicit result which allows other functions or APL primitives to use the returned value as input.
  • The line labeled 1 assigns the text vector 'Hello, world!!' to the variable R
  • The last line is another Del which ends the function definition.

When the function is executed by typing its name the APL interpreter assigns the text vector to the variable R, but since we have not used this value in another function, primitive, or assignment statement the interpreter returns it to the terminal, thus displaying the words on the next line below the function invocation.

The session would look like this

      HWΔPGM
Hello, world!!

While not a program, if you simply supplied the text vector to the interpreter but did not assign it to a variable it would return it to the terminal as output. Note that user input is automatically indented 6 spaces by the interpreter while results are displayed at the beginning of a new line.

      'Hello, world!'
Hello, world!!

[edit] AppleScript

return "Hello, world!"

[edit] ASP

<% Response.Write("Hello World!") %>

[edit] ASP.NET

// in the page behind using C#
protected void Page_Load(object sender, EventArgs e)
{
 Response.Write("Hello, world!");
}
// ASPX Page Template
 
<asp:Literal ID="Literal1" runat="server" Text="Hello World!"></asp:Literal>

or

<asp:Label ID="Label1" runat="server" Text="Hello World"></asp:Label>

or

Hello World!

[edit] Assembly language

[edit] Accumulator-only architecture: DEC PDP-8, PAL-III assembler

See the example section of the PDP-8 article.

[edit] First successful uP/OS combinations: Intel 8080/Zilog Z80, CP/M, RMAC assembler

bdos    equ    0005H    ; BDOS entry point
start:  mvi    c,9      ; BDOS function: output string
        lxi    d,msg$   ; address of msg
        call   bdos
        ret             ; return to CCP

msg$:   db    'Hello, world!$'
end     start

[edit] Popular home computer: ZX Spectrum, Zilog Z80, HiSoft GENS assembler

 10          ORG #8000    ; Start address of the routine
 20 START    LD A,2       ; set the output channel
 30          CALL #1601   ; to channel 2 (main part of TV display)
 40          LD HL,MSG    ; Set HL register pair to address of the message
 50 LOOP     LD A,(HL)    ; De-reference HL and store in A
 60          CP 0         ; Null terminator?
 70          RET Z        ; If so, return
 80          RST #10      ; Print the character in A
 90          INC HL       ; HL points at the next char to be printed
100          JR LOOP
110 MSG      DEFM "Hello, world!"
120          DEFB 13      ; carriage return
130          DEFB 0       ; null terminator

[edit] Accumulator + index register machine: MOS Technology 6502, CBM KERNEL, MOS assembler syntax

        LDX #$00         ;starting index
LOOP    LDA MSG,X        ;read message text
        BEQ LOOPEND      ;end of text
        JSR $FFD2        ;BSOUT kernel sub, write to current output device
        INX
        BNE LOOP         ;repeat
LOOPEND RTS              ;return from subroutine
;
MSG     .BYT 'Hello, world!',$00

[edit] Accumulator/Index microcoded machine: Data General Nova, RDOS

See the example section of the Nova article.

[edit] Expanded accumulator machine: Intel x86, DOS, TASM

MODEL   SMALL
IDEAL
STACK   100H

DATASEG
        MSG DB 'Hello, world!', 13, '$'

CODESEG
Start:
        MOV AX, @data
        MOV DS, AX
        MOV DX, OFFSET MSG
        MOV AH, 09H      ; DOS: output ASCII$ string
        INT 21H
        MOV AX, 4C00H
        INT 21H
END Start

[edit] ASSEMBLER x86 (DOS, MASM)

.MODEL Small
.STACK 100h
.DATA
   db msg 'Hello, world!$'
.CODE
start:
   mov ah, 09h
   lea dx, msg ; or mov dx, offset msg
   int 21h
   mov ax,4C00h
   int 21h
end start

[edit] ASSEMBLER x86 (DOS, FASM)

; FASM example of writing 16-bit DOS .COM program
; Compile: "FASM HELLO.ASM HELLO.COM" 
  org  $100
  use16    
  mov  ah,9
  mov  dx,xhello
  int  $21    ; DOS call: text output
  mov  ah,$4C
  int  $21    ; Return to DOS
xhello db 'Hello world !!!$'

[edit] Expanded accumulator machine: Intel x86, Microsoft Windows, FASM

Example of making 32-bit PE program as raw code and data:

 
 format PE GUI
 entry start
 
 section '.code' code readable executable
 
     start:
 
         push   0
         push   _caption
         push   _message
         push   0
         call   [MessageBox]
 
         push   0
         call   [ExitProcess]
 
 section '.data' data readable writeable
 
   _caption db 'Win32 assembly program',0
   _message db 'Hello, world!',0
 
 section '.idata' import data readable writeable
 
   dd 0,0,0,RVA kernel_name,RVA kernel_table
   dd 0,0,0,RVA user_name,RVA user_table
   dd 0,0,0,0,0
 
  kernel_table:
     ExitProcess dd RVA _ExitProcess
     dd 0
  user_table:
     MessageBox dd RVA _MessageBoxA
     dd 0
 
  kernel_name db 'KERNEL32.DLL',0
  user_name db 'USER32.DLL',0

  _ExitProcess dw 0
     db 'ExitProcess',0
  _MessageBoxA dw 0
     db 'MessageBoxA',0
 
 section '.reloc' fixups data readable discardable

[edit] Expanded accumulator machine: Intel x86, Linux, FASM

 format ELF executable
 entry _start
 
 _start:
      mov eax, 4
      mov ebx, 1
      mov ecx, msg
      mov edx, msg_len
      int 0x80
      mov eax, 1
      xor ebx, ebx
      int 0x80
 
      msg db 'Hello, world!', 0xA
      msg_len = $-msg

[edit] Expanded accumulator machine:Intel x86, Linux, GAS

.data
msg:
    .ascii     "Hello, world!\n"
    len = . - msg
.text
    .global _start
_start:
    movl $len,%edx
    movl $msg,%ecx
    movl $1,%ebx
    movl $4,%eax
    int $0x80
    movl $0,%ebx
    movl $1,%eax
    int $0x80

[edit] Expanded accumulator machine: Intel x86, Linux, NASM

    section .data
msg     db      'Hello, world!',0xA
len     equ     $-msg

    section .text
global  _start
_start:
        mov     edx,len
        mov     ecx,msg
        mov     ebx,1
        mov     eax,4
        int     0x80

        mov     ebx,0
        mov     eax,1
        int     0x80

[edit] Expanded accumulator machine: Intel x86, Linux, GLibC, NASM

extern printf ; Request symbol "printf".
global main   ; Declare symbol "main".

section .data
  str: DB "Hello World!", 0x0A, 0x00

section .text
main:
  PUSH str    ; Push string pointer onto stack.
  CALL printf ; Call printf.
  POP eax     ; Remove value from stack.
  MOV eax,0x0 ; \_Return value 0.
  RET         ; /

[edit] General-purpose fictional computer: MIX, MIXAL

TERM    EQU    19          console device no. (19 = typewriter)
        ORIG   1000        start address
START   OUT    MSG(TERM)   output data at address MSG
        HLT                halt execution
MSG     ALF    "HELLO"
        ALF    " WORL"
        ALF    "D    "
        END    START       end of program

[edit] General-purpose fictional computer: MMIX, MMIXAL]

string  BYTE   "Hello, world!",#a,0   string to be printed (#a is newline and 0 terminates the string)
  Main  GETA   $255,string            get the address of the string in register 255
        TRAP   0,Fputs,StdOut         put the string pointed to by register 255 to file StdOut
        TRAP   0,Halt,0               end process

[edit] General-purpose-register CISC: DEC PDP-11, RT-11, MACRO-11

        .MCALL  .REGDEF,.TTYOUT,.EXIT
        .REGDEF

HELLO:  MOV    #MSG,R1
        MOVB   (R1)+,R0
LOOP:  .TTYOUT
        MOVB   (R1)+,R0
        BNE    LOOP
       .EXIT

MSG:   .ASCIZ  /Hello, world!/
       .END    HELLO

[edit] CISC Amiga: Motorola 68000

        xref _LVOCloseLibrary
        xref _LVOOpenLibrary
        xref _LVOPutStr

        ; open DOS library

        movea.l  4,a6
        lea.l    dosname(pc),a1
        clr.l    d0
        jsr      _LVOOpenLibrary(a6)
        movea.l  d0,a6

        ; actual print string

        move.l   #hellostr,d1
        jsr      _LVOPutStr(a6)

        ; close DOS library

        movea.l  a6,a1
        movea.l  4,a6
        jsr      _LVOCloseLibrary(a6)
        rts

dosname     dc.b 'dos.library',0

hellostr    dc.b 'Hello, world!',10,0

[edit] CISC Atari: Motorola 68000

;print
     move.l   #Hello,-(A7)
     move.w   #9,-(A7)
     trap     #1
     addq.l   #6,A7

;wait for key
     move.w   #1,-(A7)
     trap     #1
     addq.l   #2,A7

;exit
     clr.w   -(A7)
     trap    #1
 
Hello
     dc.b    'Hello, world!',0

[edit] CISC on advanced multiprocessing OS: DEC VAX, VMS, MACRO-32

        .title    hello

        .psect    data, wrt, noexe

chan:   .blkw     1
iosb:   .blkq     1
term:   .ascid    "SYS$OUTPUT"
msg:    .ascii    "Hello, world!"
len =   . - msg

        .psect    code, nowrt, exe

        .entry    hello, ^m<>

        ; Establish a channel for terminal I/O
        $assign_s devnam=term, -
                  chan=chan
        blbc      r0, end

        ; Queue the I/O request
        $qiow_s   chan=chan, -
                  func=#io$_writevblk, -
                  iosb=iosb, -
                  p1=msg, -
                  p2=#len

        ; Check the status and the IOSB status
        blbc      r0, end
        movzwl    iosb, r0

        ; Return to operating system
end:    ret

       .end       hello

[edit] Mainframe: IBM z/Architecture series using BAL

HELLO    CSECT               The name of this program is 'HELLO'
         USING *,12          Tell assembler what register we are using
         SAVE (14,12)        Save registers
         LR    12,15         Use Register 12 for this program  
         WTO   'Hello, world!' Write To Operator
         RETURN (14,12)      Return to calling party
         END  HELLO          This is the end of the program           

[edit] RISC processor: ARM, RISC OS, BBC BASIC's in-line assembler

.program         
         ADR R0,message
         SWI "OS_Write0"
         SWI "OS_Exit"
.message         
         DCS "Hello, world!"
         DCB 0
          ALIGN

or the even smaller version (from qUE);

         SWI"OS_WriteS":EQUS"Hello, world!":EQUB0:ALIGN:MOVPC,R14

[edit] RISC processor: MIPS architecture

         .data
msg:     .asciiz "Hello, world!"
         .align 2
         .text
         .globl main      
main:
         la $a0,msg
         li $v0,4
         syscall
         jr $ra

[edit] RISC processor: PowerPC, Mac OS X, GAS

.data
msg:
    .ascii "Hello, world!\n"
    len = . - msg

.text
    .globl _main

_main:
    li r0, 4 ; write
    li r3, 1 ; stdout
    addis r4, 0, ha16(msg) ; high 16 bits of address
    addi r4, r4, lo16(msg) ; low 16 bits of address
    li r5, len ; length
    sc

    li r0, 1 ; exit
    li r3, 0 ; exit status
    sc

[edit] AutoHotkey

MsgBox, Hello`, world!

[edit] AutoIt

MsgBox(1,'','Hello, world!')

[edit] Avenue - Scripting language for ArcView GIS

MsgBox("Hello, world!","aTitle")

[edit] AWK

BEGIN { print "Hello, world!" }

[edit] B

This is the first known Hello, world! program ever written:[1]

main( ) {
  extrn a, b, c;
  putchar(a); putchar(b); putchar(c); putchar('!*n');
}
a 'hell';
b 'o, w';
c 'orld';

[edit] Baan Tools

Also known as Triton Tools on older versions. On Baan ERP you can create a program on 3GL or 4GL mode.

[edit] 3GL Format

function main()
{
    message("Hello, world!")
}

[edit] 4GL Format

choice.cont.process:
on.choice:
    message("Hello, world!")

On this last case you should press the Continue button to show the message.

[edit] Bash or sh

See also UNIX-style shell.

echo 'Hello, world!'

or

printf 'Hello, world!\n'

[edit] Batch (MS-DOS)

@echo Hello World!

[edit] OR

@echo off
set hellostring=Hello World!
echo %hellostring%

[edit] C++

#include <iostream>
 
int main()
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

[edit] OR

#include <iostream>
using namespace std;
 
int main()
{
    cout << "Hello, World!" << endl;
    return 0;
}


[edit] C#

public class HelloWorld
{
    static int Main()
    {
        System.Console.Write("Hello, World!\n");
    }
}

[edit] OR

public class HelloWorld
{
    static int Main()
    {
        System.Console.WriteLine("Hello, World!");
    }
}

[edit] DarkBasic

print "Hello World"
wait key

[edit] Fortran

[edit] Fortran 77

00    program hello
      write(*,*) 'Hello World!'
      stop
      end

[edit] Fortran 90/95

program hello
    write(*,*) 'Hello, World!'
end program hello

[edit] GW-BASIC

10 PRINT "Hello, World!"
20 END

[edit] Haskell

main = putStrLn "Hello, World!"

[edit] J

'Hello, world!'   NB. interactive mode only
'Hello World!' 1!:2(2)   NB. (2) - screen, (4) - stdout

[edit] Java

class HelloWorld
{  
        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}

[edit] OR

class HelloWorld
{  
        public static void main(String args[])
        {
           String h = "Hello World!";
           System.out.println( h );
        }
}

[edit] JavaScript

document.write ("Hello, world!") ; //on the page
alert ("Hello, world!") ; //optional; on a popup window

[edit] OR

var h = "Hello";
var w = "World";
document.write(h + " " + w + "!"); //Write to document
window.alert(h + " " + w + "!"); //Popup box

[edit] Lexico (in spanish)

tarea muestre "Hello World!"

[edit] Liberty BASIC

print "Hello World!"

[edit] OR

hello$ = "Hello World"
print hello$

[edit] Classic BASIC

10 PRINT "HELLO WORLD"

[edit] OpenScript

-- in a popup window
request "Hello world"

[edit] Pascal

program HelloWorld;
 
begin
   writeln('Hello, world!');
end.

[edit] Perl

[edit] As PL file:

print "Hello World!";

[edit] As CGI file:

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<H1>Hello World!</H1>";

[edit] PHP

<?php
    echo 'Hello, world!';
?>
#or use short-hand echoing, syntaxed as such:
<?="Hello, world!"?>

[edit] Prolog

print('Hello, world!').

[edit] Python

Python 2.x:

#!/usr/bin/env python
 
print "Hello, World!"

Python 3.x:

#!/usr/bin/env python
 
print("Hello, World!")

[edit] Revolution

(This works the same for Transcript or xTalk)

[edit] Printed in the message box
put "Hello, World!" 
[edit] Shown within a dialog box
answer "Hello, world!" 
[edit] Printed on the main window interface
create field "myField"
set the text of field "myField" to "Hello, world!"  
[edit] As CGI file
#!revolution 

on startup 
  put "Content-Type: text/plain" & cr & cr 
  put "Hello World!" 
end startup 

[edit] Ruby

puts 'Hello, World!'

[edit] Scheme

(display "Hello, World!\n")

[edit] Seed7

$ include "seed7_05.s7i";

const proc: main is func
  begin
    writeln("Hello, world");
  end func;

[edit] Standard ML

fun hello = (fn () => "hello, world!"); 
hello ();

[edit] Tcl

set h Hello
set w World
puts "$h $w!"

[edit] x86 Assembler (MS-DOS)

The source should be compiled and linked with Turbo Assembler Under MS-DOS.

    .model tiny

    .data
message    db    'Hello, World!'
    
    .code
    org 100h

start:
    mov ah,9
    mov dx,offset message
    int 21h

    ret
    end start

[edit] GNU Assembler (Linux)

.text
    .global _start
_start:
    movl	$len,%edx
    movl	$helloworld,%ecx
    movl	$1,%ebx
    movl	$4,%eax
    int	$0x80
    movl	$0,%ebx
    movl	$1,%eax
    int	$0x80
.data
helloworld:
    .ascii	"Hello, world!\n"
    len = . - helloworld

[edit] VHDL

     use std.textio.all; --  Imports the standard textio package.
     
     entity hello_world is
     end hello_world;
     
     architecture behaviour of hello_world is
     begin
        process
           variable l : line;
        begin
           write (l, String'("Hello world!"));
           writeline (output, l);
           wait;
        end process;
     end behaviour;
In other languages