Hello world programs in assembly languages

From SytechPedia

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

See the example program in the Wikipedia PDP-8 article.

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

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

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

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[edit]

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

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

See the example section of the Nova article.

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

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)[edit]

.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)[edit]

; 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[edit]

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

Using FASM import macro, unicode (MessageBoxW is one of few unicode functions 'supported' by Windows 9x/ME) and section sharing, no relocation (not required for 32-bit Windows NT executables, recommended for DOS-based Windows, required for x64), no heap - Not a beginners example but only 1024 instead of 3072 bytes:

format PE GUI 4.0
heap 0
entry start
include 'win32a.inc'

section '.text' code import readable executable data
  library kernel, 'KERNEL32.DLL',\
    user,'USER32.DLL'

  import kernel,\
    ExitProcess, 'ExitProcess'
  import user,\
    MessageBoxW, 'MessageBoxW'

  start:
    xor ebx, ebx
    push ebx
    push ebx
    push _message
    push ebx
    call [MessageBoxW]

    push ebx
    call [ExitProcess]

_message du 'Hello, world!' ,0
section '.reloc' fixups data readable discardable

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

format ELF executable
entry _start
 
_start:
     mov eax, 4
     mov ebx, 1
     mov ecx, msg
     mov edx, msg_len
     int 80h

     mov ebx, 0
     mov eax, 1
     int 80h
 
     msg db 'Hello, world!', 0xA
     msg_len = $-msg

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

.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[edit]

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[edit]

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[edit]

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[edit]

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[edit]

RT-11, MACRO-11[edit]

.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

Variant for Elektronika BK using BIOS function, MICRO-11[edit]

MOV     #TXT,R1              ;Moving string address to R1
         CLR     R2                   ;String length=0, means null will be the termination character
         EMT     20                   ;Print the string
         HALT
 
 TXT:    .ASCIZ  /Hello, world!/
         .END

CISC Amiga (Workbench 2.0): Motorola 68000[edit]

include lvo/exec_lib.i
        include lvo/dos_lib.i
 
        ; open DOS library
        movea.l  4.w,a6
        lea      dosname(pc),a1
        moveq    #36,d0
        jsr      _LVOOpenLibrary(a6)
        movea.l  d0,a6
 
        ; actual print string
        lea      hellostr(pc),a0
        move.l   a0,d1
        jsr      _LVOPutStr(a6)
 
        ; close DOS library
        movea.l  a6,a1
        movea.l  4.w,a6
        jsr      _LVOCloseLibrary(a6)
        rts
 
dosname     dc.b 'dos.library',0
hellostr    dc.b 'Hello, world!',0

CISC Atari: Motorola 68000[edit]

;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 Sharp X68000 (Human68K): Motorola 68000[edit]

pea (strign)    ; push string address onto stack
        dc.w $FF09      ; call DOS "print" by triggering an exception
        addq.l #4,a7    ; restore the stack pointer

        dc.w $FF00      ; call DOS "exit"

strign:
        dc.b "Hello, world!",13,10,0

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

.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[edit]

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[edit]

.program         
          ADR R0, message
          SWI "OS_Write0"
          SWI "OS_Exit"
 .message         
          EQUS "Hello, world!"
          EQUB 0
          ALIGN

or the even smaller version (from qUE);

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

RISC processor: MIPS architecture[edit]

.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[edit]

.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

Sigma 6/7/8/9 METASYMBOL[edit]

SYSTEM   BPM
 START  M:PRINT (MESS,HW)
        M:EXIT
 HW     TEXTC    'HELLO WORLD'
        END      START