0% found this document useful (0 votes)
13 views

Lecture 02 Introduction To Assembly Language

Uploaded by

ahmedelhrmeel201
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)
13 views

Lecture 02 Introduction To Assembly Language

Uploaded by

ahmedelhrmeel201
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/ 31

Introduction

to Assembly Language
Introduction to assembly language

Assembly language program consist of statements, one per


line. Each statement call instruction, which the assembler
translates into machine code, or an assembler directive. which
tells the assembler to perform some specific task. Both
instruction and directives consists of four fields:

2 Dr. Hassanein Shaban Ahmed


Introduction to assembly language
Statements in assembly is either
 Instruction: one per line and assembler translates it into
machine code
 Assembler directive : instructs assembler to perform
some specific task
Most (but not all) instructions are made up of three
characters and have an operand then a comma
then another operand.
Syntax: Instruction operand1, [operand2]
 operand1 can be a register, or memory location.
 operand2 can be a register, memory location,
or immediate value.
3 Dr. Hassanein Shaban Ahmed
Introduction to assembly language
In General, instruction in assembly consists of four fields:
name operation operand1,[operand2 ] ;comment

An example of an instruction is
start: MOV CX,5 ;initialize counter
An example of an assembler directive is
MAIN PROC

4 Dr. Hassanein Shaban Ahmed


Introduction to assembly language
Name Field:
The name field is used for instruction labels, procedure name, and
variables names: the assembler translates names into memory
addresses.
 It consists of letters, digits, and the special
characters ? . @ _ $ %
 It is long from 1 to 31 characters
 It must be the first character
 Not begin with a digit

5 Dr. Hassanein Shaban Ahmed


Introduction to assembly language
Operation Field:
For an instruction, the operation field contains a symbolic
operation code (opcode) for example MOV,ADD.
In an assembler directive, the operation field contains a
pseudo-operation code. Pseudo-ops are not translated
machine code; they simply tell assembler to do something.
such as PROC is used to create a procedure.

6 Dr. Hassanein Shaban Ahmed


Introduction to assembly language
Operands Field:
For an instruction, the operand field specifies the data
that are to be acted on by the operation. An instruction may
have zero, one, or two operands for example:
NOP INC AX ADD word1,2

operand1 can be a register, or memory location. It is the


destination operand
 operand2 can be a register, memory location, or
immediate value (constant). It is the source operand

For an assembler directive, the operand field contains


more information about the directive
7 Dr. Hassanein Shaban Ahmed
Introduction to assembly language
Comment Field:
The comment field of a statement is used by the
programmer to say something about what the statement
does. A semicolon marks the beginning of this field, and the
assembler ignores anything typed after the semicolon.
 It starts by a semi colon
 It is optional
 The assembler ignore it
 It is used by the programmer to say what the statement does

8 Dr. Hassanein Shaban Ahmed


Program Data
The processor “machine language” operates only on binary data.
In assembly language program we may express data as binary,
decimal, or hex, and even as characters.
Numbers
A binary number is written as a bit string followed by letter “B” or
“b”. For example 1010B
A decimal number is a string of decimal digit, ending with an optional letter “D” or
“d”
A hex number must be begin with a decimal digit and end with the letter “H” or
“h”. For example 0ABCH (the reason for this is that the assembler would be
unable to tell whether a symbol such as “ABCH” represents the variable name
“ABCH” or the hex number ABC
Characters
Characters and character string must be enclosed in single or
double quotes for example, “A” or ‘hello’
9 Dr. Hassanein Shaban Ahmed
Variables
We use assembler directive to define a variable. We use
one of the following pseudo op.
 DB : define Byte
 DW : Define Word

10 Dr. Hassanein Shaban Ahmed


Variables: DB and DW Directive
The DB and DW are directive that define data. The byte variable
defines as the following :
name DB initial_value
For Example:
ALPHA1 DB 4
ALPHA2 DB ?
Notice that:The question mark “?” means the variables
uninitialized.

11 Dr. Hassanein Shaban Ahmed


DB and DW Directive
The word variable defines as the following :
name DW initial_value
For Example:
BETA1 DW 329

12 Dr. Hassanein Shaban Ahmed


Program Structure
The machine language programs consists of code, data,
and stack. Each part occupies a memory segment. This
organization is reflected in an assembly language program.
This time, the code, data, and stack are structured as
program segments. Each program segment is translated into
a memory segment by assembler.
Memory Models:
The size of code and data of a program can be determined by
specifying a memory model using the .MODEL directive. The
syntax is
.MODEL memory model
Most frequently memory models are SMALL, MEDIUM,
COMPACT, LARGE, and Huge. In general the appropriate model
is small.
13 Dr. Hassanein Shaban Ahmed
Memory Models
Model Description
Code in one segment
SMALL data in one segment
Code in more than one segment
MEDIUM data in one segment
Code in one segment
COMPACT data in more than one segment

Code in more than one segment


LARGE data in more than one segment
No array larger than 64kb
Code in more than one segment
HUGE data in more than one segment
Arrays may be larger than 64kb
The .MODEL directive should come before any segment definition
14 Dr. Hassanein Shaban Ahmed
Segment Definition
Data Segment
A program’s data segment contains all variables definitions
and constant. To declare a data segment, we use the directive
.DATA, followed by variable and constant declarations. For
example,
.DATA
WORD1 DW 2
WORD2 DW 5
MSG DB ‘THIS IS A MESSAGE’

15 Dr. Hassanein Shaban Ahmed


Cont…
A program containing data segment should
begin with two instructions:
MOV AX, @DATA
MOV DS,AX

@DATA is the name of data segment defined by


.DATA

16 Dr. Hassanein Shaban Ahmed


Segment Definition
Stack Segment
The purpose of the stack segment declaration is to set aside a
block of memory (the stack area) to store the stack. The
declaration syntax is
.STACK size
Where size is an optional number that specifies the stack size
in bytes. For example
.STACK 100H
This means that the stack area is 100H bytes.

17 Dr. Hassanein Shaban Ahmed


Segment Definition
Code Segment
The code segment contains a program instructions. The
declaration syntax is
.CODE name
Where name is the optional name of the segment (there is no
need for a name in a SMALL program, because the
assembler will generate an error
Inside a code segment, instructions are organized as
procedures.

18 Dr. Hassanein Shaban Ahmed


Segment Definition

• The simplest procedure definition as the following


Name Proc
; body of procedure
Name EndP
• Where Name is the name of the procedure
• Proc and EndP are assembler directive (more on defined
procedure in chapter 8).

An example of a code segment definition:


.CODE
MAIN PROC
;main procedure instructions
MAIN ENDP
; other procedure go here

19 Dr. Hassanein Shaban Ahmed


Putting it together A form of assembly programs

We can construct the general Form of a SMALL model program.


This form may be used in most applications:
.MODEL SMALL
.STACK 100H
.DATA
;variables & constants definitions here
.CODE
MAIN PROC
; instructions go here
MAIN ENDP
; other procedure go here
END MAIN
The last line in the program should be the END directive, followed
by name of the main procedure

20 Dr. Hassanein Shaban Ahmed


The MOV Instruction

The MOV instruction transfers data of the second operand2


(source) to the first operand1(destination). The general format for
MOV is
[label:] MOV operand1,operand2 comment
[label:] MOV register/memory, register/memory, immediate
Or simplified format is
MOV operand1,operand2

 Both operands must agree in size:


(both must have the same size) Byte, Word,….
 With Emu8086, each hex. Number having the size
” Byte” should start by 0.
21 Dr. Hassanein Shaban Ahmed
Example:
MOV AL,0B2h; put B2h into AL register

Instruction

Operand 1

Operand 2
Comment

Emu8086 doesn’t accept : MOV AL, B2


22 Dr. Hassanein Shaban Ahmed
The MOV Instruction

Example
MOV DX,AX ;register-to-register
MOV data1,DH ;register-to-memory, direct
MOV [DI],BX ;register-to-memory, indirect
MOV CX,40 ;immediate-to-register
MOV data1,40 ;immediate-to-memory
MOV CH,data1 ;memory-to-register
MOV AL, 0FFh ; Hex. Number move
MOV AH, 255 ; Dec. Number move
MOV DL, 01011111b; Binary Number move
MOV BL, AH ; Register value move
MOV CL, ‘A’ ; set CL to ASCII code of 'A', it is 41h.

23 Dr. Hassanein Shaban Ahmed


The XCHG exchange Instruction
The XCHG instruction performs another type of data transfer. It
exchanges the contents of 2 registers or register and memory location.
The general format for XCHG is
[label:] XCHG register/memory, register/memory

Destination Operand
Source Operand
General Register Memory Location
General Register Yes Yes
Memory Location yes no
Example
WORDQ DW ?
……
XCHG CL,BH ; exchange the contents two
; registers
XCHG CX,WORDQ ; exchange the contents register
; and memory
24 Dr. Hassanein Shaban Ahmed
Sample assembly Programs

.model small
.stack 64
.DATA
data1 DB 52h
data2 DB 29h
sum DB ?
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
MOV AL,data1
MOV BL,data2
ADD AL,BL
MOV sum,AL
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

25 Dr. Hassanein Shaban Ahmed


INC and DEC Instruction

 INC is used to add 1 to register or memory


location
 Dec is used to subtract one from register or
memory location
 The general format for DEC/INC is
[label:] INC/DEC register, memory

Note that INC and DEC require only one operand


 Example:
 INC word1
 DEC b1
 INC AX

26 Dr. Hassanein Shaban Ahmed


ADD and SUB instruction
The ADD and SUB instructions are used to add or substrate the
contents of two registers, a register and memory location, or to add
(subtract) a number to (from) a register or memory location. The
general format for ADD/SUB is
ADD destination, source
SUB destination, source
Illegal
Combinations of operands for ADD and SUB
Destination
Source
Register Example Memory Example
Register Yes ADD AX,AX Yes ADD WORD,AX

Memory Yes ADD AX,WORD NO ADD WORD1,WORD2

Constant Yes ADD AX,5 Yes ADD WORD,5

27 Dr. Hassanein Shaban Ahmed


ADD instruction- Addition
Operation: operand1 = operand1 + operand2
Examples:
MOV AL, 5 ; AL = 5
ADD AL, -3 ; AL = 2
MOV AL,08h
ADD AL,07h ; AL=0Fh
MOV AL,0FFh
ADD AL,01h ; AL=00h and AH =00h
MOV AL,0FFh
ADD AX,01h ; AL=00h and AH =01h
28 Dr. Hassanein Shaban Ahmed
SUB instruction- Subtraction
Operation: operand1 = operand1 - operand2
Examples:
MOV AL, 5 ; AL = 5
SUB AL, 4 ; AL = 1
MOV AL,0Ah
SUB AL,07h ; AL=03h
MOV AL,0FFh
SUB AL,01h ; AL=FEh

29 Dr. Hassanein Shaban Ahmed


ADD and SUB instruction EX.4.3

.MODEL SMALL
.STACK 64
.DATA
xx1 Dw 0AFh
yy1 Dw 96h
zz1 Dw ?
.CODE
MAIN PROC FAR
mov ax,@data
mov ds,ax
mov ax,xx1
add ax,yy1
mov zz1,ax
mov ax,4c00h
int 21h
MAIN ENDP
END MAIN

30 Dr. Hassanein Shaban Ahmed


NEG instruction
The NEG instruction is used to negate the contents of the
destination. NEG does this by replace the contents by its two’s
complement. The general format for NEG is
[label:] NEG register, memory

Ex4.4
.MODEL SMALL
.STACK 64
.DATA
xx1 Dw 0FFFEh
.CODE
MAIN PROC
mov ax,@data
mov ds,ax
mov ax,xx1
NEG ax
mov ah,4ch
int 21h
MAIN ENDP
END MAIN
31 Dr. Hassanein Shaban Ahmed

You might also like