- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

I'm kinda new to assembly language; also, it's very much difficult than C. sedih

Well, as you search throughout Internet, it's very hard to find a long and complete tutorial on assembly programming under Linux. You may find it in several books about assembly like Professional Assembly Programming, Art of Assembly, Assembly Step-by-step Guide... but it merely talks about assembly under Linux environment. It's quite uncommon while programming under Linux...I just don't know why around!!! If you know, tell me please!

As I read a simple tutorial about programming assembly under Linux, the language used is Netwide Assembler (NASM: http://www.nasm.us/).
Well then, let's take a look at what they guide about it.

@ The assembly program divides into three parts
1. The .data section: is used to declare initialized data and constants
- Instructions use: DB, DW, DD, DQ and DT
Samples:


section .data
message db 'Hello World!',10 ;declare a message with initialized bytes 'Hello World!", plus LF character

2. The .bss section: is used to declare uninitilized data, or called variables; memory space is reserved for variables.
- Instruction use: RESB, RESD, RESQ and REST
Samples:

section .bss
filename resb 255 ;reserve 255 bytes for filename variable

3. The .text section: the actual assembly program is written here. It begins with GLOBAL indicator to mark the entry of program, just like main() function in C/C++.
Sample:

section .text
global _main

_main:
... ;assembly code instructions here

@ Linux system calls
1. Assembly uses interrupt to create a system call. Often, int 80h
2. The order of parameters for system calls: EBX, ECX, EDX, ESI, EDI and EBP. So, you can use up to 6 parameters for a system call; well, it's enough since you cannot find any Linux system call use more than 6 parameters.
3. The register EAX always stores return value of the call.
4. The order of system call interrupt in NASM is straightforward: system call numbers then parameters from left to right.
That's some notes I take after reading a long journal on NASM.

Let's do a simple code:
I = A program uses exit() system call.

section .text
global _main

_main:
mov eax,1 ;system call number of exit() is 1
mov ebx,0 ;next is the first parameter, which will be stored in ebx; well here, we set exit code is 0
int 80h ;activate the exit() call by interrupting, postfix h indicates the value of hex, or you can write 0x80, show the same meaning

II - Compile and Link program
1. Compilation: use terminal to do the job

~ ] $ nasm -f elf exit.asm

It will produce the object file *.o

2. Linking: to connect object file and product the executable binary

~ ] $ ld -s -o exit exit.o

The binary hello will be created.

3. Run program:

~ ] $ ./exit

If it's correct, it will run smoothly w/o error or warning.

That's all I can say about NASM 'til this time.
If you wanna know more about it, why not go ahead and try. then write a tutorial for it.
People really appreciate if you like to share the rare and hard things :inlove:.

Have fun!@

0 comments

Post a Comment