1 of 50

CPE 408330� Assembly Language and�Microprocessors��Chapter 7: Assembly Language �Program Development with MASM�����

[Computer Engineering Department,

Hashemite University, © 2008]

2 of 50

Lecture Outline

  • 7.1 Statement Syntax for a Source Program
  • 7.2 Assembler Directives
  • 7.3 Creating a Source File with an Editor
  • 7.4 Assembling and Linking Programs
  • 7.5 Loading and Executing a Run Module

2

CPE 0408330 @ 2008

S. Abed - HU, Jordan

3 of 50

7.1 Statement Syntax for a Source�Program

􀂄 Assembly language statement syntax

􀂄 Directive statement syntax

􀂄 Constants in a statement

􀂄 Operand expressions using the arithmetic, relational, and logical operators

􀂄 Value-returning and attribute operators

3

CPE 0408330 @ 2008

S. Abed - HU, Jordan

4 of 50

7.1 Statement Syntax for a Source�Program

  • The source program is a series of assembly language and directive statements that solves a specific problem.
    • The assembly language statements tell the microprocessor the operations to be performed, and
    • The directive statements instruct the assembler program on how the program is to be assembled.
  • Syntax: the rules that must be used when writing assembly language and directive statements for MASM.

4

CPE 0408330 @ 2008

S. Abed - HU, Jordan

5 of 50

7.1 Statement Syntax for a Source�Program

􀂄 Assembly language statement syntax:

LABEL: OPCODE OPERAND(S) ;COMMENT(S)

  • EXAMPLES:

START: MOV CX, 10 ;Load a count of 10 into register CX

MOV CX, 10 ;Initialize the count in CX

CLC ;Clear the carry flag

*. Leave one blank space between fields and one space before the opcode if no label is used.

5

CPE 0408330 @ 2008

S. Abed - HU, Jordan

6 of 50

7.1 Statement Syntax for a Source�Program

6

CPE 0408330 @ 2008

S. Abed - HU, Jordan

Symbol

Register

DL

Data register low byte

DH

Data register high byte

DX

Data register

CL

Counter register low byte

CH

Counter register high byte

CX

Counter register

BL

Base register low byte

BH

Base register high byte

BX

Base register

AL

Accumulator register low byte

AH

Accumulator register high byte

AX

Accumulator register

Symbol

Register

ES

Extra segment register

SS

Stack segment register

DS

Data segment register

CS

Code segment register

BP

Base pointer register

SP

Stack pointer register

DI

Destination index register

SI

Source index register

Symbols used for specifying register operands

􀂄 Assembly language statement syntax

7 of 50

7.1 Statement Syntax for a Source Program

7

CPE 0408330 @ 2008

S. Abed - HU, Jordan

Addressing mode

Operand

Example

Segment

Register

Destination

MOV AX, BX

-

Immediate

Source

MOV AL, 15H

-

Direct

Destination

MOV [15] , AX

Data

Register indirect

Source

MOV AX, [SI]

Data

MOV AX, [BP]

Stack

MOV AX, [DI]

Data

MOV AX, [BX]

Data

Based

Destination

MOV [BX]+DISP, AL

Data

MOV [BP]+DISP, AL

Stack

Indexed

Source

MOV AL, [SI]

Data

MOV AL, [DI]

Data

Based indexed

Destination

MOV [BX][SI]+DISP, AH

Data

MOV [BX][DI]+DISP, AH

Data

MOV [BP][SI]+DISP, AH

Stack

MOV [BP][DI]+DISP, AH

Stack

􀂄 Assembly language statement syntax

8 of 50

7.1 Statement Syntax for a Source�Program

8

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Directive statement syntax:

LABEL: DIRECTIVE OPERAND(S) ; COMMENT (S)

EXAMPLES:

DB FFH ; Allocate a byte location initialized to FFH

DB FFH, FFH, FFH, FFH, FFH ; load next 5 bytes

(DB stands for Define Bytes)

  • DB: causes the next byte location in memory to be loaded with the given value.
  • DB: used to initialize memory locations with data.

9 of 50

7.1 Statement Syntax for a Source�Program

9

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Constants in a statement

􀂙 Constants in an instruction or directives can be expressed in any of many data types such binary, decimal, hexadecimal, octal, and character data types.

􀂙 The first digit of a hexadecimal number must be one of the numbers in the range 0 through 9.

􀂙 Typically, data and addresses are expressed in hexadecimal form, and the count for shift, rotate, and string instructions in commonly expressed in decimal form.

􀂙2’s complement of the number must be used for negative numbers expressed in binary, hexadecimal, or octal form.

10 of 50

7.1 Statement Syntax for a Source�Program

10

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Constants in a statement

  • Can use Decimal
    • Mov AL, 10 or Mov AL, 10D. Use Neg: Mov AL, -10
  • Can use Hex
    • Mov AL, 10H
  • Can use Binary
    • Mov AL, 10B
  • Can use Octal
    • Mov AL, 10Q
  • Can use Char
    • Mov AL, “A”

11 of 50

7.1 Statement Syntax for a Source�Program

  • EXAMPLE

The repeat count in CX for a string instruction is to be equal to decimal 255. Assume that the instruction that is to load the count has the form

MOV CX, XX

where XX stands for the count, an immediate operand that is to be loaded into CX. Show how the instruction should be written.

  • Solution:

MOV CX, 255D

or MOV CX, 255

or MOV CX, 0FFH

11

CPE 0408330 @ 2008

S. Abed - HU, Jordan

12 of 50

7.1 Statement Syntax for a Source�Program

  • EXAMPLE

The count in a move instruction that is to load CX is to be -10. Write the instruction and express the immediate operand in binary form.

  • Solution:

The binary form of 1010 is 010102. Forming the 2’s complement by complementing each bit and adding 1, we get

10101

+1

________

10110

Therefore, the instruction is written as

MOV CX, 10110B

12

CPE 0408330 @ 2008

S. Abed - HU, Jordan

13 of 50

7.1 Statement Syntax for a Source�Program - Expressions

13

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • Expression: group of several arithmetic, logical and relational operations
  • Arithmetic : +,-,*,/,%,shifts
  • Logical: NOT,AND,OR,XOR
  • Relational: testing conditions,returns true, false
  • We can use an expression rather than a straight operand i.e.
    • Mov AL, A / B , where A and B are variables
  • The next tables list all the operators that can be used in an expression.
  • Examples: *, /, MOD, SHL, SHR, +, -, EQ, NE, LT, GT, LE, GE, NOT, AND, OR, XOR
  • Parentheses can be used to change order of execution i.e. MOV AL, (A+B)/5

14 of 50

7.1 Statement Syntax for a Source�Program - Expressions

14

CPE 0408330 @ 2008

S. Abed - HU, Jordan

15 of 50

7.1 Statement Syntax for a Source�Program - Expressions

15

CPE 0408330 @ 2008

S. Abed - HU, Jordan

16 of 50

7.1 Statement Syntax for a Source�Program - Expressions

16

CPE 0408330 @ 2008

S. Abed - HU, Jordan

17 of 50

7.1 Statement Syntax for a Source�Program - Expressions

  • EXAMPLE

Find the value the assembler assigns to the source operand for the instruction

MOV BH, ( A * 4 – 2 ) / ( B – 3 )

for A=8 and B=5.

  • Solution:

The solution is calculated as

(8*4-2)/(5-3) = (32 – 2)/(2)

= (30)/(2) = 15

And using hexadecimal notation, we get the instruction

MOV BH, 0FH

17

CPE 0408330 @ 2008

S. Abed - HU, Jordan

18 of 50

7.1 Statement Syntax for a Source�Program - Expressions

  • EXAMPLE

What value is used for the source operand in the expression

MOV AX, A LE (B – C)

if A=234, B=345, and C=111?

  • Solution:

Substituting into the expression, we get

234 LE (345 – 111)

234 LE 234

since the relational operator is satisfied, the instruction is

equivalent to

MOV AX, 0FFFFH

18

CPE 0408330 @ 2008

S. Abed - HU, Jordan

19 of 50

7.1 Statement Syntax for a Source�Program

19

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Value-returning and attribute operators:

  • Operators used to return information about the operand rather than the operand itself or to change the attributes of the operand before it is fed to the instruction.
  • The next tables give us a list: Value-Returning

- SEG A, Offset A, TYPE A (B, W, Far or Near), SIZE A and Length A

  • Attribute:

- PTR (override the TYPE), DS: ES: SS, SHORT, THIS, HIGH and LOW

20 of 50

7.1 Statement Syntax for a Source�Program

20

CPE 0408330 @ 2008

S. Abed - HU, Jordan

21 of 50

7.1 Statement Syntax for a Source�Program

21

CPE 0408330 @ 2008

S. Abed - HU, Jordan

22 of 50

7.2 Assembler Directives

22

CPE 0408330 @ 2008

S. Abed - HU, Jordan

23 of 50

7.2 Assembler Directives

23

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Data directives

􀂄 Segment-control directives

􀂄 Modular programming directives

􀂄 Directives for memory usage control

􀂄 Directives for program listing control

24 of 50

7.2 Assembler Directives

24

CPE 0408330 @ 2008

S. Abed - HU, Jordan

Duplicate the values in ( ) a number of times

25 of 50

7.2 Assembler Directives

25

CPE 0408330 @ 2008

S. Abed - HU, Jordan

26 of 50

7.2 Assembler Directives

26

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Segment-control directives

􀂙Three kinds of memory segment:

• Code segment

• Data segment

• Stack segment

􀂙The segment-control directives partitions and assigns a source program to a specific memory segment.

􀂙The beginning of a segment is identified by the segment (SEGMENT) directive and its end is

marked by the end of segment (ENDS) directive.

27 of 50

7.2 Assembler Directives

27

CPE 0408330 @ 2008

S. Abed - HU, Jordan

28 of 50

7.2 Assembler Directives

28

CPE 0408330 @ 2008

S. Abed - HU, Jordan

Name of the segment

Paragraph (align-type):

- Sets the boundaries (here it is 16 byte)

- Can also be Byte, Word or Page

Combine-type attribute:

Specifies that this along with other

segments that have the same

name should be concatenated to

generate one physical Seg. Can also

be: Common (overlap), AT (specific)

Stack (part of the stack) or Memory

(Above all defined seg)

Class attribute, tells that this

is a code seg. Other values

are: Data, Stack

Tells which register

holds the base address

of the Seg

29 of 50

7.2 Assembler Directives

29

CPE 0408330 @ 2008

S. Abed - HU, Jordan

30 of 50

7.2 Assembler Directives

30

CPE 0408330 @ 2008

S. Abed - HU, Jordan

31 of 50

7.2 Assembler Directives

31

CPE 0408330 @ 2008

S. Abed - HU, Jordan

32 of 50

7.2 Assembler Directives

32

CPE 0408330 @ 2008

S. Abed - HU, Jordan

33 of 50

7.2 Assembler Directives

33

CPE 0408330 @ 2008

S. Abed - HU, Jordan

34 of 50

7.2 Assembler Directives

34

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Modular programming directives

􀂙Two kinds of procedures:

• Near procedure - IP in stack when called

• Far procedure - IP and CS in stack when called

􀂙NEAR is assumed as the default attribute of a

procedure.

􀂙If a procedure can be called from other modules,

its name must be made public by using the

PUBLIC directive.

􀂙If a procedure in another module is to be called

from the current module, its name must be

declared external by using the EXTRN directive.

35 of 50

7.2 Assembler Directives

35

CPE 0408330 @ 2008

S. Abed - HU, Jordan

36 of 50

7.2 Assembler Directives

36

CPE 0408330 @ 2008

S. Abed - HU, Jordan

37 of 50

7.2 Assembler Directives

37

CPE 0408330 @ 2008

S. Abed - HU, Jordan

38 of 50

7.2 Assembler Directives

38

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Examples of a source program using directives

39 of 50

7.2 Assembler Directives

39

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Examples of a source program using directives

40 of 50

7.3 Creating a Source File with an�Editor

40

CPE 0408330 @ 2008

S. Abed - HU, Jordan

C:\_

EDIT A:BLOCK.SRC [↵]

Type the program line by line

.

MOV AX, DATASEGADDR

MOV DS, AX

.

etc.

Alt-F

Menu to save or print the file

NEW

OPEN

.

etc.

Loaded file not saved. Save it now

<Yes> <No> <Cancel> <Help>

[↵]

X [↵]

41 of 50

7.4 Assembling and Linking Programs

41

CPE 0408330 @ 2008

S. Abed - HU, Jordan

42 of 50

7.4 Assembling and Linking Programs

42

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Source listing

Original

source code

Machine

Code

Program

starting

address

43 of 50

7.4 Assembling and Linking Programs

43

S. Abed - HU, Jordan

􀂄 Source listing

Symbol

table

Errors and warnings are reported here

Segments created

44 of 50

7.4 Assembling and Linking Programs

44

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Link program and modular programming

45 of 50

7.4 Assembling and Linking Programs

45

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄Benefits of modular programming:

    • Programmers can be working on a software project simultaneously.
    • Smaller sizes of the modules require less time to edit and assemble.
    • Modular programming makes it easier to reuse old software.

46 of 50

7.4 Assembling and Linking Programs

46

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Initiating the assembly and linking processes

ML [OPTIONS] file name [[OPTIONS] file name] … [/link link-options]

ML /Fl /Fm BLOCK.ASM (↵)

Display sequences for initiating assembly of a program

47 of 50

7.4 Assembling and Linking Programs

47

CPE 0408330 @ 2008

S. Abed - HU, Jordan

􀂄 Initiating the assembly and linking processes

LINK (↵)

Display sequences to initiate linking of object files

48 of 50

7.5 Loading and Executing a Run�Module

48

CPE 0408330 @ 2008

S. Abed - HU, Jordan

49 of 50

7.5 Loading and Executing a Run�Module

49

CPE 0408330 @ 2008

S. Abed - HU, Jordan

50 of 50

  • Solve the following problems from Chapter 7 from the course textbook:

3, 21, 39, 42, 50

50

CPE 0408330 @ 2008

S. Abed - HU, Jordan

H.W. #7