You are on page 1of 46

Computer Organization and

Assembly Language
Muhammad Usman

Federal Urdu University of Arts, Science & Technology, Islamabad


Assembly Language
• Low-level programming language
• Implements a symbolic representation of
the machine code
• 1 to 1 correspondence with machine code
instructions
• Specific for each computer architecture
• Assembler is used to convert Assembly
code
FUUAST, Islamabad 2
Continue…
• Assembly Language for Intel Architecture
32 (IA-32)
• Microsoft Macro Assembler (MASM)

FUUAST, Islamabad 3
Assembler & Linker
• An assembler is a utility program that
converts source code programs from
assembly language into machine language
• A linker is a utility program that combines
individual files created by an assembler
into a single executable program

FUUAST, Islamabad 4
Is Assembly Portable?
• A language whose source programs can
be compiled and run on a wide variety of
computer systems is said to be portable.
C++ programs, for example.
• Assembly language is NOT portable
because it is designed for a specific
processor family

FUUAST, Islamabad 5
Why Assembly?
• Compliers do most of the work in
generating assembly code
• Chances are, you will never write a
program in assembly language
• Writing concurrent program using thread
package, it is important to know memory
region to hold variable.
– Information of such type is only visible in
assembly level code

FUUAST, Islamabad 6
Continue…
• Virus and worms attacks exploit the
weakness of the system
– To understand how to locate weakness in the
system
– How to guard them
– Requires knowledge of machine level
representation

FUUAST, Islamabad 7
Continue…
• Assembly language is ideal for writing
embedded programs because of its
economical use of memory
• Device drivers are programs that translate
general operating system commands into
specific references to hardware details

FUUAST, Islamabad 8
What we do in Assembly
• How programs interface with OS,
processor and BIOS
• How data is represented in memory and
other external devices
• How the processor accesses and executes
instructions
• How instructions access and process data
• How a program accesses external devices
FUUAST, Islamabad 9
Moore’s Law
• Gordon Moore, a founder of Intel
• 1965, 64 transistor on a single chip
• Moore predicted that the number of
transistors per chip would double every
two years for the next 10 years
• It was optimistic prediction
• More than 45 years, double transistor
counts on average every 18 months
FUUAST, Islamabad 10
x86 Complexity

FUUAST, Islamabad 11
Historical Perspective
Processor Year Transistors Technology
16-bit, 655,360
8086 1978 29K
byte address
More addressing
80286 1982 135K
modes
32-bit, Supported
i386 1985 275K
Unix
Improved, FP unit
i486 1989 1.2M
on processor
FUUAST, Islamabad 12
Continue…
Processor Year Transistors Technology
Improved
Pentium 1993 3.1M
instructions set
Added class of
P Pro 1995 5.5M
‘conditional move’
P-II 1997 7M Continuation
P-III 1999 8.2M L2 cache, SSE
P-IV 2000 42M SSE2, NetBurst
FUUAST, Islamabad 13
Continue…
Processor Year Transistors Technology
64-bit,
P-IV E 2004 125M
Hyperthreading
Multicore, no
Core 2 2006 291M support for
hyperthreading
Hyperthreading,
Core i7 2008 781M two programs to
each core
FUUAST, Islamabad 14
Student’s Machine Language
• For a 4-bit representation
• The first 2 bits represent operation
– 00 eat
– 01 study
– 10 play
– 11 watch TV
• The last 2 bit represent what to operate on

FUUAST, Islamabad 15
Continue…
• The last 2 bit represent the eating (00)
– 00 rice
– 01 burger
– 10 bread
– 11 pizza
• The last 2 bit represent the reading (01)
– 00 COA
– 01 DS
– 10 OS
– 11 VP
FUUAST, Islamabad 16
Continue…
• 00AB instructions format
– Four different eat instructions
– 0000 eat rice
– 0001 eat burger
– 0010 eat bread
– 0011 eat pizza

FUUAST, Islamabad 17
Continue…
• 01AB instruction format
– Four different study instructions
– 0100 studying COA
– 0101 studying DS
– 0110 studying OS
– 0111 studying VP

FUUAST, Islamabad 18
(A) PUSH (B) 0.687510 (C) 0.10112

(D) 11112 (E) Zero Address (F) S RAM

(H) System
(G) D RAM (I) Cache
Memory
1. What is the relationship between A & E?
2. B in binary format is ___________ box?
3. Which box has type of SRAM?
4. H is made up of?
5. Which box has max number of 4-bit series?
6. Example of E is ________?

FUUAST, Islamabad 19
Assembly Programming
• MASM: Microsoft Macro Assembler by
Microsoft (Win)
• NASM: Netwide Assembler (Win & Linux)
• TASM: Turbo Assembler by Boreland (Win
& Linux)
• GAS: GNU Assembler (Linux)

FUUAST, Islamabad 20
Program Basics
• Single Line Comments
– Comments start with a semicolon ;
– Characters written on the right side of the
semicolon is considered as a comment
ADD AX, BX ; Adds value of BX & AX registers
– Comments are not executed
– Will not be changed into machine code
– Length of a comment will not influence the
size of the program
FUUAST, Islamabad 21
Continue…
• Multi Line Comments
– Begin with COMMENT directive and a
programmer-chosen character
– End with the same programmer-chosen
character
COMMENT !
This is a comment.
This line is also a comment.
!
– Any other character can be used as well
FUUAST, Islamabad 22
Identifier
• A name that programmer apply to an item
in the program
• It might identify a variable, a constant, a
procedure, or a code label
• Keep in mind while creating identifier:
– May contain between 1 and 247 characters
– Not case sensitive except for predefined
symbols

FUUAST, Islamabad 23
Continue…
– The first character must be a letter (A..Z, a..z),
underscore (_), @ , ?, or $
– Subsequent characters may also be digits
– An identifier cannot be the same as an
assembler reserved word
– var1, Count, $first, _main, MAX , open_file, xVal

FUUAST, Islamabad 24
Reserve Words
• Special meaning in assembly
• Can only be used in their correct context
• Cannot be used as identifiers
– Instruction mnemonics
• Operations that the computer can execute
• MOV, ADD, MUL
– Directives
• Tell the MASM how to assemble program
• .DATA, .CODE

FUUAST, Islamabad 25
Continue…
– Attributes
• Provide size and usage information for variables
and operands
• BYTE, WORD
– Operators
• Used in expressions
• Arithmetic, logical
– Predefined symbols
• Used to return constant integer values
• @data

FUUAST, Islamabad 26
Statements
• An assembly program has a set of
statements:
– Instructions such as MOV and ADD that will
be translated into machine code or object
code
– Directives that tells assembler to perform
certain actions like define a data item etc.

FUUAST, Islamabad 27
Instructions
• An instruction is a statement that becomes
executable when a program is assembled
• Instructions are translated by the assembler
into machine language
• Syntax:
[label] mnemonic operand(s) [;comment]
label optional
instruction mnemonic required: such as MOV, ADD, SUB, MUL
operands usually required: such as aex, 10
comment optional

FUUAST, Islamabad 28
Data Label
• A data label identifies the location of a
variable
• It provide a convenient way to reference
the variable in code
• For example, defines a variable named
count:
count DWORD 100

FUUAST, Islamabad 29
Continue…
• Assembler assigns a numeric address to each
label
• It is possible to define multiple data items
following a single label
• Example, array defines the location of the first
number (1024). The other numbers following
in memory immediately afterward:
array DWORD 1024, 2048
DWORD 4096, 8192

FUUAST, Islamabad 30
Code Label
• A label in the code area of a program must
end with a colon (:)
• In this context, labels are used as targets
of jumping and looping instructions
target:
mov ax,bx
...
jmp target

FUUAST, Islamabad 31
Directives
• A directive is a command embedded in the
source code
• Directives do not execute at run time
• Not case sensitive (.data, .DATA, .Data)
• Directives can define:
– Variables
– Macros
– Procedures
– Memory segments
FUUAST, Islamabad 32
Continue…
• Used to declare code, data areas, select
memory model, declare procedures, etc.
• The DWORD directive tells the assembler
to reserve space in the program for a
doubleword variable
myVar DWORD 26 ; DWORD directive

FUUAST, Islamabad 33
Segment
• Important function of directives is to define
program sections or segments
– .data
• The .DATA directive identifies the area of a program
containing variables
– .code
• The .CODE directive identifies the area of a program
containing instructions
– .stack 100h
• The .STACK directive identifies the area of a program
holding the runtime stack and setting its size

FUUAST, Islamabad 34
Data Types
• Fundamental data type of IA-32 is
– byte 8-bit
– word 2-bytes (16-bits)
– doublewords 4-bytes (32-bits)
– quadwords 8-bytes (64-bits)

FUUAST, Islamabad 35
Integer Constant
[{+ | −}] value [radix]
[] are optional
Symbol Detail Definition
h Hexadecimal 03Ah
q/o Octal 42o, 42q
d Decimal 26d
b Binary 11010011b
r Encoded real

FUUAST, Islamabad 36
Real Number Constant
[sign] integer.[integer][exponent]
• Valid real number constants are:
2.
+3.0
-44.2E+05
26.0E5

FUUAST, Islamabad 37
Character Constant
• Enclose character in single or double
quote
• ASCII character = 1 byte
• Example: “x”, ‘A’

FUUAST, Islamabad 38
String Constant
• Enclosed in single or double quotation
marks
• Each character occupies a single byte
• Example: “xyz”, “ABC”, “Good”

FUUAST, Islamabad 39
Arithmetic Operators
Operator Name Precedence
() Parenthesis 1
+, - Unary plus and minus 2
*, / Multiply, divide 3
MOD Modulus 3
+, - Add, subtract 4

FUUAST, Islamabad 40
Operands
• Instructions can have between zero and
three operands
• No Operand
– stc ; set carry flag
• One operand
– inc eax ;register
– inc myByte ;memory

FUUAST, Islamabad 41
Continue…
• Two operand
– add ebx, ecx ; register, register
– sub myByte, 25 ; memory, constant
– add eax, 36 * 25 ; register, constant-expression

FUUAST, Islamabad 42
Mnemonics
Mnemonics Description
MOV Move one value to another
ADD Add two values
SUB Subtract one value from another
MUL Multiply two values
JMP Jump to a new location
CALL Call a procedure

FUUAST, Islamabad 43
Sample Program
main PROC
mov eax, 5 ; move 5 to the EAX register
add eax, 6 ; add 6 to the EAX register
call WriteInt ; display value in EA
exit ; quit
main ENDP

FUUAST, Islamabad 44
HLL to Assembly Instruction
• X = (Y+4) * 3
mov eax, Y ;move Y to the EAX register
add eax, 4 ;add 4 to the EAX register
mov ebx, 3 ;move 3 to the EBX register
mul eax, ebx ;multiply EAX by EBX
mov X, eax ;move EAX to X

FUUAST, Islamabad 45
Hardware & Software
• MASM
• Editor: Microsoft Visual C++ 2005 Express
• 32-bit Debugger: The debugger supplied
with Visual C++ 2005 Express is excellent

• MASM32
www.masm32.com

FUUAST, Islamabad 46

You might also like