fdp_mac

17
NASM Intro

Upload: vipuls021

Post on 02-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 1/17

NASM Intro

Page 2: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 2/17

Working Environment

• CPU –  Core 2 Duo, 64bit with 2.3 GHz clockfrequency

• OS –  Ubuntu 12.04, 64bit

Other tools to be used 

• Editor –  gedit, a GNU editor

• Assembler –  NASM (Netwide Assembler)

• LINKER –  LD , a GNU linker

Page 3: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 3/17

 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

Page 4: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 4/17

Page 5: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 5/17

Page 6: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 6/17

Linux System call –  32bit execution

Using INT 80h

SYS_Write• Sys_Read

• Sys_Exit

Page 7: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 7/17

 

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

Page 8: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 8/17

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 todisplay

Returns

In EAX Register = actual bytes read

Page 9: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 9/17

Page 10: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 10/17

Structure of assembly program

section .data

; initialised data declaration

section .bss;uninitialized data declaration

section .text

global _start

 _start:

;code

Page 11: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 11/17

Directive Effect 

.text Switch to text segment.

.data

Switch to initialized part of

data segment.

.bss

Switch to uninitialized part of

data segment.

".bss" stands for "block starting symbol".

Page 12: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 12/17

Fundamental data types

 b Byte 8bits 1byte

w Word 16bits 2 bytesd Double Word 32bits 4bytes

q QuadWord 64bits 8bytes

Page 13: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 13/17

32bit (Protected

Mode ) 

 NASM syntax 

(case sensitive) 

Data types supported   byte, word, double word 

data declaration  arr db 1h, 2h, 3h 

Creating Pointer

Registers to be used to

hold address / pointers 

lea esi, arr  

EAX/EBX/ECX/EDX/ES

I/EDI/EBP 

mov esi, arr  

EAX/EBX/ECX/EDX/E

SI/EDI/EBP 

Direct addressing 

mov al, num 

mov al, [num] 

Indirect addressing  mov al, [si] 

mov al, [esi] 

Indirect Indexed

addressing 

mov al, arr[si] 

mov al, [arr+si] 

 NASM Syntax Vs ‘386

Page 14: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 14/17

SYS_WRITE call (64bit)

Write characters to std_out device

• mov rax, 1 ; function number for sys_write

• mov rdi, 1 ; fille descriptor id for std_out

;device

mov rsi, msg ; address of the variable• mov rdx, msglen ; count of bytes to display

•   syscall ; system call

Page 15: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 15/17

SYS_READ call (64bit)

sys_read call to accept the number

• mov rax, 0 ; function number for sys_read

• mov rdi, 0 ; file descriptor ID for std_in device

• mov rsi, arr ;address of variable used to;store data

• mov rdx, 8 ; maximum bytes to read

• syscall

Page 16: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 16/17

SYS_EXIT call (64bit)

mov rax, 60 ; function number for sys_exit• mov rdi, 0 ; return code for zero error

• syscall

Page 17: fdp_mac

8/10/2019 fdp_mac

http://slidepdf.com/reader/full/fdpmac 17/17

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 thesame. 

• 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)