My Guru for this is Vivek Ramachand
Structure of an Assembly Program
.data - is where all initialized data resides.
.bss - is where all uninitialized data resides.
.text - is where the program instructions resides.
.globl _start - Will call external callable routines.
_start: is comparable with main() function of C programming.
Linux System calls
It is a library which kernel exposes to get various tasks done.
List of all calls are available in
bala@lappi:~/ASM$ cat /usr/include/asm/unistd.h
# ifdef __i386__
# include "unistd_32.h"
# else
# include "unistd_64.h"
# endif
bala@lappi:~/ASM$ head -15 /usr/include/asm/unistd_32.h
#ifndef _ASM_X86_UNISTD_32_H
#define _ASM_X86_UNISTD_32_H
/*
* This file contains the system call numbers.
*/
#define __NR_restart_syscall 0
#define __NR_exit 1
#define __NR_fork 2
#define __NR_read 3
#define __NR_write 4
#define __NR_open 5
#define __NR_close 6
#define __NR_waitpid 7
We can call these system calls by invoking INT 0x80
Passing arguments to syscalls
EAX - System Call Number
EBX - First Argument
ECX - Second Argument
EDX - Third Argument
ESI - Fourth Argument
EDI - Fifth Argument
Coding Simple exit program in Assembly
Calling exit(0) to exit a program
Function definition
void_exit(int status);
1.Sys call # for exit() is 1, so we need to load EAX with 1 with the command
int 0x80
.text
.globl _start
_start:
movl $1, %eax
movl $0, %ebx
int $0x80
bala@bala-laptop:~/ASM$
Now to compile the Assemble program we use the "as" compiler, to create the exit object file.
bala@bala-laptop:~/ASM$ as -o exit.o exit.s
This alone cannot do any thing we need to have a linker
bala@bala-laptop:~/ASM$ ./exit
The exit status is zero :-)
bala@bala-laptop:~/ASM$ echo $?
0
bala@bala-laptop:~/ASM$
Now we will attempt to write a Hello Earth program :-)
The syscall used to print "Hello Earth" is called write()
Then we need to exit the program gracefully with exit()
The write() syscall needs three parameters
ssize_t write(int fd, const void *buf, size_t count);
The syscall number for Write is 4 load it in EAX
The File Descriptor of STDOUT is 1 load in EBX
Buf -> is a pointer to the mem location containing "Hello Earth" in ECX
count = string length in EDX
# This is my 1st print program
.data
HelloEarthString:
.ascii "Hello Earth\n"
.text
.globl _start
_start:
#Let us load all argument for write in registers
movl $4, %eax
movl $1, %ebx
movl $HelloEarthString, %ecx
movl $12, %edx
int $0x80
#Now let us exit this program gracefully
movl $1, %eax
movl $0, %ebx
int $0x80
bala@bala-laptop:~/ASM$ as -o hello.o hello.s
Now in the step above I have create the object file
bala@bala-laptop:~/ASM$ ld -o hello hello.o
Now in the step above I have create the linked executable
bala@bala-laptop:~/ASM$ ./hello
Hello Earth
bala@bala-laptop:~/ASM$ cat hello.s
Structure of an Assembly Program
.data - is where all initialized data resides.
.bss - is where all uninitialized data resides.
.text - is where the program instructions resides.
.globl _start - Will call external callable routines.
_start: is comparable with main() function of C programming.
Linux System calls
It is a library which kernel exposes to get various tasks done.
List of all calls are available in
bala@lappi:~/ASM$ cat /usr/include/asm/unistd.h
# ifdef __i386__
# include "unistd_32.h"
# else
# include "unistd_64.h"
# endif
bala@lappi:~/ASM$ head -15 /usr/include/asm/unistd_32.h
#ifndef _ASM_X86_UNISTD_32_H
#define _ASM_X86_UNISTD_32_H
/*
* This file contains the system call numbers.
*/
#define __NR_restart_syscall 0
#define __NR_exit 1
#define __NR_fork 2
#define __NR_read 3
#define __NR_write 4
#define __NR_open 5
#define __NR_close 6
#define __NR_waitpid 7
We can call these system calls by invoking INT 0x80
Passing arguments to syscalls
EAX - System Call Number
EBX - First Argument
ECX - Second Argument
EDX - Third Argument
ESI - Fourth Argument
EDI - Fifth Argument
Coding Simple exit program in Assembly
Calling exit(0) to exit a program
Function definition
void_exit(int status);
1.Sys call # for exit() is 1, so we need to load EAX with 1 with the command
movl $1, %eax
2."Status" is "0" if program exited normally, so we need to load EBX with 0
movl $0, %ebx
3.Raise the software interrupt 0x80int 0x80
Simple exit Program starts here
bala@bala-laptop:~/ASM$ cat exit.s .text
.globl _start
_start:
movl $1, %eax
movl $0, %ebx
int $0x80
bala@bala-laptop:~/ASM$
Now to compile the Assemble program we use the "as" compiler, to create the exit object file.
bala@bala-laptop:~/ASM$ as -o exit.o exit.s
This alone cannot do any thing we need to have a linker
bala@bala-laptop:~/ASM$ ld -o exit exit.o
Now we can run the programbala@bala-laptop:~/ASM$ ./exit
The exit status is zero :-)
bala@bala-laptop:~/ASM$ echo $?
0
bala@bala-laptop:~/ASM$
Now we will attempt to write a Hello Earth program :-)
The syscall used to print "Hello Earth" is called write()
Then we need to exit the program gracefully with exit()
The write() syscall needs three parameters
ssize_t write(int fd, const void *buf, size_t count);
The syscall number for Write is 4 load it in EAX
The File Descriptor of STDOUT is 1 load in EBX
Buf -> is a pointer to the mem location containing "Hello Earth" in ECX
count = string length in EDX
Simple write Program starts here
bala@bala-laptop:~/ASM$ cat hello.s# This is my 1st print program
.data
HelloEarthString:
.ascii "Hello Earth\n"
.text
.globl _start
_start:
#Let us load all argument for write in registers
movl $4, %eax
movl $1, %ebx
movl $HelloEarthString, %ecx
movl $12, %edx
int $0x80
#Now let us exit this program gracefully
movl $1, %eax
movl $0, %ebx
int $0x80
bala@bala-laptop:~/ASM$ as -o hello.o hello.s
Now in the step above I have create the object file
bala@bala-laptop:~/ASM$ ld -o hello hello.o
Now in the step above I have create the linked executable
bala@bala-laptop:~/ASM$ ./hello
Hello Earth
bala@bala-laptop:~/ASM$ cat hello.s
No comments:
Post a Comment