Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Assembly Language Programming (ALP) Tutorials (http://www.go4expert.com/articles/assembly-language-programming/)
-   -   Hello World in Assembly (http://www.go4expert.com/articles/hello-world-assembly-t24919/)

lionaneesh 9Feb2011 22:38

Hello World in Assembly
 
Assembly language is one of the oldest low-level programming language for computers , micro-processors etc etc...It implements a symbolic representation of the Machine Opcodes needed to program a given CPU Architecture...Its a language based on mnemonics , labels , instructions , memory addresses and Registers... A major disadvantage of programming in assembly is that it is not at all platform independent and thus , cannot be ported to other architectures...

The Code



In this article we'll be learning about a simple Hello World Program in x86 Unix Assembly and Will be using basic syscall table to accomplish our tasks...

We'll be using write() function to write 'Hello World' to STDIN

Syntax

Code:

write(int fd, char *Buff, int NumBytes);
In a Assembly program the arguments can be passed on to the function via a simple call stack or via registers..

We'll be using registers to pass the arguments...

Points to remember
  1. eax should contain the syscall number
  2. ebx should point to the first
  3. ecx should point to the second
  4. edx should point to the third
  5. esx should point to the fourth
  6. edi should point to the fifth
Syscall(system call) table can be looked up in /usr/include/asm/unistd_32.h

helloworld.asm
Code:

segment .data

helloWorldString  db        "Hello World",0xA ; 0xA is ascii code for line feed

helloWorldStringlen equ $ -  helloWorldString ; it contains the length of string



segment .text

global _start



_start :

        mov eax,4                        ;syscall of write is 4

        mov ebx,1                        ;int fd = STDIN is 1

        mov ecx,helloWorldString        ;char *

        mov edx,helloWorldStringlen        ;int len

        int 0x80                        ;shift to kernel mode


; exit call


        mov eax,1                        ;syscall of exit

        mov ebx,0
                        ; exit(0)
        int 0x80
                        ; shift to kernel mode

segment .data

The first 2 lines simply define a db string containing 'Hello World' and the length of string

segment .text

First we delare a global declaration of _start lable so as to provide a entry point to the program..

The main code have been commented above for explanation , so as to provide a better understanding.

Lets now Assemble and link the program

Code:

aneesh@aneesh-laptop:~/Programming/ASM$ nasm -f elf32 helloworld.asm -o helloworld.o

aneesh@aneesh-laptop:~/Programming/ASM$ ld helloworld.o -o helloworld

Now lets run the program

Code:

aneesh@aneesh-laptop:~/Programming/ASM$ ./helloworld

Hello World

That's enough for this Article … Stay tuned for more...

lionaneesh 12Feb2011 10:54

Re: Hello World in Assembly
 
Thanks for reading and accepting..

davidriched 15Feb2011 09:53

Hello World in Assembly
 
This is a wonderful opinion. The things mentioned are unanimous and needs to be appreciated by everyone. The initiative taken for the concern is very serious and need an attention of every one. This

lionaneesh 15Feb2011 13:21

Re: Hello World in Assembly
 
Quote:

Originally Posted by davidriched (Post 79269)
This is a wonderful opinion. The things mentioned are unanimous and needs to be appreciated by everyone. The initiative taken for the concern is very serious and need an attention of every one. This

Sorry Couldn't understand you!!!!

Maybe you have posted your reply in the wrong thread!!!

sudha06 30Mar2011 18:19

Re: Hello World in Assembly
 
very nice post world in assembly

lionaneesh 31Mar2011 17:15

Re: Hello World in Assembly
 
Quote:

Originally Posted by sudha06 (Post 81272)
very nice post world in assembly

Thanks and finally i am over with my boards and now ready to post some great articles to G4E


All times are GMT +5.5. The time now is 09:27.