ALP Programming INTRODUCTION TO ASSEMBLY LANGUAGE PROGRAMMING Rahul P
What is Assembly Language? • An assembly (or assembler) language, often abbreviated asm, is a low-level programming language for a computer, or other programmable device, in which there is a very strong (generally one-to-one) correspondence between the language and the architecture's machine code instructions. • Assembly language is converted into executable machine code by a utility program referred to as an assembler. The conversion process is referred to as assembly, or assembling the source code. Assembly time is the computational step where an assembler is run. • Assembly language uses a mnemonic to represent each low-level machine instruction or opcode, typically also each architectural register, flag, etc. Many operations require one or more operands in order to form a complete instruction and most assemblers can take expressions of numbers and named constants as well as registers and labels as operands, freeing the programmer from tedious repetitive calculations.
Assembler An assembler is a program that takes basic computer instructions and converts them into a pattern of bits that the computer's processor can use to perform its basic operations. Some people call these instructions assembler language and others use the term assembly language. NASM The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM is considered to be one of the most popular assemblers for Linux.
Installing NASM in Ubuntu Step 1 : Open terminal in Ubuntu ( Alt + Ctrl + T ) Step 2 : run the update command sudo apt-get update (apt-get update downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPA) Step 3: run NASM installation command sudo apt-get install nasm After the terminal processes complete, NASM is successfully installed in your system
Assembly - Basic Syntax An assembly program can be divided into three sections − •The data section, •The bss section, and •The text section.
The data Section The data section is used for declaring initialized data or constants. This data does not change at runtime. You can declare various constant values, file names, or buffer size, etc., in this section. The syntax for declaring data section is − >> section.data
The bss Section The bss section is used for declaring variables. The syntax for declaring bss section is − >> section.bss
The text section The text section is used for keeping the actual code. This section must begin with the declaration global _start, which tells the kernel where the program execution begins. The syntax for declaring text section is − >> section.text global _start _start:
Comments Assembly language comment begins with a semicolon (;). It may contain any printable character including blank. It can appear on a line by itself, like − The syntax for declaring text section is − ; This is a comment in your program code or, on the same line along with an instruction, like − add eax , ebx ; add ebx to eax
Assembly Language Statements Assembly language programs consist of three types of statements − • Executable instructions • Assembler directives • Macros. • The executable instructions or simply instructions tell the processor what to do. Each instruction consists of an operation code (opcode). Each executable instruction generates one machine language instruction. • The assembler directives tell the assembler about the various aspects of the assembly process. These are non-executable and do not generate machine language instructions. • The Macros are basically a text substitution mechanism.
SYSTEM CALLS System calls are APIs for the interface between the user space and the kernel space. Linux System Calls We can make use of Linux system calls in our assembly programs. The following steps are needed for using Linux system calls in our 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.
Linux System Calls ( continues ) The following code snippet shows the use of the system call sys_exit: mov eax,1 ; system call number (sys_exit) int 0x80 ; call kernel The following code snippet shows the use of the system call sys_write: mov edx,4 ; message length mov ecx,msg ; message to write mov ebx,1 ; file descriptor (stdout) mov eax,4 ; system call number (sys_write) int 0x80 ; call kernel
Linux syscall numbers Name Number in eax sys_restart_syscall 0x00 sys_exit 0x01 sys_fork 0x02 sys_read 0x03 sys_write 0x04 sys_open 0x05 sys_close 0x06
File Descriptor A file descriptor is a 16-bit integer assigned to a file as a file id. When a new file is created or an existing file is opened, the file descriptor is used for accessing the file. File descriptor of the standard file streams – stdin - 0 stdout - 1 stderr - 2
The Hello World Program in Assembly section .data msg db 'Hello, world!', 0xa ;string to be printed len equ $ - msg ;length of the string section .text global _start ;must be declared for linker (ld) section .bss _start: ;tells linker entry point mov edx,len ;message length mov ecx,msg ;message to write ;call kernel mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel
Compiling and Linking an Assembly Program Step 1: Type the code using a text editor and save it as filename.asm Step 2: Make sure that you are in the same directory as where you saved filename.asm. Step3 : To assemble the program, type nasm -f elf filename.asm (If there is any error, you will be prompted about that at this stage. ) Step 4: To link the object file and create an executable file, type ld -m elf_i386 -s -o filename filename.o Step 5 : Execute the program by typing ./hello
Knowledge is having the right ANSWER. Intelligence is asking the right QUESTION. ?
Rahul P

Introduction to Assembly Language Programming

  • 1.
    ALP Programming INTRODUCTION TOASSEMBLY LANGUAGE PROGRAMMING Rahul P
  • 2.
    What is AssemblyLanguage? • An assembly (or assembler) language, often abbreviated asm, is a low-level programming language for a computer, or other programmable device, in which there is a very strong (generally one-to-one) correspondence between the language and the architecture's machine code instructions. • Assembly language is converted into executable machine code by a utility program referred to as an assembler. The conversion process is referred to as assembly, or assembling the source code. Assembly time is the computational step where an assembler is run. • Assembly language uses a mnemonic to represent each low-level machine instruction or opcode, typically also each architectural register, flag, etc. Many operations require one or more operands in order to form a complete instruction and most assemblers can take expressions of numbers and named constants as well as registers and labels as operands, freeing the programmer from tedious repetitive calculations.
  • 3.
    Assembler An assembler isa program that takes basic computer instructions and converts them into a pattern of bits that the computer's processor can use to perform its basic operations. Some people call these instructions assembler language and others use the term assembly language. NASM The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM is considered to be one of the most popular assemblers for Linux.
  • 4.
    Installing NASM inUbuntu Step 1 : Open terminal in Ubuntu ( Alt + Ctrl + T ) Step 2 : run the update command sudo apt-get update (apt-get update downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPA) Step 3: run NASM installation command sudo apt-get install nasm After the terminal processes complete, NASM is successfully installed in your system
  • 5.
    Assembly - BasicSyntax An assembly program can be divided into three sections − •The data section, •The bss section, and •The text section.
  • 6.
    The data Section Thedata section is used for declaring initialized data or constants. This data does not change at runtime. You can declare various constant values, file names, or buffer size, etc., in this section. The syntax for declaring data section is − >> section.data
  • 7.
    The bss Section Thebss section is used for declaring variables. The syntax for declaring bss section is − >> section.bss
  • 8.
    The text section Thetext section is used for keeping the actual code. This section must begin with the declaration global _start, which tells the kernel where the program execution begins. The syntax for declaring text section is − >> section.text global _start _start:
  • 9.
    Comments Assembly language commentbegins with a semicolon (;). It may contain any printable character including blank. It can appear on a line by itself, like − The syntax for declaring text section is − ; This is a comment in your program code or, on the same line along with an instruction, like − add eax , ebx ; add ebx to eax
  • 10.
    Assembly Language Statements Assemblylanguage programs consist of three types of statements − • Executable instructions • Assembler directives • Macros. • The executable instructions or simply instructions tell the processor what to do. Each instruction consists of an operation code (opcode). Each executable instruction generates one machine language instruction. • The assembler directives tell the assembler about the various aspects of the assembly process. These are non-executable and do not generate machine language instructions. • The Macros are basically a text substitution mechanism.
  • 11.
    SYSTEM CALLS System callsare APIs for the interface between the user space and the kernel space. Linux System Calls We can make use of Linux system calls in our assembly programs. The following steps are needed for using Linux system calls in our 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.
  • 12.
    Linux System Calls( continues ) The following code snippet shows the use of the system call sys_exit: mov eax,1 ; system call number (sys_exit) int 0x80 ; call kernel The following code snippet shows the use of the system call sys_write: mov edx,4 ; message length mov ecx,msg ; message to write mov ebx,1 ; file descriptor (stdout) mov eax,4 ; system call number (sys_write) int 0x80 ; call kernel
  • 13.
    Linux syscall numbers NameNumber in eax sys_restart_syscall 0x00 sys_exit 0x01 sys_fork 0x02 sys_read 0x03 sys_write 0x04 sys_open 0x05 sys_close 0x06
  • 14.
    File Descriptor A filedescriptor is a 16-bit integer assigned to a file as a file id. When a new file is created or an existing file is opened, the file descriptor is used for accessing the file. File descriptor of the standard file streams – stdin - 0 stdout - 1 stderr - 2
  • 15.
    The Hello WorldProgram in Assembly section .data msg db 'Hello, world!', 0xa ;string to be printed len equ $ - msg ;length of the string section .text global _start ;must be declared for linker (ld) section .bss _start: ;tells linker entry point mov edx,len ;message length mov ecx,msg ;message to write ;call kernel mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel
  • 16.
    Compiling and Linkingan Assembly Program Step 1: Type the code using a text editor and save it as filename.asm Step 2: Make sure that you are in the same directory as where you saved filename.asm. Step3 : To assemble the program, type nasm -f elf filename.asm (If there is any error, you will be prompted about that at this stage. ) Step 4: To link the object file and create an executable file, type ld -m elf_i386 -s -o filename filename.o Step 5 : Execute the program by typing ./hello
  • 17.
    Knowledge is having theright ANSWER. Intelligence is asking the right QUESTION. ?
  • 18.