0% found this document useful (0 votes)
165 views19 pages

Assembly Language Ab

This document provides information about assembly language and its advantages compared to machine language and high-level languages. Assembly language uses symbolic codes that are easier for humans to understand compared to machine language's binary. It has a one-to-one correspondence with machine language. While more difficult than high-level languages, assembly language allows for more control over hardware and is faster and more compact. The document describes common assembly language directives like SEGMENT and PROC that define code and data segments and procedures.

Uploaded by

seyfi
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)
165 views19 pages

Assembly Language Ab

This document provides information about assembly language and its advantages compared to machine language and high-level languages. Assembly language uses symbolic codes that are easier for humans to understand compared to machine language's binary. It has a one-to-one correspondence with machine language. While more difficult than high-level languages, assembly language allows for more control over hardware and is faster and more compact. The document describes common assembly language directives like SEGMENT and PROC that define code and data segments and procedures.

Uploaded by

seyfi
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/ 19

3.

ASSEMBLY LANGUAGE
Each family of processors has its own set of instructions for handling various operations like
getting input from keyboard, displaying information on screen and performing various other
jobs. These set of instructions are called 'machine language instructions'.
Processor understands only machine language instructions, which are strings of 1's and 0's.
Human programmers find it difficult to write programmes in machine code; it is difficult to
understand, difficult to read, difficult to remember. Instead, they may write in 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.
Assembly language is a low level programming language. You need to get some knowledge
about computer structure.
Levels of Programming
1. Machine Language
2. Assembly Language
3. High-level Languages
Machine Language
What the machine can understand
Ones & Zeros 100111010101110
Individual instructions that the processor executes one at a time
Assembly Language

Symbolic instruction that we can understand

Simple atomic instructions

Maps directly into machine language (1-to-1)

Is designed for a family of microprocessors


High-level Languages
Are the modern programming languages
C/C++, VB, Java, C#
Much more simple to understand & remember
Keywords in high-level languages corresponds to many Assembly instructions

Advantages of Assembly Language


An understanding of assembly language provides knowledge of:

Interface of programs with OS, processor and BIOS;


Representation of data in memory and other external devices;
How processor accesses and executes instruction;
How instructions access and process data;
How a program accesses external devices.

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.

Assembly vs High-level
Simpler than the Machine language
Require less memory & execution time
Can perform highly technical & complex tasks
However:
Specific to a processor family
Harder to remember & understand
Knowledge in Computer Architecture is necessary.
-Assembler language instruction has a one-to-one correspondence with the binary
machine code: the programmer controls precisely all the operations performed by the
processor (a high level language relies on a compiler or interpreter to generate the
instructions).
- Assembler can generate faster and more compact programs
- Assembler language allows direct access and full control of input/output
operations
-However, high-level language programs are easier to write and develop than assembler
language programs
Assembler
Programming the instructions directly in machine code is possible but very time
consuming as each basic instruction can have one of several different machine codes
depending on how the data is stored.
- The process of converting the microprocessor instructions to the binary
machine code can be performed automatically by a computer program,
called an ASSEMBLER.
-We use assembler to translate or convert assembly program into machine language.

Assembly Language Directives


Assembler directives are instructions to the assembler or the linker indicating how the
program should be created. Although they have the same format as an assembly language
instruction, they do not translate to object code. This section will only address a few of the
available directives. Please refer to one of the resources listed at the end of this chapter for
more information on the assembler directives used with the Intel 80x86.
SEGMENT Directive
One of the most important directives with respect to the final addressing and organization
of the application is SEGMENT. This directive is used to define the characteristics and or
contents of a segment.
There are three main segments: the code segment, the data segment, and the stack
segment. To define these segments, the assembly language file is divided into areas using
the SEGMENT directive. The beginning of the segment is defined with the keyword
SEGMENT while its end is defined using the keyword ENDS. Figure 17-2 presents the format
and parameters used to define a segment.

Format and Parameters Used to Define a Segment


The label uniquely identifies the segment. The SEGMENT directive label must match the
corresponding ENDS directive label.
The alignment attribute indicates the "multiple" of the starting address for the segment. For
a number of reasons, either the processor or the operating system may require that a
segment begin on an address that is divisible by a certain power of two. The align attribute is
used to tell the assembler what multiple of a power of two is required. The following is a list
of the available settings for alignment.

BYTE There is no restriction on the starting address.


WORD The starting address must beeven, i.e., the binary address must end in a zero.
DWORD The starting address must be divisible by four, i.e., the binary address must end
in two zeros.
PARA The starting address must be divisible by 16, i.e., the binary address must end in
four zeros.
PAGE The starting address must be divisible by 256, i.e., the binary address must end in
eight zeros.
The combine attribute is used to tell the linker if segments can be combined with other
segments. The following is a list of a few of the available settings for the combine attribute.
NONE The segment is to be located independently of the other segments and is logically
considered separate.
PUBLIC or COMMON The segment may be combined with other segments of the same
name and class.
STACK Works like PUBLIC for stack segments.
the class attribute helps the assembler classify the information contained in the segment.
This is important in order to organize the data, code, and other information that the linker
will be partitioning into segments when it comes time to create the final application. Typical
values are 'Data', 'Code', or 'Stack'. Note that the apostrophes are to be included as part of
the attribute value.

Examples :
KodSeg

SEGMENT para public Code

DataSeg

SEGMENT para public Data

StakSeg

SEGMENT para Stack Stack

PROC Directive
The next directive, PROC, is used to define the beginning of a block of code within a code
segment. It is paired with the directive ENDPwhich defines the end of the block. The code
defined between PROC and ENDP should be treated likea procedure or a function of a highlevel language. This means thatjumping from one block of code to another is done by calling
it like a procedure.

Figure 17-4Format and Parameters Used to Define a Procedure


As with the SEGMENT directive, the labels for the PROC directive and the ENDP directive
must match. The attribute for PROC is either NEAR or FAR. A procedure that has been
defined as NEAR uses only an offset within the segment for addressing. Procedures defined
as FAR need both the segment and offset for addressing.

END Directive
Another directive, END, is used to tell the assembler when it has reached the end of allof the
code. Unlike the directive pairs SEGMENT and ENDS and PROCand ENDP, there is no
corresponding directive to indicate the beginning of the code.
Data Definition Directives
The previous directives are usedto tell the assembler how to organize the code and data.
The next class of directives is used to define entities that the assembler will convert directly
to components to be used by the code. They do not represent code; rather they are used to
define data or constants on which the application will operate.
The primary form of these directives is Dx where a character is substituted for the 'x' to
indicate the incremental size of memory that is being reserved. For example, a single byte

can be reserved using the directive DB. Figure 17-5 presents some of the define directives
and their format.

Figure 17-5Format and Parameters of Some Define Directives

The label, which is to follow the formatting guidelines of the label field defined earlier, is not
required. When it is used, the assembler assigns it the address corresponding to the next
element of memory being reserved. The programmer may then use itthroughout their code
to refer back to that address.
The expression after the directive is required. The expression is used to tell the assembler
how much memory is to be reserved and if it is to be initialized. There are four primary
formats for the expression.
Constants The expression can be a list of one or more constants.
These constants will be converted to binary and stored in the order
that they were defined.
String The expressioncan be a string. The assembler will divide the string into its
characters and store each character in the incremental space required by the selected define
directive, i.e., DB reserves memory a byte at a time, DW reserves memory a word at a time,
DD reserves memory a double word at a time, and DQ reserves memory a quad word at a
time.
Undefined A question mark (?) can be used to tell the assembler that the memory is to
be reserved, but left undefined.
Duplicated elements The keyword DUP may be used to replicate the same value in order
to fill a block of memory.

Data Definition Directive


DB (Define Byte): 1 Byte
DW (Define Word):2 Byte
DD (Define double word):: 4 Byte
DF (Define Far Word): 6 Byte
DQ (Define Quad Word): 8 Byte
DT (Define Ten Byte): 10 Byte
DUP: Duplicate
NUM1 DB 3 DUP(0); Allocate 3 bytes space in the memory and set the content 0.
NUM2 DW 10 DUP(5) Allocate 10 word or 20 bytes space in the memory and set the content 5.

String definition
STR1 DB KARABUK
STR2 DB K,A,R,A,B,U,K
Array definition
ARRY1 DB 2, 4, 0, -5, 7
ARRY2 DB 12, 0FH, 01001001B
0F means hexadecimal number,
01001001B means binary numbers.

Example program in EXE format


TITLE EXAMPLE1.ASM (.EXE )
PAGE 60,132
;--------------------------------------------DATASG SEGMENT PARA 'DATA'
S1 DB 45
S2 DB 30
RESULT DB ?
DATASG ENDS
;--------------------------------------------STACKSG SEGMENT PARA STACK 'STACK'
DB 32 DUP (0)
STACKSG ENDS
;--------------------------------------------CODESG SEGMENT PARA 'CODE'
MAIN

PROC FAR
ASSUME CS:CODSG, SS:STACKSG, DS:DATASG
MOV AX, DATASG
MOV DS,AX
MOV ES,AX
bu komut dikkat alnmaz
MOV AL,S1
MOV BL,S2
ADD AL,BL
MOV RESULT,AL

MOV AH,4CH
INT 21H
MAIN ENDP
CODESG ENDS
END MAIN

; AL ye SAYI1 deerini at
; AL ve BL yi topla

; Exit program
;

Using simplified segment directive


.MODEL, .STACK, .DATA, and .CODE Directives
Instead of going to the trouble of defining the segments with the SEGMENT directive, a
programmer may select a memory model. By defining the memory model for the program, a
basic set of segment definitions is assumed. The directive .MODEL can do this. Figure 17-3
presents the format of the .MODEL directive.
8

.MODEL memory_model
Figure 17-3 Format of the .MODEL Directive
Table 17-1 presents the different types of memory models that can be used with the
directive. The memory models LARGE and HUGE are the same except that HUGE may
contain single variables that use more than 64K of memory. There are three more directives
that can be used to simplify the definition of the segments. They are .STACK, .DATA, and
.CODE.
When the assembler encounters one of these directives, it assumes that it is the beginning
of a new segment, the type being defined by the specific directive used (stack, data, or
code). It includes everything that follows the directive in the samesegment until a different
segment directive is encountered.
The .STACK directive takes an integer as its operand allowing the programmer to define the
size of the segment reserved for the stack.
The .CODE segment takes a label as its operand indicating the segment's name.
Table 17-1 Memory Models Available for use with .MODEL
MODEL

EXE Program (Simplified Format using Model Directive)


TITLE ORNEK1.ASM (.EXE FORMAT)
PAGE 60,132
;--------------------------------------------.MODEL SMALL
.STACK 32
.DATA
SAYI1 DB 45
9

Small
Medium

Number of
Code
segments
1
More than 1

Number of
Data
segments
1
1

Compact
Large
Huge
Flat

1
More than 1
More than 1
More than 1

More than 1
More than 1
More than 1
More than 1

SAYI2 DB 30
SONUC DB ?
;--------------------------------------------.CODE
ANA

PROC FAR
MOV

AX, @DATA

MOV

DS,AX

MOV AL,SAYI1
MOV BL,SAYI2
ADD AL,BL
MOV SONUC,AL

MOV AH,4CH
INT 21H
ANA

ENDP

END ANA

EXE and COM programs


Up to now, you have written, asssembled and executed programs in EXE format. The linker
automatically generates a particular format for an EXE program. You can also generate a
COM program for execution. One example of a commonly used COM program is
COMMAND.COM. The advantages of COM programs are that they are smaler than EXE
programs and are more easily adapted to resident programs.

There are some differences bitween a EXE and COM programs. An EXE programs may be
virtually any size, whereas a COM program is restricted to one segment and a maximum of
64K, including the program segment prefix (PSP)The PSP is a 256 byte (hex 100) block that
DOS inserts immediately preceding a COM and EXE program when it loads them in memory.
COM programs is always smaller than its original EXE program. One reason is that 512 byte
header block that predcedes an EXE program on disk does not precede a COM program.

10

Because addressing begins at an offset of hex 100 bytes from the beginning of the PSP, code
an ORG directive as ORG 100H immediately following the code SEGMENT or CODE
statement.

EXE program (using Segment directive)

COM program (using Segment directive)

TITLE EXAMPLE1.ASM (in .EXE FORMAT)

TITLE EXAMPLE1.ASM (in .COM FORMAT)

PAGE 60,132

PAGE 60,132

;---------------------------------------------

;---------------------------------------------

DATASG SEGMENT PARA 'DATA'

CODESG SEGMENT PARA 'CODE'

NUM1 DB 45

ASSUME CS:CODESG, SS:CODESG,

NUM2 DB 30

DS:CODESG , ES:CODESG

RESULT DB ?

ORG 100H

DATASG ENDS

BEGIN:: JMP MAIN

;---------------------------------------------

;----------------------------------------------

STACKSG SEGMENT PARA STACK 'STACK'

NUM1 DB 45

DB

32 DUP (0)

NUM2 DB 30

STACKSG ENDS

RESULT DB ?

;---------------------------------------------

;----------------------------------------------

CODESG SEGMENT PARA 'CODE'

MAIN

MAIN

PROC FAR

MOV AL, NUM1

ASSUME CS:CODESG,

MOV BL, NUM2

SS:STACKSG, DS:DATASG
MOV

AX, DATASG

MOV

DS,AX

MOV

ES,AX

PROC NEAR

ADD AL,BL
MOV RESULT,AL

MOV AH,4CH
INT 21H

MOV AL, NUM1


MOV BL, NUM2

MAIN

ADD AL,BL

CODESG ENDS

MOV RESULT,AL

END BEGIN

MOV AH,4CH
INT 21H
MAIN

ENDP

ENDP

CODESG ENDS

END MAIN

11

EXE Program (Simplified Format using Model


Directive)

COM Program (Simplified program using Model )

TITLE ORNEK1.ASM (.EXE FORMAT)


PAGE 60,132

TITLE ORNEK1.ASM (COM FORMAT)

;--------------------------------------------.MODEL SMALL

PAGE 60,132
;---------------------------------------------

.STACK 32

.MODEL SMALL

.DATA

.CODE

DB 45

ORG 100H

NUM2 DB 30

BEGIN: JMP ANA

RESULT DB ?

;----------------------------------------------

;---------------------------------------------

NUM

NUM

.CODE
MAIN

PROC FAR

DB 45

NUM2 DB 30
RESULT DB ?

MOV

AX, @DATA

;----------------------------------------------

MOV

DS,AX

MAIN

PROC NEAR

MOV AL,NUM1

MOV AL,NUM1

MOV BL,NUM2

MOV BL,NUM2

ADD AL,BL

ADD AL,BL

MOV RESULT,AL

MOV RESULT,AL

MOV AH,4CH

MOV AH,4CH

INT 21H

INT 21H

MAIN

ENDP

MAIN

END MAIN

ENDP

END BEGIN

How to compile EXE and COM program


.EXE

.COM

TASM example1.ASM

TASM example1.ASM

TLINK example.OBJ

TLINK/t example.OBJ

We get example.exe

We get example1.com
12

Differences between Com and Exe programs


A COM program is restricted to one 64K segment.
A COM program is smaller than its original EXE program.
A COM program does not have data and stack segment. Whereas EXE program needs
data and stack segment.
in EXE program use FAR as a parameter, COM program use NEAR.

Program Control Instruction


Up to now, program examples have executed in a straight line, with one instruction
sequentially following another. Seldom, however, is a programmable problem that simple.
Mostprograms consists of a number of loops in which a series of steps repeats until reaching
a specific requirement and various tests to determine which of several actions to take. A
common requirement is to test whether a program is to terminate execution.
These requremenets involve a transfer of control to the address of an instruction that does
not immediadetly follow the one currently executing. A transfer of control may be forward
to execute a new series of steps or backward to reexecute the same steps.
Certain instruction can transfer control outside the normal sequential steps by causing an
offset value to be added to the instruction pointer (IP). Foollowing are four classes of
transfer operations, all covered in this chapter.
-

Uncondiditional jump, JMP


Looping, LOOP
Conditional jump, Jnnn(such as JE,JZ,JNZ, JA, JAE.)
Call a procedure, CALL

Short, Near, and Far Addresses


A jump operation reaches a short address by a one-byte offset, limited to a distance of -128
to 127 bytes.
A jump operation reaches near address by a one-word offset, limited to a distance of -32,768
to 32767 bytes within the same segment.
13

A far address may be another segment and is reached by a segment address and offset; Call
is the normal instruction for this purpose.

Short(-128,+127)

Near(-32768,+32767)

Far(Baka Segmentlere)

Jmp

Evet

Evet

Evet

Jnn

Evet

Evet

Hayr

Loop

Evet

Hayr

Hayr

CALL

Evet

Evet

Evet

The JMP, Jnn (conditional jump), and LOOP instructions require an operand that
refers to the label of an instruction.
Example:
FOR:
.
:
JMP FOR

THE JMP INSTRUCTION

Syntax:
[label:]

JMP short, near, or far address

This is an unconditional jump, which flushes the instruction fetch queue. Many jump
operations may slow processing speed. Some assemblers automatically generate a threebyte address operations. This can be stopped by specifying a SHORT operator in front of the
label to force a two-byte address. A JMP may be either backward or forward.

14

THE LOOP INSTRUCTION

Syntax:

[label:]

LOOP short-address

Example:
AGAIN:

MOV CX, 10 ;WHILE CX > 0


:
.
LOOP AGAIN ; -- CX; if CX > 0 JMP AGAIN

LOOP instruction is replaced with the following instruction applied sequentially.


DEC CX
JNE AGAIN
The LOOP instruction uses the value in the CX register. If the value in the CX register is zero
control falls through the LOOP instruction to the next instruction. If the value in the CX is
nonzero control jumps to the short-address. If the jump exceeds -128 to + 127 bytes a
'relative jump out of range" message is given.
See Figure 7-2 to see how the next instruction is calculated for a LOOP.

LOOPE and LOOPZ loop while equal or loop while zero while the content of the CX is zero or
the zero condition is set.

LOOPNE and LOOPNZ loop while not equal and loop while not zero as long as the value in
the CX is not zero or the zero condition is not set.
Note: Neither LOOP nor its LOOPxx variations affect any flags in the flag registers, which
would be change by other instructions within the loop routine. If this is the case a loop
routine contains no instruction that affects the ZF flag then using LOOPNE/LOOPNZ would be
equivalent to using LOOP.

15

CONDITIONAL JUMP INSTRUCTIONS

[label:]

Jnnn short-address

The type of jump used is dependent upon the data being compared. Signed and unsigned
data use different jumps.
The following conditional jump instructions have special uses. These instructions test the
carry, sign, overflow and zero bits in the flag registers.

The following conditional jump instructions apply to unsigned data.

16

The following conditional jump instructions apply to signed data.

Example : Write an assembly program that finds the sum of 1 to 100.


1. yol artl dallanma komutlar ile
.MODEL SMALL
.STACK 64
.DATA
result DW ?
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS, AX
MOV AX,00
MOV CX,100
AGAIN:
ADD AX, CX
DEC CX
JNE BAS; if CX is not zero goto AGAIN
MOV result, AX
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

;Exit program

2.yol LOOP komutu ile


.MODEL SMALL
.STACK 64
.DATA
result DW ?
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS, AX
MOV AX,00
MOV CX,100
AGAIN:
ADD AX, CX
LOOP BAS; if CX is not zero goto AGAIN
MOV result, AX
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

17

The following example find the sum of 5 to 100.


In this example we have to look at the condition. And so we use extra
cmp instruction for this purpose.
.
.MODEL SMALL
.STACK 64
.DATA
SONUC DW ?
.CODE
ANA PROC FAR
MOV AX,@DATA
MOV DS, AX
MOV AX,5
BAS:
ADD BX, AX
INC AX
CMP AX, 100
JBE BAS; AX 100 den kk ve eitken BAS a git
MOV SONUC, BX
MOV AH,4CH
INT 21H
ANA ENDP
END ANA

CMP instruction
The CMP instruction is used to compare two data fields. The result of the CMP operation
affects the AF, CF, OF, PF, SF, and ZF flags
CMP affect the Carry, Zero, Sign flag as shown below.

If AL=50, BL=60

CMP 50,60
If AL=50, BL=60
CMP 50,50
If AL=50, BL=40
CMP 50,40

18

CMP register/memory , register,memory,immediate

CMP register, register;

CMP memory, register;

CMP AX, BX

CMP SONUC, AX

CMP register, memory;

CMP memory, immediate;

CMP AX, SONUC

CMP SONUC,5

CMP register, immediate;

CMP memory, memory;

CMP AX,5

19

You might also like