intro to assembly language programming - Copy
intro to assembly language programming - Copy
Difference between high level language & low level ( assembly language)
High level languages are suitable for application programming. Such as database , web , AI,
image & video processing etc
Low level languages are suitable for system programming.eg device drivers .
We will use the NASM assembler, as it is −
Free
Lot of documentation available
Could be used on both Linux and Windows.
Procedure to create and execute a simple assembly program on Ubuntu (Linux)using nasm
8. If the program is error free, it implies hello.o object file has been created.
9. To link and create the executable give the command as
Ld –o hello hello.o
10. To execute the program write at the prompt
./hello
11. “hello world” will be displayed at the prompt
section .text
global _start
_start: ;tell linker entry point
mov edx, msglen ;message length
mov ecx, msg ;message to write
mov ebx, 1 ;choose device: screen
mov eax, 4 ; system call for write
int 80h ;call kernel
mov eax, 1 ;system call for exit
int 0x80 ;call kernel
after typing above program, save it by name hello.asm
then type the following commands:
ld –o hello hello.o
./hello
This section is for "declaring initialized data", in other words defining "variables" that already
contain stuff. However this data does not change at runtime so they're not really variables. The
.data section is used for things like filenames and buffer sizes, and you can also define constants
using the EQU instruction. Here you can use the DB, DW, DD,DQ and DT instructions. For
example:
section.data
message db 'Helloworld!' ;Declare message
msglength equ $-message ;Declare msglength & which will store the length
;of above message
Buffersize dw 1024 ;Declare buffersize to be a word with1024 bytes
This section is where you declare your variables. You use the RESB, RESW, RESD, RESQand
REST instructions to reserve uninitialized space in memory for your variables, likethis:
Section .bss
filename resb 255 ;Reserve 255 bytes
number resb 1 ;Reserve1byte
bignum resw 1 ;Reserve1word(1word=2bytes)
realarray resq 10 ;Reserve an array of 10 reals
Section .text
Global _start
_start:
The assembler directives or pseudo-ops tell the assembler about the various aspects of the
assembly process. These are non-executable and do not generate machine language instructions.
Memory Segments
A segmented memory model divides the system memory into groups of independent segments
referenced by pointers located in the segment registers. Each segment is used to contain a
specific type of data. One segment is used to contain instruction codes, another segment stores
the data elements, and a third segment keeps the program stack.
In the light of the above discussion, we can specify various memory segments as −
Data segment − It is represented by .data section and the .bss. The .data section is used
to declare the memory region, where data elements are stored for the program. This section
cannot be expanded after the data elements are declared, and it remains static throughout the
program.
The .bss section is also a static memory section that contains buffers for data to be declared later
in the program. This buffer memory is zero-filled.
Code segment − It is represented by .text section. This defines an area in memory that
stores the instruction codes. This is also a fixed area.
Stack − This segment contains data values passed to functions and procedures within the
program.
System calls
are the API interface between user programs and the Linux kernel. They are used to let the kernel
perform various system tasks, such as file access, process management
64-bit x86 uses syscall instead of interrupt 0x80. The result value will be in %rax
For making a system call using an interrupt, you have to pass all required information to the
kernel by copying them into general purpose registers.
Each system call has a fixed number.
system calls take parameters to perform their task. Those parameters are passed by writing them
in the appropriate registers before making the actual call. Each parameter index has a specific
register.
You need to take the following steps for using Linux system calls in your program −
Put the system call number in the EAX register.
Store the arguments to the system call in the registers EBX, ECX, etc.
Call the relevant interrupt (80h).
The result is usually returned in the EAX register.
Assembler directives
An assembler directive is a statement to provide necessary information to the assembler to
generate necessary machine codes.
Directives are commands to the assembler & are not instructions of processor.
Directives are also called as Pseudo-instructions
Because they are not instructions to be executed by processor, So They are not converted to
machine language instructions.
Eg
Section .data
Section .bss
Section .text
DB DW DD Char etc
Explanation of these Directives are given in write-ups.