









































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
concepts and methods of assemble language
Typology: Lecture notes
1 / 81
This page cannot be seen from the preview
Don't miss anything!
i
Simply Easy Learning by tutorialspoint.com tutorialspoint.com
Copyright & Disclaimer Notice
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
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:
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.
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:
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:
Assembly Environment Setup
tutorial, we focus on Intel 32 processors like Pentium. To follow this tutorial, you will need:
There are many good assembler programs, like:
We will use the NASM assembler, as it is:
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:
you need to install NASM. To install NASM take the following steps: CHAPTER
Assembly Basic Syntax
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 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
; 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:
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
Assembly Memory Segments
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:
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
codes. This is also a fixed area.