Vous êtes sur la page 1sur 44

Copyright Dorling Kindersley India Pvt Ltd

PROGRAMMING
CONCEPTS 1

CHAPTER 2

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

ASSEMBLY LANGUAGE
PROGRAMMING
To code efficiently in assembly language for a
particular processor, the prerequisites are
a good knowledge of the internal architecture
of the processor and addressing modes

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

The Assembly Process


An assembler is a translator that translates
source instructions (in symbolic language)
into target instructions (in machine language)
on a one-to-one basis

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Features of assemblers
Labels are used for memory addresses
Labels are used for constants
Macros are allowed

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Instructions and Directives


Instructions are executable statements
Directives are non-executable
Directives are also called pseudo instructions.
Directives aid the assembly process.

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

What the assembler does


It takes the source code (in assembly

language) and
converts it to the object code in machine
language

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

ASSEMBLERS FOR x86


NASM, FASM, MASM, TASM and HLA
FASM and NASM can run under DOS, Linux

and Windows
TASM and MASM are very popular
It is found now that Windows 7, a new
64-bit operating system, does not
directly support 16-bit programs so 16
bit assemblers may not work with
Windows 7 directly
The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

DOS
DOS stands for Disk Operating Systems. It

could be an acronym for any OS, but it is most


often used as a shorthand for MS-DOS
(Microsoft Disk Operating Systems).

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

BIOS and DOS function calls


A set of functions for using I/O devices
Can be used for displaying data
Can be used for inputting data from the

keyboard

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

The .EXIT command is a shorthand notation

for
MOV AH ,4CH
INT 21H

The x86 Microprocessor - Lyla B Das

10

i) Read the keyboard with echo

MOV AH, 01
INT 21H
This call exits with the ASCII value of the key
pressed, being available in AL .
The key pressed is also echoed on the screen
.

The x86 Microprocessor - Lyla B Das

11

Copyright Dorling Kindersley India Pvt Ltd

DOS Function Call

Copyright Dorling Kindersley India Pvt Ltd

DOS Function Call Contd.


ii) Read Keyboard without echo

MOV AH, 08
INT 21H
This call exits with the ASCII value of the key
pressed being available in AL .
The key pressed is not echoed on the
screen

The x86 Microprocessor - Lyla B Das

12

iii) Write a character to the standard display

unit.
For this ,the ASCII value of the character to be
displayed should be in DL
MOV DL, S

MOV AH, 02
INT 21H

The x86 Microprocessor - Lyla B Das

13

Copyright Dorling Kindersley India Pvt Ltd

Displaying a character

Copyright Dorling Kindersley India Pvt Ltd

STRING DISPLAY
iv) Display a character string on the standard

display unit
The logical address DS:DX should point to the
beginning of the string .
This is to be followed by the following
instructions
MOV AH, 09
INT 21H

The x86 Microprocessor - Lyla B Das

14

Copyright Dorling Kindersley India Pvt Ltd

Why MASM ?
Microsoft has written considerable

documentation
Third parties have written assembly language
reference manuals for MASM
The versions of MASM 6.0 and above have a
lot more features (aimed at simplification of
writing code) than previous versions

The x86 Microprocessor - Lyla B Das

15

Copyright Dorling Kindersley India Pvt Ltd

Here we use
MASM 6.14, which is DOS based
In this, DOS and BIOS interrupts can be used
MASM32, which is windows based
In this, DOS and BIOS interrupts cannot be

used.

The x86 Microprocessor - Lyla B Das

16

Copyright Dorling Kindersley India Pvt Ltd

ASSEMBLY LANGUAGE
PROGRAMMING STEPS
Write the code with the help of an editor
Open the DOS window
Go to the MASM and then to the BIN directory
Assemble the code
Link the code
Run the executable file

The x86 Microprocessor - Lyla B Das

17

Copyright Dorling Kindersley India Pvt Ltd

Memory Models
For defining segments
Dot models have short cuts
The tiny model is used when only one

segment is needed
To specify a segment, write
. MODEL MODEL NAME

The x86 Microprocessor - Lyla B Das

18

Copyright Dorling Kindersley India Pvt Ltd

Example 2.1 The Tiny Model


.MODEL TINY
.CODE
.STARTUP
MOV AL,67H
MOV BL,45H
ADD AL,BL
MOV DL,AL
.EXIT
END
The x86 Microprocessor - Lyla B Das

19

Copyright Dorling Kindersley India Pvt Ltd

Save it as tiny.asm
Open MASM and go to the BIN directory.

Use the following commands


ml tinym.asm
;for assembling and linking
ml/Fl tinym.asm ;for the list file

The x86 Microprocessor - Lyla B Das

20

Copyright Dorling Kindersley India Pvt Ltd

The result of assembling and


linking
C:\masm6.14\BIN>ml tinym.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.
Assembling: tinym.asm
Microsoft (R) Segmented Executable Linker Version 5.60.339
Dec 5 1994
Copyright (C) Microsoft Corp 1984-1993. All rights reserved.
Object Modules [.obj]: tinym.obj /t
Run File [tinym.com]: "tinym.com"
List File [nul.map]: NUL
Libraries [.lib]:
Definitions File [nul.def]:

The x86 Microprocessor - Lyla B Das

21

Copyright Dorling Kindersley India Pvt Ltd

Example 2.3 Part of the List file


0000
0100
0102
0104
0106

B0 67
B3 45
02 C3
8A D0

.MODEL TINY
.CODE
MOV AL,67H
MOV BL,45H
ADD AL,BL
MOV DL,AL
.EXIT
END
The x86 Microprocessor - Lyla B Das

22

Copyright Dorling Kindersley India Pvt Ltd

Short cuts in the model


.EXIT
.STARTUP

The working of all these can be obtained by


using the .listall directive in the program
.EXIT gets translated to
MOV AH,4CH
INT 21H

The x86 Microprocessor - Lyla B Das

23

The x86 Microprocessor - Lyla B Das


24
Copyright Dorling Kindersley India Pvt Ltd

Copyright Dorling Kindersley India Pvt Ltd

Using the debugger


To enter the debugger, type debug

tinym.com
We get an underscore as the prompt
On typing r, we can see the contents of the
registers, before execution of the program
Now type u, which is the command for
unassembling

The x86 Microprocessor - Lyla B Das

25

Copyright Dorling Kindersley India Pvt Ltd

Example 2.5
C:\masm6.14\BIN>debugtinym.com
r
AX=0000BX=0000CX=010CDX=0000SP=0000BP=0000
SI=0000DI=0000
DS=13ADES=13ADSS=13BDCS=13BDIP=0100NVUPEIPL
NZNAPONC

13BD:0100B067MOVAL,67
u
13BD:0100B067MOVAL,67
13BD:0102B345MOVBL,45
13BD:010402C3ADDAL,BL
13BD:01068AD0MOVDL,AL
13BD:0108B44CMOVAH,4C
13BD:010ACD21INT21
The x86 Microprocessor - Lyla B Das

26

Copyright Dorling Kindersley India Pvt Ltd

COM and EXE files


A tiny model generates only a com file,

while any other memory model generates an


.exe file

The x86 Microprocessor - Lyla B Das

27

Copyright Dorling Kindersley India Pvt Ltd

Features of a com file

Size is limited to 64K


Only one segment, which is the code
segment
Data is defined in this code segment
Code starts at offset 0100 H,
Smaller file compared to .exe files

The x86 Microprocessor - Lyla B Das

28

Copyright Dorling Kindersley India Pvt Ltd

Example 2.7

The x86 Microprocessor - Lyla B Das

29

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Data definitions in MASM Table


2.1

30

Copyright Dorling Kindersley India Pvt Ltd

The small model


Here two segments are used

A code segment and a data segment

The x86 Microprocessor - Lyla B Das

31

Copyright Dorling Kindersley India Pvt Ltd

Example 2.9a

The x86 Microprocessor - Lyla B Das

32

The x86 Microprocessor - Lyla B Das

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Example 2.9b

33

The x86 Microprocessor - Lyla B Das

34

Copyright Dorling Kindersley India Pvt Ltd

Figure 2.1 Data segment with


labels and off sets corresponding to
Example 2.9

Copyright Dorling Kindersley India Pvt Ltd

The DUP Directive


Used to replicate a given number of

characters
NUMS DB 10 DUP(0) fills up with 0s the
10 byte locations starting with the label
NUM
Other examples
STARS DB 5 DUP(#)
BLANK DB 10DUP(?)
WRDS DW 4 DUP(FF0FH)

The x86 Microprocessor - Lyla B Das

35

Used to equate names to constants


The assembler just replaces the names by the

values mentioned
Examples:
TEMP
PRICE

EQU 34
EQU 199

The x86 Microprocessor - Lyla B Das

36

Copyright Dorling Kindersley India Pvt Ltd

The EQU Directive

Copyright Dorling Kindersley India Pvt Ltd

Example 2.10

The x86 Microprocessor - Lyla B Das

37

ORG is a directive which means origin


In the context of assembly language

programming, it can change the location of


storage of data or code in memory

The x86 Microprocessor - Lyla B Das

38

Copyright Dorling Kindersley India Pvt Ltd

The ORG Directive

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Example 2.11a

39

Copyright Dorling Kindersley India Pvt Ltd

Example 2.11b

The x86 Microprocessor - Lyla B Das

40

Copyright Dorling Kindersley India Pvt Ltd

Table 2.2 Other Models

The x86 Microprocessor - Lyla B Das

41

The x86 Microprocessor - Lyla B Das

42

Copyright Dorling Kindersley India Pvt Ltd

Example 2.12 Full segment model

1. All labels should begin with a letter or the


special characters @,$ , _ and ?
2. A label can have 1 to 31 characters which
may be digits ,letters in upper or lower case
or the special characters at (@),dollar($) ,
underscore (_) ,dot(.) or question mark(?)
3. No reserved words of MASM may be used
4. Each label must be unique

The x86 Microprocessor - Lyla B Das

43

Copyright Dorling Kindersley India Pvt Ltd

General rules for writing assembly


language

Copyright Dorling Kindersley India Pvt Ltd

Example 2.13

The x86 Microprocessor - Lyla B Das

44

Vous aimerez peut-être aussi