You are on page 1of 70

Lab 01

By: Dr Khakoo Mal


Why study Assembly Language?
• Oldest Programming Language
• Bears the closest resemblance to native machine Language
• Provide Direct access to computer hardware
• Help you to understand much about OS and Computer architecture
• You will learn the architecture of the Intel processor family
• Ideal for:
• Embedded systems
• Real time applications
• Device Drivers
What will you learn?
• the architecture of the Intel processor family from programmers point
of view
• Develop an understanding of how memory, address and instructions
work at a low level
Is Assembly Language Portable?
• No
• It is designed for a specific processor family
• Some well known processor families:
• Motorola 68x00, Intell x86, SUN Sparc, Vax and IBM-370
Why 8086?
• 16-bit microprocessor chip designed by Intel between
early 1976

• 8086 family of microprocessor consist of the


8086,8088, 80186,80188, 80286,80288, 80386SX,
80486, 80486SX

• 8086 and 8088 have the same basic instruction set

• This forms the basic instruction set for other processors

• 8086 microprocessor has 14 registers


Registers
• Data Registers
• AX
• BX
• CX
• DX
• Segment Registers
• CS
• DS
• SS
• ES
• Pointer and Index Register
• SI
• DI
• SP
• BP
• IP
• Flag Register
Intel 8086
Intel 8086
• Memory of 1MB in the 8086 architecture is
segmented/divided into different memory blocks

• There are total 16 blocks in 8086 architecture


• Four major memory blocks are

• Data Segment
• Code Segment
• Stack Segment
• Extra Segment
Segment Registers in 8086
Registers
Basic Elements
Statements
 Syntax:
name operation operand(s) comments
 name and comment are optional
 Number of operands depend on the instruction
 One statement per line
 At least one blank or tab character must separate the field.

 Each statement is either:


Instruction (translated into machine code)
Assembler Directive (instructs the assembler to perform some
specific task such as allocating memory space for a variable or
creating a procedure)

12
Statement Example
operation
Operands
Comment
Label ; Code
Here: mov ax,count ;store count into ax

MAIN PROC ;creates a procedure called MAIN

Name

Assembler Directive Comment

13
Operation Field: Symbolic operation (Op code)
 Symbolic op code translated into Machine Language op code
 Examples: ADD, MOV, SUB

 In an assembler directive, the operation field represents Pseudo-op


code

 Pseudo-op is not translated into Machine Language op code, it only


tells assembler to do something.

 Example: PROC pseudo-op is used to create a procedure

14
Operand Field

 An instruction may have zero, one or more operands.


 In two-operand instruction, first operand is destination,
second operand is source.

 Examples

NOP ;no operand, does nothing


INC AX ;one operand, adds 1 to the contents of AX
ADD ;two operands, adds value 2 to the contents of
AX, 2 AX

15
Comments
• Optional

• Marked by semicolon in the beginning

• Ignored by assembler

• Good practice

16
Program Data
 Processor operates only on binary data.
 In assembly language, you can express data in:
 Binary
 Decimal
 Hexadecimal
 Characters
 Numbers
 For Hexadecimal, the number must begin with a decimal digit.
E.g.: write 0ABCh not only ABCH.
 Characters enclosed in single or double quotes.
 ASCIIcodes can be used
No difference in “A” and 41h
ASCII CODE
Contd..

 Use a radix symbol (suffix) to select binary, octal,


decimal, or hexadecimal

6A15h ; hexadecimal
0BAF1h ; leading zero required
32q ; octal
1011b ; binary
35d ; decimal (default)

19
Variables
❑ Variables play the same role in assembly language that they do in
HLL

❑ Each variable has a data type and is assigned a memory address by


the program

❑ The table below shows the assembler directives that are used to
define the variables

Pseudo-op Stands for


DB Define Byte (1 Byte)
DW Define Word (2 Bytes)
DD Define Double Word( 4 Bytes)
DQ Define Quote (8 Bytes)
DT Define Ten bytes (10 Bytes)

14
Contd..
 Syntax
variable_name type initial_value
value1, value2, value3
variable_name
 Data Definition type
Directives Or Data Defining Pseudo-ops
 DB, DW, DD, DQ, DT

Data Definition Directives Values

myArray dw 1000h,2000h

Data name

21
Contd..
Pseudo-ops Description Bytes Examples

DB Define Byte 1 var1 DB ‘A’


Var2 DB ?
array1 DB 10, 20,30,40

DW Define Word 2 var2 DW ‘AB’


array2 DW 1000, 2000

DD Define Double Word 4 Var3 DD -214743648


Arrays
 Sequence of memory bytes or words
 Example 1:
B_ARRAY DB 10h, 20h, 30h

Symbol Address Contents

B_ARRAY 0200h 10h


B_ARRAY+1 0201h 20h
B_ARRAY+2 0202h 30h

*If B_ARRAY is assigned offset address 0200h by assembler

23
Example 2

W_ARRAY DW 1000, 40, 29887, 329


*If W_ARRAY is assigned offset address 0300h by assembler

Symbol Address Contents

W_ARRAY 0300h 1000d


W_ARRAY+ 2 0302h 40d
W_ARRAY+ 4 0304h 29887d
W_ARRAY+ 6 0306h 329d
Character String

LETTERS DB ‘ABC Is equivalent to


LETTERS DB 41h, 42h, 43h

 Possible to combine characters and numbers.


MSG DB ‘HELLO’, 0Ah, 0Dh, ‘$’
Is equivalent to
MSG DB 48h, 45h, 4Ch, 4Ch, 4Fh, 0Ah, 0Dh, 24h

25
Example 3
 Show how character string “RG 2z” is stored in
memory starting at address 0.
 Solution:
Address Character ASCII Code (HEX) ASCII Code (Binary)
[Memory Contents]
0 R 52 0101 0010
1 G 47 0100 0111
2 Space 20 0010 0000
3 2 32 0011 0010
4 z 7A 0111 1010

26
A Few Basic Instructions
MOV
 Transfer data
 Between registers
 Between register and a memory location
 Move a no. directly to a register or a memory location
 Syntax
MOV destination, source
 Example Before After
MOV AX, WORD1 AX 0006 0008

WORD1 0008 0008

28
Legal Combinations of Operands for MOV

Destination Operand Source Operand Legal

General Register General Register YES

General Register Memory Location YES

General Register Segment Register YES

General Register Constant YES

Memory Location General Register YES

Memory Location Memory Location NO

Memory Location Segment Register YES

Memory Location Constant YES

29
XCHG
 Exchange the contents of
 Two registers
 Register and a memory location
 Syntax
XCHG destination, source
 Example
XCHG AH, BL
Before After

1A 00 05 00

AH AL AH AL

00 05 00 1A
BH BL BH BL
30
Legal Combinations of Operands for XCHG

Destination Operand Source Operand Legal

General Register General Register YES


General Register Memory Location YES

Memory Location General Register YES


Memory Location Memory Location NO
ADD Instruction
 To add contents of:
 Two registers
 A register and a memory location
 A number to a register
 A number to a memory
location
 Example
ADD WORD1, AX

Before After

AX 01BC 01BC

WORD1 0523 06DF

32
SUB Instruction
 To subtract the contents of:
 Two registers
 A register and a memory location
 A number from a register
 A number from a memory
location
 Example
SUB AX, DX

Before After

AX F9AB 25E3

DX D3C8 D378

33
Legal Combinations of Operands for
ADD & SUB instructions

Destination Operand Source Operand Legal


General Register General Register YES

General Register Memory Location YES

General Register Constant YES

Memory Location General Register YES

Memory Location Memory Location NO

Memory Location Constant YES

34
Contd..
ADD BYTE1, BYTE2 ILLEGAL instruction
Solution?
MOV AL, BYTE2
ADD BYTE1, AL

35
INC & DEC
 INC (increment) instruction is used to 1 to the
add contents of a register or memory location.
 Syntax: INC destination
 Example: INC WORD1

 DEC (decrement) instruction is used to subtract 1


from
the contents of a register or memory location.
 Syntax: DEC destination
 Example: DEC CX

 Destination can be 8-bit or 16-bits wide.


36Destination can be a register or a memory location.
Contd..
INC WORD1

Before After

WORD1 0002 0003

DEC BYTE1

Before After

BYTE1 FFFE FFFD

37
NEG
 Used to negate the contents of destination.
 Replace the contents by its 2’s complement.
 Syntax
NEG destination
 Example
NEG BX

Before After

BX 0002 FFFE

38
Program Structure
Program Segments
 Machine Programs consists of
 Code
 Data
 Stack
 Each part occupies a memory segment.
 Same organization is reflected in an assembly language
program as Program Segments.
 Each program segment is translated
into a memory segment by the assembler.

36
Memory Models
 Determines the size of data and code a program can have.
 Syntax:
.MODEL memory_model

Model Description

SMALL code in one segment, data in one segment

MEDIUM code in more than one segment, data in one

segment COMPACT code in one segment, data in more than

LARGE
one segment Both code and data in more than one segments
No array larger than 64KB

HUGE Both code and data in more than one segments


37 array may be larger than 64KB
Data Segment
 All variable definitions
 Use .DATA directive
 Example:

.DATA
WORD1 DW 2
BYTE1 DB
10h

42
Stack Segment
 A block of memory to store stack
 Syntax
.STACK size
 Where size is optional and specifies
the stack area size in bytes
 If size is omitted, 1 KB set aside for stack area

 For example:
.STACK 100h

43
Code Segment

• The code segment contains a


program’s instructions.
.CODE name
• Inside a code segment, instructions are
organized as procedures.
name PROC
; body of the procedure
name ENDP
• The last line in the program should be the
END directive, followed by name of the
main procedure.

4
MAIN PROC
; instructions go here
MAIN ENDP
; other procedures go
here

4
5
.MODEL SMALL
.STACK 100H
.DATA
; data definitions go here
.CODE
MAIN PROC
; instructions go here MAIN
ENDP
; other procedures go here END MAIN

4
• CPU communicates with the
peripherals through IO ports
– IN and OUT instructions to access the
ports directly

4
7
• I/O service routines
▪ The Basic Input/Output System (BIOS)
routines
▪ The DOS routines
• The INT (interrupt) instruction is
used to invoke a DOS or BIOS
routine.

4
• INT 21h may be used to invoke a large
number of DOS functions.
• A particular function is requested by
placing a function number in the AH
register and invoking INT 21h.

5
Input:
AH = 1

Output:
AL = ASCII code if character key is
pressed
= 0 if non-character key is pressed

5
MOV AH, 1 ; input key function
INT 21h ; ASCII code in AL

5
Input:
AH = 2
DL = ASCII code of the display
character or control character

5
• MOV AH, 2 ; display character
function
MOV DL, ‘?’ ; character is ‘?’
INT 21h ; display character

5
ASCII Code HEX Symbol Function
7 BEL beep
8 BS backspace
9 HT tab
A LF line feed (new line)
D CR carriage return (start of
current line)

5
5
5
• An editor is used to create the preceding
program.
• The .ASM is the conventional extension
used to identify an assembly language
source file.

6
Step:02 Assemble the Program

• The Microsoft Macro Assembler (MASM) is


used to translate the source file (.ASM file) into
a machine language object file (.OBJ file).

• MASM checks the source file for syntax errors.

• If it finds any, it will display the line number of


each error and a short description.

• C:\>MASM File_Name;
6
• The Link program takes one or more
object files, fills in any missing
addresses, and combines the object
files into a single executable file (.EXE
file)
• This file can be loaded into memory
and run.
• C:\>LINK File_Name;

6
• To run it, just type the run file
name.
• C:\>File_Name

6
Next …
• Assembly Language Programming Tools
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 …
• MASM (Macro Assembler from Microsoft)
• NASM (Netwide Assembler for both Windows and Linux), and
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
Assemble and Link Process
Source Object
File Assembler File

Source Object Executable


File Assembler File Linker
File

Link
Source Object
Assembler Libraries
File File

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
Editor
• Allows you to create assembly language source files
• Some editors provide syntax highlighting features and can be
customized as a programming environment
Environment Setting
• Go to our course website
• In lab folder, download software.rar
• Install the dosbox, by just clicking NEXT
• Go to C drive
• Create a folder with the name “masm”
• Copy the masm.rar and paste in masm folder
• Extract the .rar file
Mount Instructions
• Go to C:\Program Files (x86)\DOSBox-0.74
• Open DOSBox 0.74 Options file
Now Run the Dosbox
First Program
.model small
.stack 100h
.data
.code
main Proc
Mov ah,02
Mov dl,’?’
Int 21h
Mov ah,04ch
main Endp
End main
• Now save it as First.asm
• Exit the editor
• Masm first.asm;
• Link first.obj;
• Run it by name only - first
References
• Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2014.

You might also like