introduction to ibm pc assembly language

82
06/08/22 CAP221 1 INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

Upload: tia

Post on 06-Jan-2016

108 views

Category:

Documents


16 download

DESCRIPTION

INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE. Assembly Language Syntax. An assembly language program consists of statements. The syntax of an assembly language program statement obeys the following rules:. RULES. Only one statement is written per line - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2211

INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

Page 2: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2212

Assembly Language Syntax

• An assembly language program consists of statements.

• The syntax of an assembly language program statement obeys the following rules:

Page 3: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2213

RULES

Only one statement is written per line Each statement is either an instruction or

an assembler directive instruction is translated into machine

codeassembler directive instructs the

assembler to perform some specific task

Page 4: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2214

Program Statement

• The general format for an assembly language program statement is as follows:

name operation operand’(s) comment

Examples:

START: MOV CX,5 ; initialize counter

MAIN PROC

Page 5: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2215

Name Field

• This field is used for:

instruction label: if present, a label must be followed by a colon (:)

procedure names variable names.

Page 6: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2216

Name Field

Assembler translates names into memory addresses.

Names can be from 1 to 31 characters long: (letters, digits, and special characters: ?, ., _, $, @, %)

Embedded blanks are not allowed, names may not begin with a digit, period (if used) must be the first character

Page 7: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2217

Name Field

Examples:

Legal names Illegal names COUNTER1 2ABC

@CHARACTER TWO WORDS

$500 A45.26

SUM_OF_DIGITS YOU&ME

.TEST

DONE?

Page 8: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2218

Operation Field

For an instruction

• This field consists of a symbolic operation code, known as opcode

• The opcode describes the operation’s function

• Symbolic opcodes are translated into machine language opcode.

Page 9: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP2219

Operation Field

For an assembler directive

• This field consists of a pseudo-operation code (pseudo-op)

• pseudo-ops tell assembly to do something

Page 10: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22110

Operand Field

For an instruction• This field specifies data to be acted on. It may

have one, two or no operands at all. • Examples of instructions with different operand

fields NOP ; Instruction with no operand field INC AX ; Instruction with one operand field ADD AX, 2 ; Instruction with two operand fieldIf 2 operands: the first is destination, the second

is the source operand

Page 11: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22111

Operand Field

For an assembler directive

• This field contains more information about the directive

Page 12: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22112

Comment Field

• A semicolon marks the beginning of a comment

• A semicolon in the beginning of a line makes it all a comment line

• Good programming practice dictates the use of a comment on almost every line.

Page 13: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22113

Key rules for the use of comments

• Do not say something that is obvious

• Put instruction in context of program

Page 14: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22114

Comment Field

Examples of good and bad Comments

MOV CX , 0 ; Move 0 to CX (This is not a good

comment.)

MOV CX , 0 ; CX counts terms, initially set to 0

(This is a good comment.)

Page 15: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22115

Numbers

• Binary number is written as a bit string followed by the letter `b`.

• decimal number is written as a string of decimal digits followed by the letter `d`.

• Hex number is written as a string of hex digits followed by the letter `h`.

• Hex number must begin with a decimal digit• Numbers may have an optional sign

Page 16: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22116

Numbers

Examples:

number type 1010 decimal 1010B binary -2134D decimalABFFH illegal0ABFFH hex1BHH illegal1BFFH hex1,23 illegal

Page 17: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22117

Characters

• Characters and character segments must be enclosed in single or double quotes; ‘A' , “hello“.

• Assembler translates characters to their ASCII code

Page 18: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22118

Variables

Declaring Integer Variables:

• An integer is a whole number, such as 4 or 4444. Integers have no fractional part. Integer variables can be initialized in several ways with the data allocation directives.

Page 19: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22119

Variables

Allocating Memory for Integer Variables:

• When an integer variable is declared, the assembler allocates memory space for the variable. The variable name becomes a reference to the memory space allocated to that variable.

Page 20: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22120

Syntax

name directive initializer initial value

Page 21: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22121

Variables

Pseudo-op type size range

• DB unsigned 1 byte 0 to 255. signed 1 byte -128 to +127. • DW unsigned 2 bytes 0 to 65,535 (64K). signed 2 bytes -32,768 to +32,767. • DD unsigned 4 bytes 0 to 4,294,967,295 (4 Mbytes).

signed 4 bytes -2,147,483,648 to +2,147,483,647.

• DQ 8-byte integer 4 consecutive words

• DT 10-byte integer 10 consecutive bytes

Page 22: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22122

Byte variables

• Syntax:

Name DB initial value

Examples:

ALPHA DB 4

BYT DB ?

Page 23: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22123

Word variables• Syntax:

Name DW initial value

Example:

WRD DW -2• The assembler stores integers with the least

significant byte in the lowest address of the memory area allocated to the integer

Example:

WD DW 1234H

low byte WD contains 34h, high byte contains 12h

Page 24: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22124

Array Declaration  

• An array is a sequential collection of variables, all of the same size and type

• Array elements occupy contiguous memory locations.

• The program references each element relative to the start of the array.

• An array is declared by giving it a name, a type, and a series of initializing values or placeholders (?).

Page 25: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22125

Array Examples

B_ARRAY DB 10, 25, 20

If array starts at offset address 0200h, it will look like this:

Symbol Address ContentsB-ARRAY 0200H 10B-ARRAY+1 0200H+1 25B-ARRAY+2 0200H+2 20

Page 26: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22126

Array Examples

W_ARRAY DW 0FFFFh, 789Ah, 0BCDEh

If array starts at offset address 0100h, it will look like this:

Symbol Address ContentsW_ARRAY 0100H FFFFHW_ARRAY+2 0102H 789AHW_ARRAY+4 0104H BCDEH

Page 27: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22127

Character strings

• An array of characters can be initialized by a string of characters.

• Inside a string, the assembler differentiates between upper and lower cases (different ASCII codes).

• It is possible to combine characters and numbers in one definition

Page 28: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22128

Character strings

Examples:

1)

LETTERS DB ‘AaBCbc‘

Is equivalent to

LETTERS DB 41H,61H,42H,43H,62H,63H

2)

MSG DB ‘ABC‘,0AH,0DH,‘$‘

Is equivalent to

MSG DB 41H,42H,43H,0AH,0DH,24H

Page 29: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22129

Constant Declaration  

• In an assembly language program, constants are defined through the use of the EQU directive.

• Syntax:

Name EQU constant

The EQU directive is used to assign a name to a constant.

Use of constant names makes an assembly language easier to understand.

No memory is allocated for a constant. The symbol on the right of EQU cab also be a string

Page 30: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22130

Constant Declaration

Examples:1)

LF EQU 0AH ; LF can be used in place of 0Ah

MOV DL LFMOV DL 0AH

2)PMT EQU ‘TYPE YOUR NAME‘ ;

instead of

MSG DB ‘TYPE YOUR NAME‘

We can use

MSG DB PMT

Have the same machine code

Page 31: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22131

BASIC INSTRUCTIONS

MOV and XCHG

Page 32: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22132

MOV instruction

• Is used to transfer data :– between registers,– between a register & a memory location.

Or– To move a number directly into a register or

memory location.

Page 33: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22133

Syntax

MOV destination , sourceExample:

MOV AX , WORD1

This reads “ Move WORD1 to AX “

The contents of register AX are replaced by the contents of the memory location WORD1.

Page 34: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22134

Mov AX , WORD1

Before After

AX AX

WORD1 WORD1

0006 0008

0008 0008

Page 35: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22135

MOV AX , BX

• AX gets what was previously in BX , BX is unchanged.

Page 36: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22136

MOV AH , ‘A’

• This is a move of the 041h ( the ASCII code of “A” ) into register AH.

• The previous value of AH is overwritten

( replaced by new value )

Page 37: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22137

XCHG instruction

• (Exchange) operation is used to exchange

the contents of

– two registers, or – a register and a memory location

Page 38: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22138

Syntax

XCHG destination , source

Page 39: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22139

Example

XCHG AH , BL

This instruction swaps the contents of AH and BL.

Page 40: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22140

XCHG AH , BL

Before After

AH AL

1A

AH AL

BH BL

BH BL

00 05 00

00 05 00 1A

Page 41: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22141

Example

XCHG AX , WORD1

• This swaps the contents of AX and memory location WORD1.

Page 42: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22142

Restrictions on MOV & XCHG

Source Operand

General Register

Segment Register

Memory Location

Constant

General Registeryesyesyesno

Segment Registeryesnoyesno

Memory Locationyesyesnono

Constantyesnoyesno

MOV Destination Operand

Page 43: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22143

Restrictions on MOV & XCHGXCHG Destination Operand

Source Operand

General Register

Memory Location

General Registeryesyes

Memory Locationyesno

Page 44: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22144

Restrictions on MOV & XCHG

Example :

ILLEGAL : MOV WORD1 , WORD2

LEGAL:

MOV AX , WORD2

MOV WORD1 , AX

Page 45: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22145

ADD & SUB

• Are used to add & subtract the contents of

– two registers,– a register & memory location , or– add ( subtract ) a number to ( from ) a

register or a memory location.

Page 46: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22146

Syntax

ADD destination , source

SUB destination , source

Page 47: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22147

Example

ADD WORD1 , AX

This instruction , “ Add AX to WORD1 “ , causes the contents of AX & memory word WORD1 to be added, and the sum is stored in WORD1. AX is unchanged.

Page 48: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22148

ADD WORD1 , AX

Before After

AX AX

WORD1 WORD1

01BC 01BC

0523 06DF

Page 49: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22149

Example

SUB AX , DX

This instruction , “ Subtract DX from AX “ , the value of DX is subtracted from the value of AX , with the difference being stored in AX. DX is unchanged.

Page 50: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22150

SUB AX , DX

Before After

AX AX

DX DX

0000 FFFF

0001 0001

Page 51: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22151

Example

ADD BL , 5

This is an addition of the number 5 to the contents of register BL.

Page 52: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22152

Legal combinations of operands for

ADD & SUB

Source OperandGeneral RegisterMemory location

General Registeryesyes

Memory locationyesno

Constantyesyes

Destination operand

Page 53: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22153

ILLEGAL

ADD BYTE1 , BYTE2

Solution :

move BYTE2 to a register before adding

MOV AL , BYTE2 ; AL gets BYTE2

ADD BYTE1 , AL ; add it to BYTE1

Page 54: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22154

INC ( increment )

Is used to add 1 to the contents of a

• Register or

• Memory location

Page 55: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22155

DEC ( decrement )

Is used to subtract 1 from the contents of a

• Register or

• Memory location

Page 56: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22156

Syntax

INC destination

DEC destination

Page 57: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22157

Example

INC WORD1

adds 1 to the contents of WORD1

Page 58: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22158

INC WORD1

0002 0003

Before After

WORD1 WORD1

Page 59: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22159

Example

DEC BYTE1

subtracts 1 to the variable BYTE1

Page 60: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22160

DEC BYTE1

FE FD

Before After

BYTE1 BYTE1

Page 61: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22161

NEG

• Is used to negate the contents of the destination.

• It does this by replacing the contents by its two’s complement.

Page 62: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22162

Syntax

NEG destination

The destination may be a

register or

memory location.

Page 63: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22163

NEG BX

0002 FFFE

Before After

BX BX

Page 64: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22164

Type agreement of operands

• For instruction with 2 operand, the two operands must be of the same type; that is, both words or bytes.

• Illegal …. MOV AX , BYTE1 …. Is not allowed.

• Assembler will accept both the following instructions :

MOV AH , ‘A’ ….. moves 41h into AH

MOV AX , ‘A’ ….. moves 0041h into AX

Page 65: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22165

Translation of HLL to Assembly Language

Statement Translation B = A MOV AX , A ; moves A into AX

MOV B , AX ; and then into B

WHYBecause direct memory – memory move is illegal we

must move the contents of A into a register before moving it to B.

Page 66: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22166

Translation of HLL to Assembly Language

Statement Translation

A = 5 – A

MOV AX , 5 ; put 5 in AX

SUB AX , A ; AX…. 5 – A

MOV A , AX ; put it in A

There is another shorter way :

Page 67: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22167

NEG A ; A = - A

ADD A , 5 ; A = 5 - A

Page 68: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22168

Translation of HLL to Assembly Language

Statement Translation

A = B – 2 * A MOV AX , B ; AX has B

SUB AX , A ; AX has B – ASUB AX , A ; AX

has B – 2 * A

MOV A , AX ; move results to B

Page 69: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22169

Program Structure

• Machine language programs consist of :

– Codes,

– Data, and

– Stack.Each part occupies a memory segment. They

are structured as program segments. Each program segment is translated into a memory segment by the assembler.

Page 70: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22170

Memory Models

The size of the code & data a program can have is determined by specifying a memory model using the . MODEL directive.

Page 71: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22171

Syntax

. MODEL memory_mode1

SMALL MEDUIM COMPACT

LARGE

Code in one segment

Data in one segment

Code in more than one segment

Data in one segment

Code in one segment

Data in more than one segment

Code in more than one segment

Data in more than one segment

No array larger than 64K bytes.

Page 72: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22172

• Unless there is a lot of code or data, the appropriate model is SMALL.

• . MODEL directive should come before any segment definition.

Page 73: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22173

Data Segment

• A program’s data segment contains all the variable definitions. Constant definitions are made here as well, but they may be placed elsewhere in the program since no memory allocation is involved.

• We use the . DATA directive followed by variable & constant declarations.

• Variable addresses are computed as offsets from the start of this segment

Page 74: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22174

Example

.DATA

WORD1 DW 2

WORD2 DW 5

MSG DB ‘ This is a message ‘

MASK EQU 10010010B

Page 75: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22175

Stack Segment

• Used to set aside storage for the stack • Stack addresses are computed as offsets into

this segment • Use: .stack followed by a value that indicates

the size of the stack

Page 76: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22176

Declaration Syntax

.STACK size

An optional number that specifies the stack area size in bytes.

Page 77: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22177

Example

.STACK 100 H

Sets aside 100h bytes for the stack area ( a reasonable

size for most applications ) .

If size is omitted , 1 KB is set aside for the stack area.

Page 78: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22178

Code Segment

• It contains a program’s instructions.

Page 79: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22179

Syntax

.CODE name

Optional name for the segment

Why??

there is no need for a name in a SMALL program

The assembler will generate an error

Page 80: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22180

Inside the code segment

• Instructions are organized as procedures.

• The simplest procedure definition is :

name PROC

; body of the procedure

name ENDP

name is the name of the procedure, PROC and ENDP are pseudo-op that delineate the procedure

Page 81: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22181

Example

.CODE

MAIN PROC

; main procedure body

MAIN ENDP

; other procedures go here

Page 82: INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE

04/20/23CAP22182

Program Structure

• A program has always the following general structure:

.model small ;Select a memory model

.stack 100h ;Define the stack size

.data; Variable and array declarations; Declare variables at this level

.codemain proc

; Write the program main code at this levelmain endp

;Other Procedures; Always organize your program into procedures

end main ; To mark the end of the source file