Computer programming/Hello world

From testwiki
Jump to navigation Jump to search

Template:Mergefrom

4DOS batch

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

@echo Hello, world!

Ingres 4GL

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

ABAP - SAP AG

REPORT ZELLO.
WRITE 'Hello, world!'.

ABC

WRITE "Hello, world!"

ActionScript

trace("Hello, world!");

ActionScript 3

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

Ada

with TEXT_IO;

procedure HELLO is
begin
   TEXT_IO.PUT_LINE ("Hello, world!");
end HELLO;

ALGOL 68

The popular upper-case stropping convention for bold words:

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

OR

Using prime stropping suitable for punch cards:

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

OR

( print("Hello, world!") )

AmigaE

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

ANSI C

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}

APL

    RHWΔPGM 
[1]R'HELLOWORLD!
    
  • 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 but 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!!

AppleScript

return "Hello, world!"

ASP

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

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!

Assembly language

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

See the example section of the PDP-8 article.

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
 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

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

Accumulator/Index microcoded machine: Data General Nova, RDOS

See the example section of the Nova article.

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

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

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 !!!$'

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

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
 
      msg db 'Hello, world!', 0xA
      msg_len = $-msg

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

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

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         ; /

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

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

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

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

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

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

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           

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

RISC processor: MIPS architecture

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

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

AutoHotkey

MsgBox, Hello`, world!

AutoIt

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

Avenue - Scripting language for ArcView GIS

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

AWK

BEGIN { print "Hello, world!" }

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';

Baan Tools

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

3GL Format

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

4GL Format

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

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

Bash or sh

See also UNIX-style shell.

echo 'Hello, world!'

or

printf 'Hello, world!\n'

Batch (MS-DOS)

@echo Hello World!

OR

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

C++

#include <iostream>

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

OR

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, World!" << endl;
    return 0;
}

DarkBasic

print "Hello World"
wait key

Fortran

Fortran 77

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

Fortran 90/95

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

GW-BASIC

10 PRINT "Hello, World!"
20 END

Haskell

main = putStrLn "Hello, World!"

J

'Hello, World!'

Java

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

OR

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

JavaScript

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

OR

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

Liberty BASIC

print "Hello World!"

OR

hello$ = "Hello World"
print hello$

Classic BASIC

10 PRINT "HELLO WORLD"

OpenScript

-- in a popup window
request "Hello world"

Pascal

program HelloWorld;

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

Perl

As PL file:

print "Hello World!";

As CGI file:

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

PHP

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

Python

print "Hello, World!"

Revolution

(This works the same for Transcript or xTalk)

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

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

Ruby

puts 'Hello, World!'

Seed7

$ include "seed7_05.s7i";

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

Tcl

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

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

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

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;