0% found this document useful (0 votes)
37 views93 pages

Microprocessor Lecture With Assembly Language

The document discusses microprocessors and microcontrollers. It describes that a microprocessor is an electronic component containing millions of transistors, resistors, and diodes that act as the central processing unit on a computer chip. The document provides a history of microprocessors including early 4-bit microprocessors from Intel and the development of 8-bit microprocessors. It summarizes functions of common microprocessor components like the ALU and register array.

Uploaded by

R-jay Macasinag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views93 pages

Microprocessor Lecture With Assembly Language

The document discusses microprocessors and microcontrollers. It describes that a microprocessor is an electronic component containing millions of transistors, resistors, and diodes that act as the central processing unit on a computer chip. The document provides a history of microprocessors including early 4-bit microprocessors from Intel and the development of 8-bit microprocessors. It summarizes functions of common microprocessor components like the ALU and register array.

Uploaded by

R-jay Macasinag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 93

MICROPROCESSOR AND

MICROCONTROLLER
MICROPROCESSOR
 A microprocessor is an electronic component that is
used by a computer to do its work.

 It is a central processing unit on a single integrated


circuit chip containing millions of very small
components including
 transistors
 resistors
 and diodes that work together.
BASIC COMPONENTS OF MICROPROCESSOR
 TRANSISTORS A transistor can act as a switch or gate for electronic
signals, opening and closing an electronic gate
many times per second.

 RESISTORS A resistor is a passive two-terminal electrical


component that implements electrical resistance
as a circuit element. In electronic circuits, resistors
are used to reduce current flow, adjust signal
levels, to divide voltages, bias active elements, and
terminate transmission lines, among other uses.
 DIODES
Diodes can be used as rectifiers, signal limiters,
voltage regulators, switches, signal modulators,
signal mixers, signal demodulators, and oscillators.
The fundamental property of a diode is its tendency
to conduct electric current in only one direction.
MICROPROCESSOR HISTORY
 The computer you are using to read this
module uses a microprocessor to do its work.
 The microprocessor is the heart of any normal
computer.
 The microprocessor you are using might be a
Pentium, AMD, a PowerPC, a Sun-SPARC or
any of the many other brands and types of
microprocessors.
MICROPROCESSOR
 INTEL

 AMD

 PowerPC

 Sun-SPARC
MICROPROCESSOR
 A microprocessor - also known as a CPU or
central processing unit. It is a complete
computation engine that is fabricated on a
single chip.
MICROPROCESSOR
 Intel coined the term ‘MICROPROCESSOR’ and
in 1971 released the first 4-bit microprocessor
as the 4004 having 2300 transistors, 640 bytes
of memory addressing capacity and a 108 kHz
clock speed.
4 BIT MICROPROCESSOR
 In the 4-bit microprocessor or computer architecture will have a
data path width or a highest operand width of 4 bits or a nibble.
And also these architectures or microprocessors typically will
have a matching register file with registers width of 4 bits and 4-
8-bit wide addresses.
 Most of the first microprocessors during the early 1970s had 4-
bit word sizes. Both the Intel 4004, the first commercial
microprocessor, and the 4040 had a 4-bit word length, but had
8-bit instructions. Some of the first microcontrollers such as the
TMS1000 made by Texas Instruments and NEC's μPD751 also
had 4-bit words. 4-bit word were proven to be very limiting and
by 1974 there was a shift to larger architectures such as 8 and
12-bit architectures.
MICROPROCESSOR
 A computer's processor clock speed determines how
quickly the central processing unit (CPU) can retrieve and
interpret instructions. This helps your computer complete
more tasks by getting them done faster. Clock speeds are
measured in gigahertz (GHz), with a higher number
equating to higher clock speed.
 The quartz crystal vibrates or ticks an exact 60 seconds per
minute when electricity is applied to it.
MICROPROCESSOR
 The 4004 was not very powerful – all it could do was add
and subtract, and it could only do that 4 bits at a time. The
4004 powered one of the first portable electronic
calculators.
 4-bit computing
In computer architecture, 4-bit integers,
memory addresses, or other data units are those that
are 4 bits wide. Also, 4-bit CPU and ALU architectures
are those that are based on registers, address buses, or
data buses of that size. A group of four bits is also called
a nibble and has 24 = 16 possible values.
MICROPROCESSOR
 Gordon Moore, cofounder of Intel Corporation predicted
that “The number of transistors per integrated circuit would
double every 18 months.” This came to known as ‘MOORE’s
LAW’

 Just 25 years since the invention of the 4004, we have


processors that are designed with 15 million transistors,
that can address 1 TB (1 X 10 ^ 12) of memory, and can
operate at 400 MHz to 1.5 GHz frequency.
MICROPROCESSOR
 The Intel 4004 was quickly replaced by the 8-bit
microprocessor (Intel 8008). In the mid 1970’s, the Intel
8080 was widely used in control applications & small
computers also were designed using 8080 as CPU – also
known as ‘Microcomputers’.

 Most microcomputers are now built with 32 & 64 bit


microprocessor. The 8 – bit microprocessor are being used
as programmable logic devices in control applications &
more powerful micro-processors are being used for
mathematical computing and in data processing.
INTEL 4004
 The Intel 4004 was a 4-bit central processing unit (CPU)
released by the Intel Corporation in 1971. It was the first
complete CPU on one chip, and also the first commercially
available microprocessor.
 The 4001 was a ROM (read-only memory) with four lines of
output.
 The 4002 was a RAM (random access memory) with four
lines of input/output.
 The 4003 was a static shift register to be used for expanding
the I/O lines, for example, for keyboard scanning or for
controlling a printer.
 The 4004 included control functions for memory and I/O,
which are not normally handled by the microprocessor.
INTEL 8080
 The first microprocessor to make it into a home computer
was the Intel 8080. Introduced in 1974, it was a complete 8-
bit computer on one chip.

 It was an extended and enhanced variant of the earlier


8008 design, although without binary compatibility. The
initial specified clock frequency limit was 2 MHz and with
common instructions having execution times of 4,7,10 or 11
cycles this meant a few hundredth thousand instructions
per second. The 8080 has sometimes been labelled ‘the
first truly usable microprocessor”
INTEL 8085
 The Intel 8085 is an 8-bit microprocessor introduced by
Intel in 1977. It was binary-compatible with the more-
famous Intel 8080 but required less supporting hardware,
thus allowing simpler and less expensive microcomputer
systems to be built.
 In many engineering schools the 8085 processor is used in
introductory microprocessor courses.
INTEL PENTIUM
 Pentium is a registered trademark that is included in the
brand name of many of Intel’s x86-compatible
microprocessors. The name Pentium was derived from the
Greek pente, meaning five.
 Intel’s fifth-generation micro architecture, the P5, was first
released under the Pentium brand on March 22, 1993. The
Pentium 4 brand refers to Intel’s line of single-core desktop
and laptop central processing units (CPU) introduced on
November 20, 2000.
 The initial 32-bit x86 instruction set of the Pentium 4
microprocessors was extended by the 64-bit x86-64 set &
were clocked from 1.3 GHz to 2 GHz.
Latest Microprocessor by Intel
Latest Microprocessor by Intel
 On June 3, 2007, Intel released the desktop Pentium
Dual-Core branded processors known as the
Pentium E2140 and E2160. An E2180 model was
released later in September 2007.
 Core 2 is a brand encompassing a range of Intel’s
consumer 64-bit x86-64 single-, dual-, and quad-
core microprocessors based on the Core micro
architecture. The single- and dua-core models are
single-die, whereas the quad-core models comprise
two dies, each containing two cores, packaged in a
multi-chip module.
Latest Microprocessor by Intel
 The Core 2 brand was introduced on July 27,
2006.
 Intel Pentium D CPU comprises of two dies,
each containing a single core residing next to
each other on a multi-chip module package.
 The Pentium D consumes a relatively high
power. It runs very hot and is not a good
OverClocker. Intel Dual Core is the first dual-
core CPU from Intel.
Latest Microprocessor by Intel
 The Dual Core makes use of two cores on a single
die. i.e. it has two chips in one package It solves
the problem of overheating of Intel Pentium D
without compromising on the performance.
 Intel Core 2 Duo processors are the next
generation processors from Intel. It is developed
with a new Architecture called Core. The new
architecture of Intel core2 duo makes it a lot
better processor that runs cool. Most importantly
the Intel core2 duo is super over clocker.
Latest Microprocessor by Intel
 Recently Intel has launched the latest
Microprocessor named IntelCore2 Quad
Processor Q6600 (8M Cache, 2.4 GHz, 1066
MHz FSB).
Microprocessor
 The following diagram shows an extremely simple microprocessor capable
of doing things:
Microprocessor
 The Microprocessor is a clock-driven, semiconductor device
consisting of electronic logic circuits manufactured by using
either a LSI (Large Scale integration) or VLSI (Very Large scale
Integration).
 Large-scale integration is a situation in which a chip has tens
of thousands of transistors on it. A microprocessor is a clock-
driven semiconductor device consisting of electronic logic
circuits manufactured by using a large-scale
integration technique.
 Microprocessor can be divided into 3 segments for the sake of
the clarity:
 ALU
 Register Array
 Control Unit
Microprocessor
 ALU (Arithmetic/Logic Unit):
This unit performs such arithmetic operations as Addition,
Subtraction and such logic operations AND, OR & exclusive OR.
 Register Array:
This area of the microprocessor consists of various registers
identified by letters such as B,C,D,E,H,L and Accumulator.
Registers are temporary storage locations inside the CPU that
hold data and addresses. The register file is the component
that contains all the general purpose registers of
the microprocessor.
 Control Unit:
It controls the flow of data between the microprocessor and
memory & peripherals.
Microprocessor
 Microprocessor also consists:
 An Address Bus (that may be 8,16 or 32 bits wide)
that sends an address to memory.
 A Data Bus (that my be 8, 16, or 32 bits wide) that can
send data to memory or receive data from memory.
An RD (read) and WR (write) line to tell the memory
whether it wants to set or get the addressed location.
 A clock line that lets a clock pulse sequence the
processor
 A reset line that resets the program counter to zero
(or whatever)
Microprocessor
 The system bus is a communication Path
between the Microprocessor and Peripherals;
it is nothing but a group of wires to carry bits.

 I/O devices are also know as peripherals. i.e,


keyboard, switches, and Analog to digital
converter are input devices. Such as LEDs,
printer, X-Y plotter, Digital to analog converter,
video screen are various Output devices.
Microprocessor
 Memory stores such as Binary information as
instructions and data, and provides that
information to the microprocessor whenever
necessary.
 It has mainly two parts:
1 ROM (read only memory)
2. RAM (random access memory)
 Programs stored in ROM can only be read, they
can’t be altered.
 Whereas in RAM you can easily read and altered
the stored information.
Microprocessor
Microprocessor and Microcomputers
 A microprocessor is a large-scale integrated
circuit made up of tens and even hundreds of
thousands of transistors, resistors, switches,
and other circuit elements miniaturized to fit
on a single silicon chip.
 Microcomputers are finding an ever-increasing
use in controlling analytical instruments and in
processing, storing, and displaying the data
derived from them.
Microprocessor and Microcomputers
 Automation leads to more rapid data
acquisition, which shortens the time required
for analysis or increase precision by providing
time for additional replicate measurements to
be made.
 How Does the microprocessor work?
The microprocessor works according to the
pattern: “READ, INTERPRET & PERFORM” or
you can say “FETCH, DECODE, & EXECUTE”.
Microprocessor and Microcomputers
 The instructions are stored sequentially in memory, then
microprocessor fetches the data/instruction from its memory sheet,
decodes it, and executes the instruction. This process is continued
until the microprocessor comes across an instruction to STOP.
During the entire process microprocessor uses the system bus to
fetch the binary instructions and data from the memory. It uses
registers from the register section to store data temporarily, and it
performs the computing function in the ALU section. Finally it sends
out the result in binary, using the same bus-lines to output ports.

 References:
http://en.wikipedia.ort/wiki/Main_Page
http://www.google.co.in/imgphp?hl=en&ta
INTRODUCTION TO ASSEMBLY PROGRAMMING
 Assembly language is a low-level programming
language for a computer or other
programmable device specific to a particular
computer architecture in contrast to most
high-level programming languages, which are
generally portable across multiple systems.
Assembly language is converted into
executable machine code by a utility program
referred to as an assembler like NASM, MASM,
etc.
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. 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. However,
machine language is too obscure and complex for
using in software development.
What is Assembly Language?
 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 −
How programs interface with OS, processor, and
BIOS;
How data is represented in memory and other
external devices;
How the processor accesses and executes
instruction;
How instructions access and process data;
How a program accesses external devices.
Advantages of Assembly Language
 Other advantages of using assembly language
are −
It requires less memory and execution time;
It allows hardware-specific complex jobs in an
easier way;
It is suitable for time-critical jobs;
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
processor, memory, and registers. 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) and a group of
8 related bits makes a byte on most of the
modern computers.
Basic Features of PC Hardware
 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 been
caused due to hardware fault or electrical
disturbance.
 A parity bit, or check bit, is a bit added to a
string of binary code. Parity bits are used as
the simplest form of error detecting code.
Basic Features of PC Hardware
 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
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.
Binary Number System

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 - Note: Last slide in lecture March 29
Hexadecimal Number
 Hexadecimal number system uses base 16. The digits
in this system 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.
 Hexadecimal numbers in computing is used 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
Hexadecimal Number
Hexadecimal Number
 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 - 8CD1
 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
Binary Arithmetic
Binary Arithmetic
Addressing Data in Memory
 The process through which the processor controls the
execution of instructions is referred as the fetch-
decode-execute cycle or the execution cycle. It consists
of three continuous steps −
Fetching the instruction from memory
Decoding or identifying the instruction
Executing the instruction
 The processor may access one or more bytes of
memory at a time. Let us consider a hexadecimal
number 0725H. This number will require two bytes of
memory. The high-order byte or most significant byte
is 07 and the low-order byte is 25.
Addressing Data in Memory
 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. 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.
Addressing Data in Memory
 When the processor gets the numeric data
from memory to register, it again reverses the
bytes. 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.
Local 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
Local Environment Setup
There are many good assembler programs, such as −
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.
ASSEMBLY LANGUAGE
 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.
ASSEMBLY LANGUAGE
 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.
ASSEMBLY LANGUAGE
 To install NASM, take the following steps −
 Check The netwide assembler (NASM) website for the latest version.
 Download the Linux source archive nasm-X.XX.ta.gz, where X.XX is
the NASM version number in the archive.
 Unpack the archive into a directory which creates a
subdirectory nasm-X. XX.
 cd to nasm-X.XX and type ./configure. This shell script will find the
best C compiler to use and set up Makefiles accordingly.
 Type make to build the nasm and ndisasm binaries.
 Type make install to install nasm and ndisasm in /usr/local/bin and
to install the man pages.
 This should install NASM on your system. Alternatively, you can use
an RPM distribution for the Fedora Linux. This version is simpler to
install, just double-click the RPM file.
ASSEMBLY LANGUAGE
 An assembly program can be divided into three
sections −
The data section,
The bss section, and
The text 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
ASSEMBLY LANGUAGE
 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
_start, which tells the kernel where the program
execution begins.
The syntax for declaring text section is −
section.text
global _start_start:
ASSEMBLY LANGUAGE
 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 −
; 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
 Assembly Language Statements
Assembly language programs consist of three
types of statements −
Executable instructions or instructions,
Assembler directives or pseudo-ops, and
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.
ASSEMBLY LANGUAGE
 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.
ASSEMBLY LANGUAGE
 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.
Mnemonics are predefined assembly-language names for
machine instructions, pseudo-ops, directives, and data
allocation statements. Mnemonics are not case-sensitive.
ASSEMBLY LANGUAGE
 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
 MOV AL, 10 ; Transfer the value 10 to the AL register
ASSEMBLY LANGUAGE - 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.
 The registers store data elements for processing without
having to access the memory. A limited number of registers
are built into the processor chip.
ASSEMBLY LANGUAGE - REGISTERS
 Processor Registers
There are ten 32-bit and six 16-bit processor registers in IA-32
(Intel Archintecture – 32 bit) architecture. The registers are
grouped into three categories −
 General registers,
 Control registers, and
 Segment registers.
 The general registers are further divided into the following
groups −
 Data registers,
 Pointer registers, and
 Index registers.
ASSEMBLY LANGUAGE - REGISTERS
 Data Registers
Four 32-bit data registers are used for arithmetic, logical, and
other operations. These 32-bit registers can be used in three
ways −
 As complete 32-bit data registers: EAX, EBX, ECX, EDX.

 Lower halves of the 32-bit registers can be used as four 16-bit


data registers: AX, BX, CX and DX.

 Lower and higher halves of the above-mentioned four 16-bit


registers can be used as eight 8-bit data registers: AH, AL, BH,
BL, CH, CL, DH, and DL.
ASSEMBLY LANGUAGE - REGISTERS

Some of these data registers have specific use in arithmetical operations.


REGISTERS
 AX is the primary accumulator; it is used in input/output
and most arithmetic instructions. For example, in
multiplication operation, one operand is stored in EAX or
AX or AL register according to the size of the operand.
 BX is known as the base register, as it could be used in
indexed addressing.
 CX is known as the count register, as the ECX, CX registers
store the loop count in iterative operations.
 DX is known as the data register. It is also used in
input/output operations. It is also used with AX register
along with DX for multiply and divide operations involving
large values.
REGISTERS
 Pointer Registers
The pointer registers are 32-bit EIP, ESP, and EBP registers and corresponding
16-bit right portions IP, SP, and BP. There are three categories of pointer
registers −
 Instruction Pointer (IP) − The 16-bit IP register stores the offset address of
the next instruction to be executed. IP in association with the CS register
(as CS:IP) gives the complete address of the current instruction in the code
segment.
 Stack Pointer (SP) − The 16-bit SP register provides the offset value within
the program stack. SP in association with the SS register (SS:SP) refers to
be current position of data or address within the program stack.
 Base Pointer (BP) − The 16-bit BP register mainly helps in referencing the
parameter variables passed to a subroutine. The address in SS register is
combined with the offset in BP to get the location of the parameter. BP can
also be combined with DI and SI as base register for special addressing.
REGISTERS
 Index Registers
The 32-bit index registers, ESI and EDI, and
their 16-bit rightmost portions. SI and DI, are
used for indexed addressing and sometimes
used in addition and subtraction. There are two
sets of index pointers −
Source Index (SI) − It is used as source index for
string operations.
Destination Index (DI) − It is used as
destination index for string operations.
REGISTERS
 Control Registers
The 32-bit instruction pointer register and the
32-bit flags register combined are considered
as the control registers.
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.
REGISTERS
 Segment Registers
 Segments are specific areas defined in a program for containing
data, code and stack. There are three main segments −
 Code Segment − It contains all the instructions to be executed. A
16-bit Code Segment register or CS register stores the starting
address of the code segment.
 Data Segment − It contains data, constants and work areas. A 16-
bit Data Segment register or DS register stores the starting
address of the data segment.
 Stack Segment − It contains data and return addresses of
procedures or subroutines. It is implemented as a 'stack' data
structure. The Stack Segment register or SS register stores the
starting address of the stack.
ASSEMBLY LANGUAGE
 The Hello World Program in Assembly
The following assembly language code display the string “Hello
World!” on the screen.
section .text ;(The text section is used for keeping the
actual code. This section must begin with the declaration global
_start, which tells the kernel where the program execution begins.)
global _start ; must be declared for linker (ld)
_start: ; 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
mov eax, 1 ; system call number (sys_exit)
int 0x80 ;call kernel
ASSEMBLY LANGUAGE
 The Hello World Program in Assembly
The following assembly language code display the string “Hello
World” on the screen.
Section .data
msg db ‘Hello, world!’ , 0xa ; our dear string
len equ $ - msg ; length of our dear string
ASSEMBLY LANGUAGE
 In machine code, there are no functions. At least, the
processor knows nothing about functions. The programmer
can structure his code as he likes.
 _start: is something called a symbol which is just a name for a
location in your program. Symbols are used to refer to
locations whose address you don't know yet. They are
resolved during linking. The symbol _start: is used as the entry
point which is where the operating system jumps to start your
program. Unless you specify the entry point by some other
way, every program must contain _start. The other symbols
your program uses are msg, which is resolved by the linker to
the address where the string Hello, world! resides
and len which is the length of msg.
ASSEMBLY LANGUAGE
 The rest of the program does the following things:
1. Set up the registers for the system call write(1, msg,
len). write has system call number 4 which is stored in eax to
let the operating system know you want system call 4. This
system call writes data to a file. The file descriptor number
supplied is 1 which stands for standard output.
2. Perform a system call using int $0x80. This instruction
interrupts your program, the operating system picks this up
and performs the function whose number is stored in eax. It's
like a function call that calls into the OS kernel. The calling
convention is different from other functions, with args
passed in registers.
ASSEMBLY LANGUAGE
 The rest of the program does the following things:
3.Set up the registers for the system call _exit(?). Its system
call number is 1 which goes into eax. Sadly, the code forgets
to set the argument for _exit, which should be 0 to indicate
success. Instead, whatever was in ebx before is used instead,
which seems to be 1.
4.Perform a system call using int $0x80. Because _exit ends
the program, it does not return. Your program ends here.
 The directive db tells the assembler to place the following
data into the program where we currently are. This places the
string Hello, world! followed by a newline into the program so
we can tell the write system call to write that string.
ASSEMBLY LANGUAGE
 In assembly language, we use "db" (data byte) to allocate some
space, and fill it with a string.
 The line len equ $ - msg tells the assembler that
len is the difference between $ (where we currently are) and msg.
This is defined so we can pass to write how long the text we want to
print is.
 $ points to the byte after the last character of the string variable msg.
Therefore, $-msg gives the length of the string.
 The equ iWe can also write
msg db 'Hello, world!',0xa ;our dear string
len equ 13 ;length of our dear stringnstruction
assigns absolute or relocatable values to symbols. Use it to: Assign
single absolute values to symbols.
 Everything after a semicolon (;) in the program is a comment ignored
by the assembler.
ASSEMBLY LANGUAGE
 In assembly language, we use "db" (data byte) to allocate
some space, and fill it with a string.
 A kernel is the central part of an operating system. It manages
the operations of the computer and the hardware, most
notably memory and CPU time.
 int 0x80 is the assembly language instruction that is used to
invoke system calls in Linux on x86 (i.e., Intel-compatible)
processors
ASSEMBLY LANGUAGE (linker)
ASSEMBLY LANGUAGE
section .text
global _start ;must be declared for linker
_start: ;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 edx,9 ;message length
mov ecx,s2 ;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
ASSEMBLY LANGUAGE
section .data
msg db 'Displaying 9 stars',0xa ;a message
len equ $ - msg ;length of message
; $ means the current position, so $ -
;variable means current address minus
;start address of variable, i.e. the
length.
s2 times 9 db '*‘ ; nine data bytes are allocated from the
;current
;segment each with a character
;value of '*', the address given
;the name
;s2.
ASSEMBLY SYSTEM CALLS
System calls are APIs (Application Programming Interface) for
the interface between user space and kernel space. We have
already used the system calls sys_write and sys_exit 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. You need to take the following steps for using Linux
system calls in your program:
 Put the system call number in the EAX register.
 Store the arguments to the system call in the registers EBX, ECX, etc.
 Call the relevant interrupt (80h)
 The result is usually returned in the EAX register
ASSEMBLY SYSTEM CALLS
There are six registers that stores the arguments of the system
call used.
These are the EBX, ECX, EDX, ESI, EDI, and EBP. These registers
take the consecutive arguments, starting with the EBX
register. If there are more than six arguments then the
memory location of the first argument is stored in the EBX
register.
The following code snippet shows the use of the system call
sys_exit:
 mov eax,1 ; system call number (sys_exit)
 int 0x80 ; call kernel (the main component of a
; Linux operating system)
ASSEMBLY SYSTEM CALLS
The following code snippet shows the use of the system call
sys_write:

mov edx,4 ; 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
All the syscalls are listed in /usr/include/asm/unistd.h, together
with their numbers (the value to put in EAX before you call int
80h).
ASSEMBLY SYSTEM CALLS
The following table shows some of the system calls used in this
tutorial:
ASSEMBLY SYSTEM CALLS
The following example reads a number from the keyboard and
displays it on the screen:
section .data ;Data segment
userMsg db 'Please enter a number: ' ;Ask the user to enter a number
lenUserMsg equ $-userMsg ;The length of the message
dispMsg db 'You have entered: '
lenDispMsg equ $-dispMsg
Note: In assembly language, we use "db" (data byte) to allocate
some space, and fill it with a string.
section .bss ;Uninitialized data
num resb 5 ; RESB 5 allocates 5 byte.
ASSEMBLY SYSTEM CALLS

section .text ;Code Segment


global main
main:
;User prompt
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
ASSEMBLY SYSTEM CALLS
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h

;Output the message 'The entered number is: '


mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 80h
ASSEMBLY SYSTEM CALLS
;Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 5
int 80h

; Exit code
mov eax, 1
mov ebx, 0
int 80h
MOV Instruction
 The MOV 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:
Syntax of the MOV instruction is:
MOV destination, source
The MOV instruction may have one of the following five forms:
MOV register, register
MOV register, immediate
MOV memory, immediate
MOV register, memory
MOV memory, register
MOV Instruction
 The MOV 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:
Syntax of the MOV instruction is:
MOV destination, source
The MOV instruction may have one of the following five forms:
MOV register, register
MOV register, immediate
MOV memory, immediate
MOV register, memory
MOV memory, register
Note: An immediate operand is a constant value or the result of a
constant expression.
Advantage of Assembly Language
 Advantages Of Assembly Language

 Programs written in machine language are replaceable by


mnemonics which are easier to remember.
 Memory Efficient.
 It is not required to keep track of memory locations.
 Faster in speed.
 Easy to make insertions and deletions.
 Hardware Oriented.
 Requires fewer instructions to accomplish the same result.
Disadvantage of Assembly Language
 Disadvantages Of Assembly Language

 Long programs written in such languages cannot be executed


on small sized computers.
 It takes lot of time to code or write the program, as it is more
complex in nature.
 Difficult to remember the syntax.
 Lack of portability of program between computers of different
makes.
 No SDKs (System Development Kit).

You might also like