0% found this document useful (0 votes)
8 views41 pages

Copy of CSA_SA1 (REVIEWER)

Uploaded by

x 4 d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views41 pages

Copy of CSA_SA1 (REVIEWER)

Uploaded by

x 4 d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

MODULE 1:

ASSEMBLY
INTRODUCTION
subtopic 1:
INTRODUCTION TO ASSEMBLY LANGUAGE

WHAT IS ASSEMBLY LANGUAGE


• each personal computer has a microprocessor that
manages the computer's arithmetical, logical, and
control activities
• each family of processors has its own set of
instructions for handling various operations such as
getting input from keyboard, displaying information
on screen and performing various other jobs
o these set of instructions are called
“MACHINE LANGUAGE INSTRUCTIONS”
• a processor understands only machine language
instructions, which are strings of 1's and 0's
o however, machine language is too obscure
and complex for using in software
development
o so, the low-level assembly language is
designed for a specific family of processors
that represents various instructions in
symbolic code and a more understandable
form

ADVANTAGES OF ASSEMBLY LANGUAGE


• having an understanding of assembly language
makes one aware of:
o how programs interface with OS, processor,
and BIOS
o how data is represented in memory and other
external devices
o how the processor accesses and executes
instruction

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 1


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 1: introduction to assembly language
o how instructions access and process data
o how a program accesses external devices
• other advantages include:
o requires less memory and execution time
o allows hardware-specific complex jobs in an
easier way
o suitable for time-critical jobs
o 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
processor, memory, and registers
• REGISTERS
o registers are processor components that
hold data and address
• MEMORY
o to execute a program, the system copies it
from the external device into the internal
memory
• PROCESSOR
o the processor then executes the program
instructions
• the fundamental unit of computer storage is a bit; it
could be ON (1) or OFF (0) and a group of nine
related bits makes a byte, out of which eight bits are
used for data and the last one is used for parity
o PARITY BIT
▪ 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
been caused due to hardware fault or
electrical disturbance

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 2


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 1: introduction to assembly language

according to the rule of parity, the
number of bits that are ON (1) in each
byte should always be odd
• the processor supports the following data sizes:
o WORD: a 2-byte data item
o DOUBLEWORD: a 4-byte (32 bit) data item
o QUADWORD: an 8-byte (64 bit) data item
o PARAGRAPH: a 16-byte (128 bit) area
o KILOBYTE: 1024 bytes
o MEGABYTE: 1,048,576 bytes

BINARY NUMBER SYSTEM


• every number system uses positional notation, i.e.,
each position in which a digit is written has a different
positional value
o 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:

• the value of a binary number is based on the


presence of 1 bits and their positional value. So, the
value of a given binary number is:

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 28 − 1

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 3


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 1: introduction to assembly language
HEXADECIMAL NUMBER SYSTEM
• the hexadecimal number system uses base 16
• the digits in this system range from 0 to 15
o by convention, the letters A through F is used
to represent the hexadecimal digits
corresponding to decimal values 10 through
15
• hexadecimal numbers in computing is used for
abbreviating lengthy binary representations
o basically, hexadecimal number system
represents a binary data by dividing each
byte in half and expressing the value of each
half-byte
o the following table provides the decimal,
binary, and hexadecimal equivalents:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 4


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 1: introduction to assembly language
• 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
o e.g., binary number 1000 1100 1101 0001 is
equivalent to hexadecimal: 8CD1
• to convert a hexadecimal number to binary, just write
each hexadecimal digit into its 4-digit binary
equivalent
o e.g., hexadecimal number FAD8 is
equivalent to binary: 1111 1010 1101 1000

BINARY ARITHMETIC
• the following table illustrates four simple rules for
binary addition:

o rules (iii) and (iv) show a carry of a 1-bit into


the next left position
• e.g.,

• NOTE: a negative binary value is expressed in


TWO'S COMPLEMENT NOTATION

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 5


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 1: introduction to assembly language
o according to this rule, to convert a binary
number to its negative value is to reverse its
bit values and add 1
o e.g.,

• to subtract one value from another, convert the


number being subtracted to two's complement
format and add the numbers
o e.g., subtract 42 from 53

o note that overflow of the last 1 bit is lost

ADDRESSING DATA IN MEMORY


• the very process through which the processor
controls the execution of instructions is referred as
the FETCH-DECODE-EXECUTE cycle, or the
EXECUTION cycle
o it consists of three continuous steps:
▪ fetching the instruction from memory
▪ decoding or identifying the instruction
▪ executing the instruction

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 6


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 1: introduction to assembly language
• the processor may access one or more bytes of
memory at a time
o let us consider a hexadecimal number
0725H; this number will require two bytes of
memory
o the high-order byte or most significant byte is
07 and the low-order byte is 25
• the processor stores data in reverse-byte sequence,
i.e., a low-order byte is stored in a low memory
address and a high-order byte in high memory
address
o so, if the processor brings the value 0725H
from register to memory, it will transfer 25
first to the lower memory address and 07 to
the next memory address

• when the processor gets the numeric data from


memory to register, it again reverses the bytes
o there are two kinds of memory addresses:
▪ ABSOLUTE address – a direct
reference of specific location
▪ SEGMENT address (or OFFSET) –
starting address of a memory
segment with the offset value

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 7


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
subtopic 2:
ASSEMBLY ENVIRONMENT SETUP & SYNTAX

LOCAL ENVIRONMENT SETUP


• assembly language is dependent upon the
instruction set and the architecture of the processor
o in this tutorial, we focus on Intel-32
processors like Pentium
o 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
o there are many good assembler programs,
such as:
▪ Microsoft Assembler (MASM)
▪ Borland Turbo Assembler (TASM)
▪ the GNU assembler (GAS)
o we will use the NASM assembler, as it is:
▪ free; you can download it from
various web sources
▪ well-documented; you will get lots of
information on net
▪ could be used on both Linux and
Windows

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

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 8


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
• otherwise, you will see just
, and you will need to
install NASM
• to install NASM, take the following steps:
o check the netwide assembler (NASM)
website for the latest version
o download the Linux source archive
, where is the NASM
version number in the archive
o unpack the archive into a directory which
creates a subdirectory
o to and type ; this
shell script will find the best C compiler to use
and set up makefiles accordingly
o type to build the and
binaries
o type to install and
in and to install the
main pages
• this should install NASM on your system
o alternatively, you can use an RPM
distribution for the Fedora Linux
o this version is simpler to install, just double-
click the RPM file

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

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 9


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
• THE SECTION
o this section is used for declaring variables
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:

o or, on the same line along with an instruction:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 10


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
ASSEMBLY LANGUAGE STATEMENTS
• assembly language programs consist of three types
of statements:
o executable instructions or just instructions
o assembler directives or pseudo-ops
o macros
• EXECUTIVE INSTRUCTIONS
o or simply INSTRUCTIONS
o these tell the processor what to do, with each
instruction consisting of an OPERATION
CODE (opcode)
o each executable instruction generates one
machine language instruction
• ASSEMBLER DIRECTIVES
o or PSEUDO-OPS
o these tell the assembler about the various
aspects of the assembly process
o these are non-executable and do not
generate machine language instructions
• MACROS
o these 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:

o the fields in the square brackets are optional


o a basic INSTRUCTION has two parts:
▪ the NAME of the instruction
(mnemonic), which is to be executed

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 11


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
▪ and the second are the OPERANDS
or the parameters of the command
• e.g.,

THE HELLO WORLD PROGRAM IN ASSEMBLY


• the following assembly language code displays the
string on the screen:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 12


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
o when the above code is compiled and
executed, it produces the following result:

COMPILING & LINKING AN ASSEMBLY PROGRAM


• in NASM, make sure you have set the path of
and binaries in your environment variable
• now, take the following steps for compiling and
linking the above program:
o type the above code using a text editor and
save it as
o make sure that you are in the same directory
as where you saved
o to assemble the program, type:

o if there is any error, you will be prompted


about that at this stage; otherwise, an object
file of your program named will be
created
o to link the object file and create an
executable file named hello, type the
following:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 13


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
o execute the program by typing
• if you have done everything correctly, it will display
on the screen

ASSEMBLY MEMORY SEGMENTS


• we have already discussed the 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
o try the following code:

o when the above code is compiled and


executed, it produces the following result:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 14


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
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:
o DATA SEGMENT
▪ can be represented by section
and the section
▪ 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 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
o CODE SEGMENT
▪ it is represented by section
▪ this defines an area in memory that
stores the instruction codes
▪ this is also a fixed area
o STACK
▪ this segment contains data values
passed to functions and procedures
within the program

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 15


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
ASSEMBLY REGISTERS
• processor operations mostly involve processing
data; this data can be stored in memory and
accessed from thereon
• however, reading data from and storing data into
memory slows down the processor, as it involves
complicated processes of sending the data request
across the control bus and into the memory storage
unit and getting the data through the same channel
• to speed up the processor operations, the processor
includes some internal memory storage locations,
called registers
• REGISTERS
o the registers are internal memory storage
locations that store data elements for
processing without having to access the
memory
o a limited number of registers are built into the
processor chip

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

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 16


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
• these 32-bit registers can be used in three ways:
o as complete 32-bit data registers:

o lower halves of the 32-bit registers can be


used as four 16-bit data registers:
and
o lower and higher halves of the above-
mentioned four 16-bit registers can be used
as eight 8-bit data registers:
and

• some of these data registers have specific use in


arithmetical operations
o is the primary accumulator
▪ it is used in input/output and most
arithmetic instructions
▪ e.g., in multiplication operation, one
operand is stored in or or
register according to the size of the
operand
o is known as the base register
▪ as it could be used in indexed
addressing
o is known as the count register
▪ as the registers store the
loop count in iterative operations
o is known as the data register
▪ it is also used in input/output
operations

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 17


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
▪ it is also used with register along
with for multiply and divide
operations involving large values

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

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 18


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
▪ 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

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

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 19


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
• many instructions involve comparisons and
mathematical calculations and change the status of
the flags and some other conditional instructions test
the value of these status flags to take the control flow
to other location
• the common flag bits are:
o OVERFLOW flag ( )
▪ it indicates the overflow of a high-
order bit (leftmost bit) of data after a
signed arithmetic operation
o DIRECTION flag ( )
▪ it determines left or right direction for
moving or comparing string data
▪ when the value is , the string
operation takes left-to-right direction
and when the value is set to , the
string operation takes right-to-left
direction
o INTERRUPT flag ( )
▪ it determines whether the external
interrupts like keyboard entry, etc.,
are to be ignored or processed
▪ it disables the external interrupt when
the value is and enables interrupts
when set to
o TRAP flag ( )
▪ it allows setting the operation of the
processor in single-step mode
▪ the program we used sets the
trap flag, so we could step through
the execution one instruction at a
time
o SIGN flag ( )
▪ it shows the sign of the result of an
arithmetic operation

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 20


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
▪ this flag is set according to the sign of
a data item following the arithmetic
operation
▪ the sign is indicated by the high-order
of leftmost bit
▪ a positive result clears the value of
to and negative result sets it to
o ZERO flag ( )
▪ it indicates the result of an arithmetic
or comparison operation
▪ a nonzero result clears the zero flag
to , and a zero result sets it to
o AUXILIARY CARRY flag ( )
▪ it contains the carry from bit to bit
following an arithmetic operation;
used for specialized arithmetic
▪ the is set when a 1-byte arithmetic
operation causes a carry from bit
into bit
o PARITY flag ( )
▪ it indicates the total number of 1-bits
in the result obtained from an
arithmetic operation
▪ an even number of 1-bits clears the
parity flag to and an odd number of
1-bits sets the parity flag to
o CARRY flag ( )
▪ it contains the carry of or from a
high-order bit (leftmost) after an
arithmetic operation
▪ it also stores the contents of last bit of
a shift or rotate operation
• the following table indicates the position of flag bits
in the 16-bit flags register:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 21


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
SEGMENT REGISTERS
• SEGMENTS
o these are specific areas defined in a program
for containing data, code and stack
o there are three main segments:
▪ CODE segment – contains all the
instructions to be executed; a 16-bit
Code Segment register or register
stores the starting address of the
code segment
▪ DATA segment – contains data,
constants and work areas; a 16-bit
Data Segment register or register
stores the starting address of the
data segment
▪ STACK segment – contains data and
return addresses of procedures or
subroutines; it is implemented as a
'stack' data structure; the Stack
Segment register or register stores
the starting address of the stack
• apart from the DS, CS and SS registers, there are
other extra segment registers:
o ES (extra segment), FS and GS, which
provide additional segments for storing data
• in assembly programming, a program needs to
access the memory locations
o all memory locations within a segment are
relative to the starting address of the
segment
o a segment begins in an address evenly
divisible by 16 or hexadecimal 10
o so, the rightmost hex digit in all such memory
addresses is 0, which is not generally stored
in the segment registers
• the segment registers stores the starting addresses
of a segment

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 22


MODULE 1: ASSEMBLY INTRODUCTION
subtopic 2: assembly environment setup & syntax
o to get the exact location of data or instruction
within a segment, an offset value (or
displacement) is required
o to reference any memory location in a
segment, the processor combines the
segment address in the segment register
with the offset value of the location
• e.g., look at the following simple program to
understand the use of registers in assembly
programming
o this program displays 9 stars on the screen,
along with a simple message:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 23


MODULE 2:
SYSTEM CALLS &
ADDRESSING MODES
subtopic 1:
SYSTEM CALLS

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

LINUX SYSTEM CALLS


• you can make use of Linux system calls in your
assembly programs
o you need to take the following steps for using
Linux system calls in your program:
▪ put the system call number in the
register
▪ store the arguments to the system
call in the registers , , etc.
▪ call the relevant interrupt ( ).
▪ the result is usually returned in the
register
• there are six registers that store the arguments of
the system call used
o these are &
o these registers take the consecutive
arguments, starting with the register
o if there are more than six arguments, then
the memory location of the first argument is
stored in the register
• the following code snippet shows the use of the
system call :

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 24


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 1: system calls

• the following code snippet shows the use of the


system call :

• all the are listed in


, together with their
numbers (the value to put in EAX before you call
)
• the following table shows some of the system calls
used in this tutorial:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 25


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 1: system calls
• the following example reads a number from the
keyboard and displays it on the screen:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 26


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 1: system calls
o when the above code is compiled and
executed, it produces the following result:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 27


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 2: addressing modes
subtopic 2:
ADDRESSING MODES

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

THREE BASIC MODES OF ADDRESSING


• REGISTER ADDRESSING
o in this addressing mode, a register contains
the operand
o depending upon the instruction, the register
may be the first operand, the second
operand or both
o as processing data between registers does
not involve memory, it provides the fastest
processing of data
o e.g.,

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 28


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 2: addressing modes
• IMMEDIATE ADDRESSING
o an immediate operand has a constant value
or an expression
o when an instruction with two operands uses
immediate addressing, the first operand may
be a register or memory location, and the
second operand is an immediate constant
▪ the first operand defines the length of
the data
o e.g.,

• DIRECT MEMORY ADDRESSING


o when operands are specified in memory
addressing mode, direct access to main
memory, usually to the data segment, is
required
o this way of addressing results in slower
processing of data
o to locate the exact location of data in
memory, we need the segment start
address, which is typically found in the
register and an offset value
▪ this offset value is also called
EFFECTIVE ADDRESS
o in direct addressing mode, the offset value is
specified directly as part of the instruction,
usually indicated by the variable name

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 29


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 2: addressing modes
o the assembler calculates the offset value and
maintains a symbol table, which stores the
offset values of all the variables used in the
program
o in direct memory addressing, one of the
operands refers to a memory location and
the other operand references a register
o e.g.,

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

o the following operations access data from


the tables in the memory into registers:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 30


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 2: addressing modes
• INDIRECT MEMORY ADDRESSING
o this addressing mode utilizes the computer's
ability of Segment:Offset addressing
o generally, the base registers , (or ,
) and the index registers ( , ), coded
within square brackets for memory
references, are used for this purpose
o indirect addressing is generally used for
variables containing several elements like,
arrays
▪ starting address of the array is stored
in, say, the register
o e.g., the following code snippet shows how
to access different elements of the variable:

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:

• this instruction may have 1 of the following 5 forms:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 31


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 2: addressing modes

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

o it is not clear whether you want to move a


byte equivalent or word equivalent of the
number
▪ in such cases, it is wise to just use a
TYPE SPECIFIER
• the following table shows some of the common type
specifiers:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 32


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 2: addressing modes
o e.g., the following program illustrates some
of the concepts discussed above; it stores a
name in the section of the
memory, then changes its value to another
name programmatically and
displays both the names:

o when the above code is compiled and


executed, it produces the following result:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 33


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants
subtopic 3:
VARIABLES & CONSTANTS

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

ALLOCATING STORAGE SPACE FOR INITIALIZED DATA


• the syntax for storage allocation statement for
initialized data is:

o where, is the identifier for


each storage space
o the assembler associates an offset value for
each variable name defined in the data
segment
• there are five basic forms of the directive:

• e.g., the following are some examples of using


define directives:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 34


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants

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

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 35


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants
o when the above code is compiled and
executed, it produces the following result:

ALLOCATING STORAGE SPACE FOR UNINITIALIZED DATA


• the directives are used for reserving space
for uninitialized data
o these directives take a single operand that
specifies the number of units of space to be
reserved
o each directive has a related
directive
• there are five basic forms of the reserve directive:

MULTIPLE DEFINITIONS
• you can have multiple data definition statements in
a program
• e.g.,

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 36


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants
o the assembler then allocates contiguous
memory for multiple variable definitions

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:

• the directive is useful in defining arrays and


tables; the following program displays asterisks on
the screen:

o when the above code is compiled and


executed, it produces the following result:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 37


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants

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

• you can then use this constant value in your code:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 38


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants

• the operand of an statement can be an


expression:

o the above code segment would define


as
• the following example below illustrates the use of the
directive:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 39


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants

o when the above code is compiled and


executed, it produces the following result:

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:

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 40


SYSTEM CALLS &
MODULE 2: ADDRESSING MODES
subtopic 3: variables & constants
• later in the code, you can redefine it as:

• NOTE: this directive is case-sensitive

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:

o the above code replaces by


• like the directive, it also allows redefinition
and is case-sensitive

CS0003 (COMPUTER SYSTEMS & ARCHITECTURE) 41

You might also like