32 Bit Linux Assembly



Hi All, this blog post contains the basic hello world program and explanation to Linux based 32 Bit Assembly Language. Before moving to this topic, we should know "What is Assembly Language?" so, I would like to start my blog with the answer to the above question.

Assembly language is a low-level programming language with which we can communicate directly with the microprocessor. also, it is very specific for a processor family. I will explain the intel 32-bit processor-based assembly language in this blog. and it has one to one correspondence with Machine Code. Machine code is a binary format language that can be understood by machines only. because a human can not handle the bunch of binary letters. so assembly language came to short out this issue. it is working as an interpreter or bridge between humans and machines. you can get more idea about the scenario from below the image.



In Linux, you can use "lscpu or cat /proc/cupinfor" these commands to find the processor information.

before moving to the assembly language we will take a look at CPU and register because it will make our life too easier. You can check the below image, which is represented the CPU internal architecture.


during the execution executes an instruction, it would required a temporary storage unit to store variables, data which is want to use different computations. so, register was taken this task in modern computers. now let's look registers little bit deeply. INTEL 32 bit CPU has the main 6 register category those are General Purpose Registers, Segment Registers, Flags / EIP, Floating Point Unit Registers, MMX Registers, XMM Registers.

General Register
under this there are 8 registers is listed.




  • eax = it is storing operands and result from the different system call.
  • ebx = it is a base register - pointer to data.
  • ecx = it is a counter register - use to loop.
  • edx = it is a data register - i/o pointer.
  • esi/edi = data pointer registers for memory operations.
  • esp = stack pointer register.
  • ebp = stack data pointer register.
We will discuss other registers in future posts. now we should go deeply in general-purpose registers with the basic 32-bit assembly language program. you can see the basic structure of 32-bit assembly language from the below image.

Normally assembly language is divided by two main parts those are TEXT section and the DATA section. all instructions are handled by the TEXT section and DATA sections handle data such as strings and all. also text section should have an entry point of the program which is the same as main function in other program languages. under this entry point, there is two things has to be listed. those are instructions and exit. also, you can see the sample format of the real assembly code from below the image.


before go deep in assembly language text and data section we should know some basic things about system calls. System call provides a very simple interface for user space programs to request the kernel to do the complicated task for them. you can check available system call from user OS by using this command vi /usr/include/i386-linux-gnu/asm/unistd_32.h (note:- this path would be changed based on the operating system). If you want to print some strings on screen you should use the write system call. sometimes we don't anything about write system calls but it not a big issue because we can get help from os document by using man command. for example "man 2 write". when you enter that command you will get a document about write system call like as below image.


there is 3 parameter in the write function. those are,
  1. fd:- it represents std value such as stdin(0), stdout(1), stderr(2).
  2. *buf:- what want to print (output value).
  3. count:- the size of output value.
the below image has shown to us how we can collaborate with register while we write code in assembly.



now let's go through the code.



the above image has a simple hello world program in 32-bit assembly language. now i am going to discuss the keywords which are used on that.
  1. ; - if the line or word starts with this symbol, it should be a comment.
  2. mov - it moves the instructions and data to registers
  3. int - it manages system call interrupt handler 
Control Instructions

this is controlling the flow of the program based on different events and calculations. in assembly language control instruction is depended on lot of flags to determine, what to do. we can divided control instructions as two main type in assembly. those are conditional and unconditional.
  1. Unconditional (It is slimier to "GO TO" command in C language.)
    It has 2 type. those are near jump and far jump.
    Near Jump : Jump to current code segment.
         
    Short Jump :  Jump to Current Position.
    Far Jumop : Jump to another Segment.
  2. Conditional Jump (JXX)(it's likes "if" statment.)
    Example : JNZ (Not equal to zero)
That's all for this post. Up coming posts will have more interesting stuff based on assembly languages.

Comments

Popular posts from this blog

Install android studio on the parrot os

How to do simple brute force attack with burp suite

PoC video of How to Hack Gmail and Bitcoin Wallet using SS7 flaw