Transcript Assembler

Week 6
Intro. to Assembly Language
Dr. Muhammad Ayaz
Levels of Programming



Machine Language
Assembly Language
High level Language
Computer Languages
Applications
High Level Language
Low Level language
Hardware
68000 Assembly Language
C, JAVA
Word, Excel
HLL
 Complier
Assembly Language
 Assembler
Machine Language
Machine Language (Low level)



A computer can directly understand only its own
machine language.
Machine languages generally consist of streams of
numbers (ultimately reduced to binary 1s and 0s).
Machine-language programming proved
Longer Codes
 Difficult to understand.
 Error prone (very easy to make mistakes).
 Very hard to find these mistakes


Not Portable: must be rewritten on another computer
architecture
Machine Language Characteristics




Direct memory management
Little-to-no abstraction from the hardware
Register access
Statements usually have an obvious
correspondence with clock cycles
Superb performance
Assembly Language




To overcome the problems of speed and errors, assembly
language is used.
English-like abbreviations form the basis of assembly
languages.
Computers cannot understand assembly-language code
until it is translated into machine language.
Normally, designed for a family of processors.
High level Language


The main advantage of high-level languages over lowlevel language is that they are easier to read, write, and
maintain.
These are modern programming languages.




C/C++, VB, JAVA, C#, Pascal, FORTRAN ….
Much more simpler to understand and remember.
High-level languages are closer to the human language.
The early high-level programming languages were
designed in the 1950s.
Comparing Low & High-level Languages (1)
Advantages of high-level language
1.
2.
3.
4.
Advantages of HIGH level
language
more effective
Reasons
easier to learn, write, read and
modify
programs are shorter in length
instructions are English-like format
portable (can use in different
computers with little
modification)
designed to solve problem
one high-level language statement
take the place of many machine
instructions
machine-independent
Comparing Low & High-level Languages (2)
Advantages of low-level language
1.
2.
3.
Advantages of LOW level
language
more efficient
Reasons
hardware features, like bus width, is
fully made use of
require smaller memory space to fewer low-level instructions are
run
needed to do the same task (no
redundant instructions produced by a
translator)
precise control of hardware
some low-level instructions are
designed specifically for controlling
the hardware at low level
Why learn Assembly Language?



To learn how high-level language code gets
translated into machine language
 ie: to learn the details hidden in HLL code
To learn the computer’s hardware
 by direct access to memory, video controller,
sound card, keyboard…
To speed up applications
 Direct access to hardware (ex: writing directly to
I/O ports instead of doing a system call)
 Good ASM code is faster and smaller: rewrite in
ASM the critical areas of code
When to Use Assembly

When speed and size matter !
 Equipment
that must response very quickly.
 Device driver.
 When the resource is limited.

When we use the specialized instructions:
 3D


graphic library
When there is no compiler !
When you want to understand internal
architecture of a CPU !
Translation Process

Assembler:




translate Assembly to a Binary Code.
check Syntax.
produce an object file (not executable).
Linker:



combine one or more object files.
resolve references to other object files / libraries.
produce an executable program.
Assembly Langu
Translation Process (Cont’)
Assembly
Program
Object
File
Assembler
Object
File
Object
File
Natawut Nupairoj
Libraries
Linker
Executable
File
Assembly Langu
Assembly Language Statements

Three types of statements in assembly language

1.
2.
3.
Typically, one statement should appear on a line
Executable Instructions

Generate machine code for the processor to execute at runtime

Instructions tell the processor what to do
Assembler Directives

Provide information to the assembler while translating a program

Used to define data, select memory model, etc.
Macros

Shorthand notation for a group of statements

Sequence of instructions, directives, or other macros
32-bit registers

Upper (first) halves do not have names
Registers


The advantage of registers over computer memory is that
they are extremely fast.
The CPU has four general purpose programming registers,
EAX,
 EBX,
 ECX and
 EDX.


Each of the four general purpose programming registers is
32 bits wide
Registers …



Among these 32 bits, we can access the lower 16 bits of
EAX (called AX) if we want to.
Further, the lower 16 bits of EBX is called BX, etc.
Moreover, we can access both the upper and lower 8 bits of
AX (called AH and AL respectively), too, and similarly for
BX, CX and DX.
16-bit Registers
General-Purpose Registers





These are Data Registers where the upper
and lower half can be accessed separately
AX = Accumulator (used in arithmetic)
BX = Base (arithmetic, data movement…)
CX = Counter (for looping instructions)
DX = Data (multiplication & division)
Index Registers




SP (stack pointer) contains the offset of the
top of the stack
BP (base pointer) often contains the offset of
a data/variable in the stack
SI (source index) and
DI (destination index) are used in string
movement instructions
 SI
points to the source string
 DI points to the destination string
Segment Registers




CS (code segment) holds the base location
of all executable instructions in a program
DS (data segment) the default base location
for memory variables
ES (extra segment) additional base location
for memory variables
SS (stack segment) base location for stack
Status and Control Registers


IP (instruction pointer) always contains the offset of
the instruction to be executed next within the current
code segment
The FLAGS register consist of individual bits
indicating either
 the mode of operation of the CPU (control flag)
 the outcome of an arithmetic operation (status)