CH 2

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 56

Chapter two

Introduction
to
Assembly Language Programming
Some Important Questions to Ask
 What is Assembly Language?
 Why Learn Assembly Language?
 What is Machine Language?
 How is Assembly related to Machine Language?
 What is an Assembler?
 How is Assembly related to High-Level Language?
 Is Assembly Language portable?

Basic Concepts Computer Organization and Assembly Language


slide 2/43
A Hierarchy of Languages

Basic Concepts Computer Organization and Assembly Language


slide 3/43
Assembly and Machine Language
 Machine language
 Native to a processor: executed directly by hardware
 Instructions consist of binary code: 1s and 0s
 Assembly language
 A programming language that uses symbolic names to represent
operations, registers and memory locations.
 Slightly higher-level language
 Readability of instructions is better than machine language
 One-to-one correspondence with machine language instructions
 Assemblers translate assembly to machine code
 Compilers translate high-level programs to machine code
 Either directly, or
 Indirectly via an assembler
Basic Concepts Computer Organization and Assembly Language
slide 4/43
Compiler and Assembler

Basic Concepts Computer Organization and Assembly Language


slide 5/43
Instructions and Machine Language
 Each command of a program is called an
instruction (it instructs the computer what to do).

 Computers only deal with binary data, hence the


instructions must be in binary format (0s and 1s)
 The set of all instructions (in binary form) makes
up the computer's machine language. This is
also referred to as the instruction set.

Basic Concepts Computer Organization and Assembly Language


slide 6/43
Instruction Fields
 Machine language instructions usually are made up of
several fields.
 Each field specifies different information for the
computer. The major two fields are:
 Opcode field which stands for operation code and it
specifies the particular operation that is to be performed.
 Each operation has its unique opcode.
 Operands fields which specify where to get the source
and destination operands for the operation specified by
the opcode.
 The source/destination of operands can be a constant, the
memory or one of the general-purpose registers.
Basic Concepts Computer Organization and Assembly Language
slide 7/43
Assembly vs. Machine Code

Flash Movie

Basic Concepts Computer Organization and Assembly Language


slide 8/43
Translating Languages
English: D is assigned the sum of A times B plus 10.

High-Level Language: D = A * B + 10

A statement in a high-level language is translated


typically into several machine-level instructions

Intel Assembly Language: Intel Machine Language:


mov eax, A A1 00404000
mul B F7 25 00404004
add eax, 10 83 C0 0A
mov D, eax A3 00404008

Basic Concepts Computer Organization and Assembly Language


slide 9/43
Mapping Between Assembly Language and
HLL
 Translating HLL programs to machine language
programs is not a one-to-one mapping
 A HLL instruction (usually called a statement) will be
translated to one or more machine language instructions

Basic Concepts Computer Organization and Assembly Language


slide 10/43
Advantages of High-Level Languages
 Program development is faster
 High-level statements: fewer instructions to code
 Program maintenance is easier
 For the same above reasons
 Programs are portable
 Contain few machine-dependent details
 Can be used with little or no modifications on different machines
 Compiler translates to the target machine language
 However, Assembly language programs are not portable

Basic Concepts Computer Organization and Assembly Language


slide 11/43
Why Learn Assembly Language?
 Accessibility to system hardware
 Assembly Language is useful for implementing system software
 Also useful for small embedded system applications
 Space and Time efficiency
 Writing assembly programs gives the computer designer the
needed deep understanding of the instruction set and how to design
one
 To be able to write compilers for HLLs, we need to be expert with
the machine language. Assembly programming provides this
experience

Basic Concepts Computer Organization and Assembly Language


slide 12/43
Assembler
 Software tools are needed for editing, assembling,
linking, and debugging assembly language programs
 An assembler is a program that converts source-code
programs written in assembly language into object files
in machine language
 Popular assemblers have emerged over the years for the
Intel family of processors. These include …
 TASM (Turbo Assembler from Borland)
 NASM (Netwide Assembler for both Windows and Linux), and
 GNU assembler distributed by the free software foundation
 Emu8086 emulator

Basic Concepts Computer Organization and Assembly Language


slide 13/43
Linker and Link Libraries
 You need a linker program to produce executable files

 It combines your program's object file created by the


assembler with other object files and link libraries, and
produces a single executable program

 LINK32.EXE is the linker program provided with the


MASM distribution for linking 32-bit programs

 We will also use a link library for input and output

 Called Irvine32.lib developed by Kip Irvine


 Works in Win32 console mode under MS-Windows

Basic Concepts Computer Organization and Assembly Language


slide 14/43
Assemble and Link Process

Source Object
File Assembler File

Source Object Executable


File Assembler File Linker
File

Source Object Link


File Assembler File Libraries

A project may consist of multiple source files


Assembler translates each source file separately into an object file
Linker links all object files together with link libraries
Basic Concepts Computer Organization and Assembly Language
slide 15/43
Basic Elements of Assembly
Language
Integer Constants

An integer constant (or integer literal) is made


up of an optional leading sign, one or more
digits, and an optional suffix character (called a
radix) indicating the number’s base:

[{+| −}] digits [ radix]


Basic Concepts Computer Organization and Assembly Language
slide 16/43
Basic Elements of Assembly…

Basic Concepts Computer Organization and Assembly Language


slide 17/43
Basics …
If no radix is given, the integer constant is
assumed to be decimal.
Here are some examples using different
radixes:

Basic Concepts Computer Organization and Assembly Language


slide 18/43
Basics …
 A hexadecimal constant beginning with a letter
must have a leading zero to prevent the
assembler from interpreting it as an identifier .
Integer Expressions
 An integer expression is a mathematical expression
involving integer values and arithmetic operators.
 The expression must evaluate to an integer, which can
be stored in 32 bits (0 through FFFFFFFFh).
 The arithmetic operators are listed in Table 3-1
according to their precedence order, from highest (1) to
lowest (4).
Basic Concepts Computer Organization and Assembly Language
slide 19/43
Basics …

Basic Concepts Computer Organization and Assembly Language


slide 20/43
Basics …
 Precedence refers to the implied order of operations
when an expression contains two or more operators.
 The order of operations is shown for the following
expressions:

Basic Concepts Computer Organization and Assembly Language


slide 21/43
Basics…
Character Constants
A character constant is a single character enclosed in
single or double quotes.
MASM stores the value in memory as the character’s
binary ASCII code. Examples are 'A‘ or "d“
String Constants
A string constant is a sequence of characters (including
spaces) enclosed in single or double quotes:
'ABC'
"Good night, Gracie"

Basic Concepts Computer Organization and Assembly Language


slide 22/43
Basics…
Reserved Words
Reserved words have special meaning in MASM and can only be
used in their correct context.
There are different types of reserved words:
Instruction mnemonics, such as MOV, ADD, and MUL
Register names
Directives, which tell MASM how to assemble programs
Attributes, which provide size and usage information for
variables and operands. Examples are BYTE and WORD
Operators, used in constant expressions
Predefined
Basic Concepts
symbols, such as @data, which return constant
Computer Organization and Assembly Language
integer values at assembly timeslide 23/43
Keywords in assembly language

Basic Concepts Computer Organization and Assembly Language


slide 24/43
Keywords in assembly language…

Basic Concepts Computer Organization and Assembly Language


slide 25/43
Basics …
Identifiers
An identifier is a programmer-chosen name. It might identify a
variable, a constant, a procedure, or a code label.
Keep the following in mind when creating identifiers:
They may contain between 1 and 247 characters.
They are not case sensitive.
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.
Basic Concepts Computer Organization and Assembly Language
slide 26/43
Basics ….
 Make identifier names descriptive and easy to
understand.
 Here are some valid identifiers:

Basic Concepts Computer Organization and Assembly Language


slide 27/43
Basics …
Directives
A directive is a command embedded in the source
code that is recognized and acted upon by the
assembler.
Directives do not execute at runtime.
Directives can define variables, macros, and
procedures.
They can assign names to memory segments and
perform many other housekeeping tasks related to the
assembler.
 In MASM, directives are case insensitive. For example, it
Basic Concepts Computer Organization and Assembly Language
recognizes .data, .DATA, and .Data as equivalent.
slide 28/43
Basics…
 The following example helps to show the difference
between directives and instructions.
 The DWORD directive tells the assembler to reserve
space in the program for a doubleword variable.
 The MOV instruction, on the other hand, executes at
runtime, copying the contents of myVar To the EAX
register:

Basic Concepts Computer Organization and Assembly Language


slide 29/43
Basics …
 Defining Segments
One important function of assembler directives is to
define program sections, or segments.
The .DATA directive identifies the area of a program
containing variables: .data
 The .CODE directive identifies the area of a
program containing executable instructions: .code
 The .STACK directive identifies the area of a
program holding the runtime stack, setting its
size: .stack 100h
Basic Concepts Computer Organization and Assembly Language
slide 30/43
 The .MODEL Directive
 The memory model directive specifies the size of the
memory the program needs.
 Based on this directive, the assembler assigns the
required amount of memory to data and code

Basic Concepts Computer Organization and Assembly Language


slide 31/43
Structure of assembly language program

Basic Concepts Computer Organization and Assembly Language


slide 32/43
Basic Concepts Computer Organization and Assembly Language
slide 33/43
Basic Concepts Computer Organization and Assembly Language
slide 35/43
Basic Concepts Computer Organization and Assembly Language
slide 36/43
Basic Concepts Computer Organization and Assembly Language
slide 37/43
Basic Concepts Computer Organization and Assembly Language
slide 38/43
Basic Concepts Computer Organization and Assembly Language
slide 39/43
Basic Concepts Computer Organization and Assembly Language
slide 40/43
For example
org 100h
.model small
.data
message db “welcome to assembly language $"
.code

main proc

mov ah,09h
mov dx,offset message
int 21h

mov ah,4ch
mov al,00
int 21h

endp
Basic Concepts Computer Organization and Assembly Language
slide 41/43
end main
Add,sub, inc,dec

Basic Concepts Computer Organization and Assembly Language


slide 42/43
Input data from keybord

Basic Concepts Computer Organization and Assembly Language


slide 43/43
Write a program to add two numbers in assembly language.
org 100h
.model small
.code
mov ax,3h
mov bx,5h
add ax,bx
add ax,30h
mov ah,02h
mov dx,ax
int 21h
.exit
end
ret
Basic Concepts Computer Organization and Assembly Language
slide 44/43
Basic Concepts Computer Organization and Assembly Language
slide 45/43
Basic Concepts Computer Organization and Assembly Language
slide 46/43
Basic Concepts Computer Organization and Assembly Language
slide 47/43
Basic Concepts Computer Organization and Assembly Language
slide 48/43
Basic Concepts Computer Organization and Assembly Language
slide 49/43
Basic Concepts Computer Organization and Assembly Language
slide 50/43
Basic Concepts Computer Organization and Assembly Language
slide 51/43
Basic Concepts Computer Organization and Assembly Language
slide 52/43
Basic Concepts Computer Organization and Assembly Language
slide 53/43
Basic Concepts Computer Organization and Assembly Language
slide 54/43
Basic Concepts Computer Organization and Assembly Language
slide 55/43
Quiz 1 : 5%
1.Write the difference between assembly language and high
level language(2pts)
2.Write assembly code that display your department name.

Basic Concepts Computer Organization and Assembly Language


slide 56/43

You might also like