Assembly Language Ab
Assembly Language Ab
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
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.
Examples :
KodSeg
DataSeg
StakSeg
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.
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.
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.
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.
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
;
.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
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
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.
PAGE 60,132
PAGE 60,132
;---------------------------------------------
;---------------------------------------------
NUM1 DB 45
NUM2 DB 30
DS:CODESG , ES:CODESG
RESULT DB ?
ORG 100H
DATASG ENDS
;---------------------------------------------
;----------------------------------------------
NUM1 DB 45
DB
32 DUP (0)
NUM2 DB 30
STACKSG ENDS
RESULT DB ?
;---------------------------------------------
;----------------------------------------------
MAIN
MAIN
PROC FAR
ASSUME CS:CODESG,
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
MAIN
ADD AL,BL
CODESG ENDS
MOV RESULT,AL
END BEGIN
MOV AH,4CH
INT 21H
MAIN
ENDP
ENDP
CODESG ENDS
END MAIN
11
;--------------------------------------------.MODEL SMALL
PAGE 60,132
;---------------------------------------------
.STACK 32
.MODEL SMALL
.DATA
.CODE
DB 45
ORG 100H
NUM2 DB 30
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
.COM
TASM example1.ASM
TASM example1.ASM
TLINK example.OBJ
TLINK/t example.OBJ
We get example.exe
We get example1.com
12
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
Syntax:
[label:]
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
Syntax:
[label:]
LOOP short-address
Example:
AGAIN:
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
[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.
16
;Exit program
17
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 AX, BX
CMP SONUC, AX
CMP SONUC,5
CMP AX,5
19