Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

tutorial on assembly language, Lecture notes of Engineering

concepts and methods of assemble language

Typology: Lecture notes

2016/2017

Uploaded on 09/08/2017

sushma-paladi
sushma-paladi 🇮🇳

4 documents

1 / 81

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Assembly Language Tutorial
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51

Partial preview of the text

Download tutorial on assembly language and more Lecture notes Engineering in PDF only on Docsity!

Assembly Language Tutorial

i

ASSEMBLY LANGUAGE TUTORIAL

Simply Easy Learning by tutorialspoint.com tutorialspoint.com

TUTORIALS POINT

Copyright & Disclaimer Notice

All the content and graphics on this tutorial are the property of tutorialspoint.com. Any content from

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the accuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com site or this tutorial content contains some errors, please contact us at webmaster@tutorialspoint.com

TUTORIALS POINT

  • Assembly Programming Tutorial Table of Content
  • Audience
  • Prerequisites
  • Copyright & Disclaimer Notice..................................................
  • Assembly Introduction
  • What is Assembly Language?
  • Advantages of Assembly Language
  • Basic Features of PC Hardware
  • The Binary Number System
  • The Hexadecimal Number System
  • Binary Arithmetic
  • Addressing Data in Memory
  • Assembly Environment Setup
  • Installing NASM........................................................................................
  • Assembly Basic Syntax
  • The data Section
  • The bss Section
  • The text section
  • Comments
  • Assembly Language Statements
  • Syntax of Assembly Language Statements
  • The Hello World Program in Assembly.....................................................
  • Compiling and Linking an Assembly Program in NASM
  • Assembly Memory Segments.................................................
  • Memory Segments
  • Assembly Registers
  • Processor Registers
  • Data Registers
  • Pointer Registers
  • Index Registers
  • Control Registers
  • Segment Registers
  • Example:
  • Assembly System Calls..........................................................
  • Linux System Calls
  • Example
  • Addressing Modes
  • Register Addressing TUTORIALS POINT
  • Immediate Addressing..............................................................................
  • Direct Memory Addressing
  • Direct-Offset Addressing
  • Indirect Memory Addressing.....................................................................
  • The MOV Instruction
  • SYNTAX:
  • EXAMPLE:
  • Assembly Variables
  • Allocating Storage Space for Initialized Data
  • Allocating Storage Space for Uninitialized Data
  • Multiple Definitions
  • Multiple Initializations
  • Assembly Constants
  • The EQU Directive
  • Example:
  • The %assign Directive..............................................................................
  • The %define Directive
  • Arithmetic Instructions
  • SYNTAX:
  • EXAMPLE:
  • The DEC Instruction
  • SYNTAX:
  • EXAMPLE:
  • The ADD and SUB Instructions
  • SYNTAX:
  • EXAMPLE:
  • The MUL/IMUL Instruction
  • SYNTAX:
  • EXAMPLE:
  • EXAMPLE:
  • The DIV/IDIV Instructions
  • SYNTAX:
  • EXAMPLE:
  • Logical Instructions
  • The AND Instruction
  • Example:
  • The OR Instruction
  • Example:
  • The XOR Instruction TUTORIALS POINT
  • The TEST Instruction
  • The NOT Instruction
  • Assembly Conditions..............................................................
  • The CMP Instruction.................................................................................
  • SYNTAX
  • EXAMPLE:
  • Unconditional Jump
  • SYNTAX:
  • EXAMPLE:
  • Conditional Jump
  • Example:
  • Assembly Loops.....................................................................
  • Example:
  • Assembly Numbers
  • ASCII Representation...............................................................................
  • BCD Representation
  • Example:
  • Assembly Strings
  • String Instructions
  • MOVS.......................................................................................................
  • LODS
  • CMPS
  • SCAS
  • Repetition Prefixes
  • Assembly Arrays
  • Example:
  • Assembly Procedures
  • Syntax:
  • Example:
  • Stacks Data Structure:
  • EXAMPLE:
  • Assembly Recursion
  • Assembly Macros...................................................................
  • Example:
  • Assembly File Management
  • File Descriptor
  • File Pointer
  • File Handling System Calls
  • Creating and Opening a File TUTORIALS POINT
  • Opening an Existing File
  • Reading from a File
  • Writing to a File
  • Closing a File
  • Updating a File
  • Example:
  • Memory Management
  • Example:

TUTORIALS POINT

 It is most suitable for writing interrupt service routines and other memory resident programs.

Basic Features of PC Hardware The main internal hardware of a PC consists of the processor, memory and the registers. The registers are processor components that hold data and address. To execute a program the system copies it from the external device into the internal memory. The processor executes the program instructions. The fundamental unit of computer storage is a bit; it could be on (1) or off (0). A group of nine related bits makes a byte. Eight bits are used for data and the last one is used for parity. According to the rule of parity, number of bits that are on (1) in each byte should always be odd. So the parity bit is used to make the number of bits in a byte odd. If the parity is even, the system assumes that there had been a parity error (though rare) which might have caused due to hardware fault or electrical disturbance. The processor supports the following data sizes:

 Word: a 2-byte data item
 Doubleword: a 4-byte (32 bit) data item
 Quadword: an 8-byte (64 bit) data item
 Paragraph: a 16-byte (128 bit) area
 Kilobyte: 1024 bytes
 Megabyte: 1,048,576 bytes

The Binary Number System Every number system uses positional notation i.e., each position in which a digit is written has a different positional value. Each position is power of the base, which is 2 for binary number system, and these powers begin at 0 and increase by 1. The following table shows the positional values for an 8-bit binary number, where all bits are set on. Bit value (^) 1 1 1 1 1 1 1 1 Position value as a power of base 2^128 64 32 16 8 4 2 Bit number 7 6 5 4 3 2 1 0 The value of a binary number is based on the presence of 1 bits and their positional value. So the value of the given binary number is: 1 + 2 + 4 + 8 +16 + 32 + 64 + 128 = 255, which is same as 2^8 - 1. The Hexadecimal Number System Hexadecimal number system uses base 16. The digits range from 0 to 15. By convention, the letters A through F is used to represent the hexadecimal digits corresponding to decimal values 10 through 15.

TUTORIALS POINT

Main use of hexadecimal numbers in computing is for abbreviating lengthy binary representations. Basically hexadecimal number system represents a binary data by dividing each byte in half and expressing the value of each half-byte. The following table provides the decimal, binary and hexadecimal equivalents: Decimal number Binary representation Hexadecimal representation 0 0 0 1 1 1 2 10 2 3 11 3 4 100 4 5 101 5 6 110 6 7 111 7 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F To convert a binary number to its hexadecimal equivalent, break it into groups of 4 consecutive groups each, starting from the right, and write those groups over the corresponding digits of the hexadecimal number. Example : Binary number 1000 1100 1101 0001 is equivalent to hexadecimal - 8CD To convert a hexadecimal number to binary just write each hexadecimal digit into its 4-digit binary equivalent. Example : Hexadecimal number FAD8 is equivalent to binary - 1111 1010 1101 1000 Binary Arithmetic The following table illustrates four simple rules for binary addition: (i) (ii) (iii) (iv) 1 0 1 1 1 +0 +0 +1 + =0 =1 =10 = Rules (iii) and (iv) shows a carry of a 1-bit into the next left position. Example:

TUTORIALS POINT

x: memory address When the processor gets the numeric data from memory to register, it again reverses the bytes. There are two kinds of memory addresses:

 An absolute address - a direct reference of specific location.
 The segment address (or offset) - starting address of a memory segment with the offset value

TUTORIALS POINT

Assembly Environment Setup

Assembly^ language is dependent upon the instruction set and the architecture of the processor. In this

tutorial, we focus on Intel 32 processors like Pentium. To follow this tutorial, you will need:

 An IBM PC or any equivalent compatible computer
 A copy of Linux operating system
 A copy of NASM assembler program

There are many good assembler programs, like:

 Microsoft Assembler (MASM)
 Borland Turbo Assembler (TASM)
 The GNU assembler (GAS)

We will use the NASM assembler, as it is:

 Free. You can download it from various web sources.
 Well documented and you will get lots of information on net.
 Could be used on both Linux and Windows

Installing NASM If you select "Development Tools" while installed Linux, you may NASM installed along with the Linux operating system and you do not need to download and install it separately. For checking whether you already have NASM installed, take the following steps:

 Open a Linux terminal.
 Type whereis nasm and press ENTER.
 If it is already installed then a line like, nasm: /usr/bin/nasm appears. Otherwise, you will see just nasm: , then

you need to install NASM. To install NASM take the following steps: CHAPTER

TUTORIALS POINT

Assembly Basic Syntax

An^ assembly program can be divided into three^ sections:

 The data section
 The bss section
 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 main , which tells the kernel where the program execution begins. The syntax for declaring text section is: section .text global main main: 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: CHAPTER

TUTORIALS POINT

; This program displays a message on screen or, on the same line along with an instruction, like: add eax ,ebx ; adds ebx to eax Assembly Language Statements Assembly language programs consist of three types of statements:

 Executable instructions or instructions
 Assembler directives or pseudo-ops
 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 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. Macros are basically a text substitution mechanism. Syntax of Assembly Language Statements Assembly language statements are entered one statement per line. Each statement follows the following format: [label] mnemonic [operands] [;comment] The fields in the square brackets are optional. A basic instruction has two parts, the first one is the name of the instruction (or the mnemonic) which is to be executed, and the second are the operands or the parameters of the command. Following are some examples of typical assembly language statements: INC COUNT ; Increment the memory variable COUNT MOV TOTAL, 48 ; Transfer the value 48 in the ; memory variable TOTAL ADD AH, BH ; Add the content of the ; BH register into the AH register AND MASK1, 128 ; Perform AND operation on the ; variable MASK1 and 128 ADD MARKS, 10 ; Add 10 to the variable MARKS MOV AL, 10 ; Transfer the value 10 to the AL register The Hello World Program in Assembly The following assembly language code displays the string 'Hello World' on the screen: section .text global main ;must be declared for linker (ld) main: ;tells linker entry point mov edx,len ;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

TUTORIALS POINT

Assembly Memory Segments

We^ have already discussed three sections of an assembly program. These sections represent various

memory segments as well. Interestingly, if you replace the section keyword with segment, you will get the same result. Try the following code: segment .text ;code segment global main ;must be declared for linker main: ;tell linker entry point mov edx,len ;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 mov eax, 1 ;system call number (sys_exit) int 0x80 ;call kernel segment .data ;data segment msg db Hello, world!',0xa ;our dear string len equ $ - msg ;length of our dear string When the above code is compiled and executed, it produces following result: Hello, world! 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. CHAPTER

TUTORIALS POINT

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