280 likes | 766 Views
Introduction to Assembly Programming using NASM. Working Environment. CPU – Core 2 Duo, 64bit with 2.3 GHz clock frequency OS – Ubuntu 12.04, 64bit Other tools to be used Editor – gedit , a GNU editor Assembler – NASM ( Netwide Assembler ) LINKER – LD , a GNU linker.
E N D
Working Environment • CPU – Core 2 Duo, 64bit with 2.3 GHz clock frequency • OS – Ubuntu 12.04, 64bit Other tools to be used • Editor – gedit, a GNU editor • Assembler – NASM (Netwide Assembler) • LINKER – LD , a GNU linker
NASM - The Netwide Assembler • an 80x86 and x86-64 assembler designed for portability and modularity • supports a range of object file formats - a.out, ELF, COFF, OBJ, WIN32, WIN64, etc. • Default format - binary
Ld – GNU Linker • can read, combine and write object files in many different formats such as COFF, ELF, etc. • Different formats may be linked together to produce any available kind of object file. • Elf – executable & linkable file format of object file & executable file – supported by linux
Commands • To assemble nasm–f elf -o hello.o hello.nasm • To link ld –o hello hello.o • To execute - ./hello
Linux System call – 32bit execution Using INT 80h • SYS_Write • Sys_Read • Sys_Exit
Sys_write call - Write characters to std_out device Call with EAX = 4 ; function number EBX = 1 ; file descriptor for std_out ECX = Address of memory variable EDX = Count of bytes to display Returns Nothing
Sys_Read call - To read characters from std_in devices Call with EAX = 3 ; function number EBX = 0 ; file descriptor for std-in ECX = Address of memory variable EDX = Maximum Count of bytes to display Returns In EAX Register = actual bytes read
Sys_exit call - Terminate current process and transfer control back to OS Call with EAX = 1 ; function number EBX = 0 ; for zero error return code Returns Nothing
Structure of assembly program section .data ; initialiseddata declaration section .bss ;uninitialized data declaration section .text global _start _start: ;code
Fundamental data types b Byte 8bits 1byte w Word16bits 2 bytes d Double Word 32bits 4bytes q QuadWord64bits 8bytes
SYS_WRITE call (64bit) Write characters to std_out device • movrax, 1 ; function number for sys_write • movrdi, 1 ; fille descriptor id for std_out ;device • movrsi, msg ; address of the variable • movrdx, msglen ; count of bytes to display • syscall ; system call
SYS_READ call (64bit) sys_read call to accept the number • movrax, 0 ; function number for sys_read • movrdi, 0 ; file descriptor ID for std_in device • movrsi, arr ;address of variable used to ;store data • movrdx, 8 ; maximum bytes to read • syscall
SYS_EXIT call (64bit) • movrax, 60 ; function number for sys_exit • movrdi, 0 ; return code for zero error • syscall
Assignments • To accept username, display the same. Also display the length. • To accept username, display the same. Also display the length. (Using macro) • To accept 5, 32bit numbers. Store them in an array. Display the same. • To accept 5, 64bit numbers. Store them in an array. Display the same. (64bit execution) • To add 5, 32bit numbers stored in an array (using procedure)