Copy of CSA_SA1 (REVIEWER)
Copy of CSA_SA1 (REVIEWER)
ASSEMBLY
INTRODUCTION
subtopic 1:
INTRODUCTION TO ASSEMBLY LANGUAGE
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 28 − 1
BINARY ARITHMETIC
• the following table illustrates four simple rules for
binary addition:
INSTALLING NASM
• if you select "Development Tools" while installing
Linux, you may get NASM installed along with the
Linux operating system and you do not need to
download and install it separately
o for checking whether you already have
NASM installed, take the following steps:
▪ open a Linux terminal
▪ type and press ENTER
▪ if it is already installed, then a line
like, appears
BASIC SYNTAX
• an assembly program can be divided into three
sections: the section, the section, and the
section
• THE SECTION
o this section is used for declaring initialized
data or constants
o this data does not change at runtime
o you can declare various constant values, file
names, or buffer size, etc., in this section
o the syntax for declaring a section is
• THE SECTION
o this section is used for keeping the actual
code, and must begin with the declaration
, which tells the kernel where
the program execution begins
o the syntax for declaring a text section is:
• COMMENTS
o assembly language comments begin with a
semicolon (;)
o it can appear on a line by itself, like:
PROCESSOR REGISTERS
• there are ten 32-bit and six 16-bit processor registers
in IA-32 architecture
o the registers are grouped into three
categories:
▪ GENERAL registers
▪ CONTROL registers
▪ SEGMENT registers
o the GENERAL registers are further divided
into the following groups:
▪ DATA registers
▪ POINTER registers
▪ INDEX registers
DATA REGISTERS
• four 32-bit data registers are used for arithmetic,
logical, and other operations
POINTER REGISTERS
• the pointer registers are 32-bit and
registers and corresponding 16-bit right portions
and
• there are three categories of pointer registers:
o INSTRUCTION pointer ( )
▪ the 16-bit register stores the offset
address of the next instruction to be
executed
▪ in association with the CS
register (as ), gives the
complete address of the current
instruction in the code segment
o STACK pointer ( )
▪ the 16-bit register provides the
offset value within the program stack
▪ , in association with the register
( ), refers to be current position
of data or address within the program
stack
▪ the 16-bit register mainly helps in
referencing the parameter variables
passed to a subroutine
▪ the address in register is
combined with the offset in to get
the location of the parameter
▪ can also be combined with and
as base register for special
addressing
o BASE pointer ( )
▪ the 16-bit register mainly helps in
referencing the parameter variables
passed to a subroutine
INDEX REGISTERS
• the 32-bit index registers, and , and their 16-
bit rightmost portions
• and , are used for indexed addressing and
sometimes used in addition and subtraction
• there are two sets of index pointers:
o SOURCE index ( )
▪ it is used as source index for string
operations
o DESTINATION index ( )
▪ it is used as destination index for
string operations
CONTROL REGISTERS
• the 32-bit instruction pointer register and the 32-bit
flags register combined are considered as the
control registers
SYSTEM CALLS
• these are APIs for the interface between the user
space and the kernel space
• we have already used the system calls,
and , for writing into the screen and exiting
from the program, respectively
ADDRESSING MODES
• most assembly language instructions require
operands to be processed
• an OPERAND ADDRESS provides the location,
where the data to be processed is stored
o some instructions do not require an operand,
whereas some other instructions may require
one, two, or three operands
• when an instruction requires two operands, the first
operand is generally the DESTINATION, which
contains data in a register or memory location and
the second operand is the source
o while SOURCE contains either the data to be
delivered (immediate addressing) or the
address (in register or memory) of the data
o generally, the source data remains unaltered
after the operation
• DIRECT-OFFSET ADDRESSING
o this addressing mode uses the arithmetic
operators to modify an address
o e.g., look at the following definitions that
define tables of data:
THE INSTRUCTION
• we have already used the MOV instruction that is
used for moving data from one storage space to
another
• the MOV instruction takes two operands
• SYNTAX:
• NOTE:
o both the operands in the operation
should be of the same size
o the value of the operand remains
unchanged
• this instruction causes ambiguity at times
o e.g., look at the statements below:
DIRECTIVES
• NASM provides various directives for
reserving storage space for variables
• the assembler directive is used for allocation
of storage space
o it can be used to reserve as well as initialize
one or more bytes
• NOTE:
o each byte of character is stored as its ASCII
value in hexadecimal
o each decimal value is automatically
converted to its 16-bit binary equivalent and
stored as a hexadecimal number
o processor uses the little-endian byte ordering
o negative numbers are converted to its 2's
complement representation
o short and long floating-point numbers are
represented using 32 or 64 bits, respectively
• e.g., the following program shows the use of
directive:
MULTIPLE DEFINITIONS
• you can have multiple data definition statements in
a program
• e.g.,
MULTIPLE INITIALIZATIONS
• the directive allows multiple initializations to
the same value
• e.g., an array named marks of size can be defined
and initialized to zero using the following statement:
CONSTANTS
• there are several directives provided by NASM that
define constants; we have already used the
directive in previous modules
o we will particularly discuss three directives:
▪
▪
▪
THE DIRECTIVE
• this directive is used for defining constants
• the syntax of the directive is as follows:
• e.g.,
THE DIRECTIVE
• this directive can be used to define numeric
constants like the directive
• this directive allows redefinition
• e.g., you may define the constant as:
THE DIRECTIVE
• this directive allows defining both numeric and string
constants
• this directive is similar to the in C
• e.g., you may define the constant as: