bsddog | VIDEO | ARTICLE | BLOG | TAG | PROJECT | CONTACT | DONATE
bsddog

FreeBSD Hello, World!

Hello World for FreeBSD in NASM, FASM, GAS, GAS32, __ASM__, F90, COBOL and C!

Hello World in VIII different languages

⊥ John: Should I use FreeBSD?

⊤ Erik: If you can't handle OpenBSD, run Windows, otherwise go with OpenBSD.


⊥ John: Why in exotic Assembly, what's the deal with the rest?

⊤ Erik: I don't care about the rest, but Assembly …, dude! You're the exotica proper.


⊥ John: Don't you have anything better to do?

⊤ Erik: No. It's -8°C, and we've been buried under heavy snow. I've stuck in my bedchamber.


⊥ John: How should I install those assemblers and compilers?

⊤ Erik: Refer to the Handbook. They used to have a nice Handbook, but now, it's Reddit's Amma Pau edition.


⊥ John: What's Amma Pau?

⊤ Erik: I've checked his FreeBSD Wiki. He's a set of pronouns. That'll be neo-FreeBSD, aka SoyBSD.


⊥ John: Do you use FreeBSD?

⊤ Erik: I used to, but now, they're the pronouns army. I have better things to do.


⊥ John: Like what?

⊤ Erik: Doing nothing.


⊥ John: Like writing Hello World in eight different languages!

⊤ Erik: Exactly.


List of the Hello World for 8 different languages in FreeBSD

  1. NASM: Netwide Assembler
  2. FASM: Flat Assembler
  3. GAS: GNU Assembler
  4. GAS32: GNU Assembler 32-bit
  5. __ASM__: Embedded Assembly Code in a C Translation Unit
  6. F90: FORTRAN 90
  7. COBOL
  8. C: ANSI C99

Hello World in NASM (Netwide Assembler)

$ vi nasm.s

$ nasm -f elf nasm.s
$ ld -s -o nasm.out nasm.o
$ brandelf -t freebsd nasm.out
$ ./nasm.out
nasm.s
%include	'nasm.inc'


section .data
msg db 'nasm', 0Ah
hbytes equ $-msg

section .text
global _start
_start:
push dword hbytes
push dword msg
push dword stdout
sys.write

push dword 0
sys.exit
nasm.inc
%define	stdin   0

%define stdout 1
%define stderr 2

%define SYS_nosys 0
%define SYS_exit 1
%define SYS_fork 2
%define SYS_read 3
%define SYS_write 4

section .text
align 4
access.the.bsd.kernel:
int 80h
ret

%macro system 1
mov eax, %1
call access.the.bsd.kernel
%endmacro

%macro sys.exit 0
system SYS_exit
%endmacro

%macro sys.fork 0
system SYS_fork
%endmacro

%macro sys.read 0
system SYS_read
%endmacro

%macro sys.write 0
system SYS_write
%endmacro

Hello World in FASM (Flat Assembler)

$ vi fasm.s

$ fasm fasm.s
$ mv fasm fasm.out
$ brandelf -t FreeBSD fasm.out
$ chmod 755 fasm.out
$ ./fasm.out
format ELF executable

entry _start

segment readable executable

_start:
push len
push msg
push 1
mov eax, 0x4
push 0
int 0x80

xor eax, eax
int 0x80

segment readable writable
msg db "fasm", 0x0a
len = $-msg

Hello World in GAS (GNU Assembler)

$ vi gas.s

$ cc -c gas.s
$ ld gas.o -o gas.out
$ brandelf -t linux gas.out
$ ./gas.out
.global _start

.text

_start:
mov $1, %rax
mov $1, %rdi
mov $msg, %rsi
mov $4, %rdx
syscall

mov $60, %rax
xor %rdi, %rdi
syscall

msg:
.ascii "gas\n"

Hello World in GAS32 (GNU Assembler 32-bit)

$ vi gas32.s

$ cc -m32 -o gas32.out gas32.s
$ ./gas32.out
.file   "gas32.s"

.extern printf
.data

msg:
.ascii "gas\n"
.text
.globl main

main:
pushl %ebp
movl %esp, %ebp
pushl $msg

call output_message
movl $0, %eax
leave
ret

output_message:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl 8(%ebp), %eax
movl %eax, (%esp)
call printf
addl $4, %esp
movl $0, %eax
leave
ret $4

Hello World in Assembly, embedded in C Code

$ vi casm.c

$ cc -o casm.out casm.c
$ ./casm.out
#include <stdio.h>

int
main()
{
int var;

__asm__ (
"movl %%eax, %%ebx;" : "=a" (var) : "a" (0x63)
);

printf( "%c%c\n", var, var+0x10 );
return 0 ;
}

Hello World in FORTRAN 90

$ vi f90.f90

$ flang f90.f90 -o f90.out
$ ./f90.out
program f90

print *, "f90"
end program f90

Hello World in COBOL

$ vi cob.cob

$ cobc -o cob.out -x cob.cob
$ ./cob.out
identification division.

program-id. cob.
procedure division.
  display "cob".
  stop run.

Hello World in ANSI C!

$ vi c.c

$ cc -o c.out c.c
$ ./c.out
#include <stdio.h>

int
main()
{
printf("c\n");
return 0;
}

― by BSDDOG

#Blog | #FreeBSD