Hello World -- linux x86 or x64

+9 M .A.K · May 30, 2014
First things first we need some tools: "nasm" the assambler (gonna create ".o" file(object file)) and "ld" the linker (gonna create executable)
and actually you need to just install "nasm" (if u r using ubuntu): sudo apt-get install nasm
"ld" is already installed with your linux distro.

now goto "Where ever you like directory" (<= gonna refer to this) and create a brand new ".asm" post-fix file.(like "helloworld.asm"(<= gonna refer to this)).
and paste this code in there:


;  Executable name : Hello_World
;  Description     : A simple program in assembly for Linux, using NASM 2.05,
;    demonstrating Hello_World program in


SECTION .data   ; Section containing initialised data

HWMsg: db "Hello World",10 ;find out what 10 is for:)
HWLen: equ $-HWMsg

SECTION .bss   ; Section containing uninitialized data

SECTION .text   ; Section containing code

global _start   ; Linker needs this to find the entry poin!

_start:
nop   ;
mov eax,4   ; Specify sys_write call
mov ebx,1   ; Specify File Descriptor 1: Standard Outpt
mov ecx,HWMsg   ; Pass offset of the message
mov edx,HWLen   ; Pass the length of the message
int 80H   ; Make kernel call

MOV eax,1   ; Code for Exit Syscall
mov ebx,0   ; Return a code of zero
int 80H   ; Make kernel call



after that we need to assemble it so open a terminal (if you already didn't) and cd to "Where ever you like directory".
if you are using a system with x86 (32bit) cpu then use this command:


nasm -f elf -g -F stabs helloworld.asm



or if you are using a x64 (64bit) system:


nasm -f elf64 -g -F stabs helloworld.asm


(use proper file name!)

now we did assembled it so what?! we need also to link it!
if you check that directory you gonna see a new file with the same name as ".asm" file but with a ".o" post-fix.(in my case: "helloworld.o"(<= gonna refer to this)).
use this to link the ".o" file and create an executable named "helloworld":


ld -o helloworld helloworld.o



"helloworld"(<= gonna refer to this) is the name of the executable.

and now hold your breath and type: 


./helloworld



if every thing goes OK you gonna see a "Hello World!" string on the screen...

Post a Reply

Replies

Oldest  Newest  Rating
+3 M .A.K · June 3, 2014
Of course it's not high level!
but it's worth learning cause gives u a better understanding of the programs mechanisms.
+2 Austin Elliott · June 14, 2014
Woah, assembly looks really hard. I'll stick with my "print("Hello, world!") for now. :)
+1 M .A.K · July 9, 2014
Yes, Of course.
0 Braydon Davis · June 3, 2014
Jee, assembly takes so much. I'd just rather write:

System.out.println ( "Hello World" );

lol
0 Tatrasiel R · February 1, 2015
This is because you're using the long way of doing things.....

When working in Assembly feel free to use C functions . This will save you a lot of time and not only that many programs at that level will make known C calls like memalloc and memcpy.
Also the code you've written will only use Syscalls in Linux....  Make your code be cross platform. :)
look at this. It's much smaller and easier to read. Assembly is simple! Don't over complicate it!;)


global _main
extern _printf




section .data
msg db "THIS WORKED!",0x13,0

section .bss

section .text

_main:

push ebp
mov ebp, esp

push msg
call _printf
add esp, 4

mov esp,ebp
pop ebp

ret
0 Colin James · August 8, 2014
Nice job on the topic. Sadly the only form of 'Assembly' I've ever had to use is CIL and some PPC (for Xbox 360 xex decompilation in IDA). 

I might learn x86 assembly though.
0 Vu L. · July 4, 2014
The 'nop' command simply means 'no operation', therefore it doesn't do anything for the program and can be removed. It may be useful in some cases when you're debugging.
0 Corbin Matschull · January 31, 2015
Why use 
System.out.println("Hello World!");

When you can use,

printf("Hello world!");

Ah the life of a C/C++ developer.
0 Jason Amador · July 22, 2014
Assembly is hard, and there are few modern applications for it to my knowledge aside from extreme optimization, but it is empowering to understand what the high level languages are doing in the hardware. I guess that's why it's included in so many university  curriculums (bad grammar? Curricula?)
Anyhow, I will post a bubble sort optimization challenge soon. We did it in a course and it seemed like I was the only one who cared, so maybe this  community will want to really tackle it and compare notes.
0 Tatrasiel R · August 30, 2014
Assembly isn't bad at all. Just think of functions and arguments... I can probably make some tutorials for this and post them here.
  • 1

Assembly

106,977 followers
About

Language that is commonly used in operating systems, compilers, and other low level programs.

Links
Moderators
Bucky Roberts Administrator