EEL 3801 - PowerPoint PPT Presentation

1 / 112
About This Presentation
Title:

EEL 3801

Description:

EEL 3801 Part III Assembly Language Programming Assembly Language Programming The basic element of an assembly program is the statement. Two types of statements ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 113
Provided by: ave65
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: EEL 3801


1
EEL 3801
  • Part III
  • Assembly Language Programming

2
Assembly Language Programming
  • The basic element of an assembly program is the
    statement.
  • Two types of statements
  • Instructions executable statements that actually
    do something.
  • Directive provide information to assist the
    assembler in producing executable code. For
    example, create storage for a variable and
    initialize it.

3
Assembly Programming (contd)
  • Assembly language instructions equate one-to-one
    to machine-level instructions, except they use a
    mnemonic to assist the memory.
  • Program control Control the flow of the program
    and what instructions to execute next (jump,
    goto).
  • Data transfer Transfer data to a register or to
    main memory.
  • Arithmetic add, subtract, multiply, divide, etc.

4
Assembly Programming (contd)
  • Logical gt lt etc.
  • Input-output read, print etc.

5
Statements
  • A statement is composed of a name, a mnemonic,
    operands and an optional comment. Their general
    format is as follows
  • name mnemonic operand(s) comments

6
Names, labels
  • Name Identifies a label for a statement or for
    a variable or constant.
  • Can contain one or more of several characters
    (see page 56 of new textbook).
  • Only first 31 characters are recognized
  • Case insensitive.
  • First character may not be a digit.

7
Names, labels
  • The period . may only be used as the first
    character.
  • Cannot use reserved names.
  • Can be used to name variables. Such when put in
    front of a memory allocation directive. Can also
    be used to define a constant. For example
  • count1 db 50 a variable (memory allocation
    directive db)
  • count2 equ 100 a constant

8
Names, labels
  • Can be used as labels for statements to act as
    place markers to indicate where to jump to. Can
    identify a blank line. For example
  • label1 mov ax,10
  • mov bx,0
  • .
  • .
  • .
  • label2
  • jmp label1 jump to label1

9
Mnemonics
  • Mnemonic identifies an instruction or
    directive. These were described above.
  • The mnemonics are standard keywords of the
    assembly language for a particular processor.
  • You will become familiar with them as time goes
    on this semester.

10
Operands
  • Operands are various pieces of information that
    tells the CPU what to take the action on.
    Operands may be a register, a variable, a memory
    location or an immediate value.
  • 10 ? immediate value
  • count ? variable
  • AX ? register
  • 0200 ? memory location
  • Comments Any text can be written after the
    statement as long as it is preceded by the .

11
Elements of Assembly Language for the 8086
Processor
  • Assembler Character Set These are used to form
    the names, mnemonics, operands, variables,
    constants, numbers etc. which are legal in 8086
    assembly.
  • Constant A value that is either known or
    calculated at assembly time. May be a number or
    a string of characters. Cannot be changed at run
    time.

12
Elements of Assembly Language (cont.)
  • Variable A storage location that is referenced
    by name. A directive must be executed
    identifying the variable name with the location
    in memory.
  • Integers Numeric digits with no decimal point,
    followed by the radix mentioned before (e.g., d,
    h, o, or b). Can be signed or unsigned.

13
Elements of Assembly Language (cont.)
  • Real numbers floating point number made up of
    digits, a decimal point, an optional exponent,
    and an optional leading sign.
  • ( or -) digits.digits exponential ( or -)
    digits

14
Elements of Assembly Language (cont.)
  • Characters and strings
  • A character is one byte long.
  • Can be mapped into the binary code equivalent
    through the ASCII table, and vice-versa.
  • May be enclosed within single or double quotation
    marks.
  • Length of string determined by number of
    characters in string, each of which is 1 byte.
    For example

15
Elements of Assembly Language (cont.)
  • a 1 byte long
  • b 1 byte long
  • stack overflow 14 bytes long
  • abc?A 8 bytes long

16
Example of Simple Assembly Program
  • The following simple program will be demonstrated
    and explained
  • mov ax,5 move 5h into the AX register
  • add ax,10 add 10h to the AX register
  • add ax,20 add 20h to the AX register
  • mov sum,ax store value of AX in variable
  • ending the program
  • mov ax, 4C00H
  • int 21

17
Example (cont.)
  • The result is that at the end of the program, the
    variable sum, which exists somewhere in memory
    (declaration not shown), now accepts the value
    accumulated in AX, namely, 35h.
  • Explain program.

18
More Complex Example (cont.)
  • 1 title Hello World Program (hello.asm)
  • 2
  • 3 this program displays Hello, World
  • 4
  • 5 dosseg
  • 6 .model small
  • 7 .stack 100h
  • 8
  • 9 .data
  • 10 hello_message db Hello, World!,0dh,0ah,
  • 11

19
More Complex Example (cont.)
  • 12 .code
  • 13 main proc
  • 14 mov ax,_at_data
  • 15 mov ds,ax
  • 16
  • 17 mov ah,9
  • 18 mov dx,offset hello_message
  • 19 int 21h
  • 20
  • 21 mov ax,4C00h
  • 22 int 21h
  • 23 main endp
  • 24 end main

20
More Complex Example (cont.)
  • The program is explained as follows
  • Line 1 Contains the title directive. All
    characters located after the title directive are
    considered as comments, even though the symbol
    is not used.
  • Line 3 Comment line with the symbol
  • Line 5 The dosseg directive specifies a standard
    segment order for the code, data and stack
    segments. The code segment is where the program
    instructions are stored. The data segment is
    where the data (variables) are stored. The stack
    segment is where the stack is maintained.

21
More Complex Example (cont.)
  • Line 6 The .model directive indicates the memory
    architecture to be used. In this case, it uses
    the Microsoft small memory architecture. It
    indicates this by the word small after .model.
  • Line 7 This directive sets aside 100h of memory
    for the stack. This is equivalent to 256 bytes
    of memory (162 256).
  • Line 9 The .data directive marks the beginning
    of the data segment, where the variables are
    defined and memory allocated to them.

22
More Complex Example (cont.)
  • Line 10 The db directive stands for define
    byte, which tells the assembler to allocate a
    sequence of memory bytes to the data that follow.
    0dh is a carriage return and 0ah is the linefeed
    symbol. The is the required terminator
    character. The number of memory bytes is
    determined by the data themselves. Hello_message
    is the name of the variable to be stored in
    memory, and the db allocates memory to it in the
    size defined by the data following it.
  • Line 12 The directive .code is the indication of
    the beginning of the code segment. The next few
    lines represent instructions.

23
More Complex Example (cont.)
  • Line 13 The proc directive is used to declare
    the main procedure called main. Any name could
    have been used, but this is in keeping with the
    C/C programming requirement that the main
    procedure be called the main function. The first
    executable instruction following this directive
    is called the program entry point - the point at
    which the program begins to execute.

24
More Complex Example (cont.)
  • Line 14 The instruction mov is used to copy the
    contents of one address into the other one. The
    first operand is called the destination address,
    while the second one is called the source
    address. In this particular use, we tell the
    assembler to copy the address of the data segment
    (_at_data) into the AX register.
  • Line 15 Copies the content of AX into DS, which
    is a register used to put the data segment, the
    default base location for variables.

25
More Complex Example (cont.)
  • Line 17 This instruction places the value 9 in
    the AH register. Remember that from the page.com
    program, this is the register used to store the
    name of the DOS subroutine to be called with the
    int 21 instruction.
  • Line 18 This instruction places the address of
    the string to be identified in the DX register.
    Remember that this is the offset, where the
    default base segment is already identified in the
    DS register as the base segment for the data
    segment. Since the address of the variable
    hello_message begins at the beginning of the data
    segment, identified by DS, we only need to supply
    the offset for the variable.

26
More Complex Example (cont.)
  • Line 19 The instruction int 21, as we saw
    before, takes the name of the function from the
    DX register, which in this case is 9. DOS
    funtion 9, incidentally, sends the contents of DX
    register to the VRT output device. The DX
    register contains the address of the string to be
    sent.
  • Line 21 and 22 These instructions represent the
    equivalent of an end or stop statement. This is
    different from that done for page.com because
    this will be an executable program (.exe), rather
    than a .com program. More on this later.
  • Line 23 Indicates the end of the main procedure.

27
More Complex Example (cont.)
  • Line 24 The END directive the last line to be
    assembled. The main next to it indicates the
    program entry point.

28
More Complex Example (cont.)
  • The program may seem overly complicated for such
    a simple program.
  • But remember that assembly language corresponds
    one-to-one with machine language instructions.
  • Note that it takes only 562 bytes of memory when
    assembled and compiled.

29
More Complex Example (cont.)
  • The same program written in a high level language
    will require several more machine level
    instructions to carry out the same thing.
  • Written in Turbo C, the executable program
    would take 8772 bytes of memory to store.

30
Specifics of ALP Data Definition Directives
  • A variable is a symbolic name for a location in
    memory. This is done because it is easy to
    remember variables, but not memory locations. It
    is like an aka, or a pseudonym.

31
Data Definition Directives
  • Variables are identified by labels. A label
    shows the starting location of a variables
    memory location. A variables offset is the
    distance from the beginning of the data segment
    to the beginning of the variable.

32
Data Definition Directives (cont.)
  • A label does not indicate the length of the
    memory that the variable takes up.
  • If a string is being defined, the label offset is
    the address of the first byte of the string (the
    first element of the string).
  • The second element is the offset 1 byte.
  • The third element is offset 2 bytes.

33
Data Definition Directives (cont.)
  • The amount of memory to be allocated is
    determined by the directive itself.

34
Define Byte
  • Allocates storage for one or more 8-bit values
    (bytes). Has the following format
  • name DB initialvalue ,initialvalue
  • The name is the name of the variable. Notice
    that it is optional.

35
Define Byte (cont)
  • initialvalue can be one or more 8-bit numeric
    values, a string constant, a constant expression
    or a question mark.
  • If signed, it has a range of 127 to 128, If
    unsigned, it has a range of 0 255.

36
Define Byte - Example
  • char db A ASCII character
  • signed1 db -128 min signed value
  • signed2 db 127 max signed value
  • unsigned1 db 0 min unsigned value
  • unsigned2 db 255 max signed value

37
Define Byte (cont)
  • Multiple values A sequence of 8-bit numbers can
    be used as initialvalue. They are then grouped
    together under a common label, as in a list.
  • The values must be separated by commas.
  • list db 10,20,30,40

38
Define Byte (cont)
  • The 10 would be stored at offset 0000
  • 20 at offset 0001
  • 30 at offset 0002 and
  • 40 at offset 0003,
  • where 0001 represents a single byte.

39
Define Byte (cont)
  • A variables value may be left undefined. This
    can be done by placing a ? for each byte to be
    allocated (as in a list).
  • count db ?

40
Define Byte (cont)
  • A string may be assigned to a variable, each of
    whose elements will be allocated a byte.
  • c_string db This is a long string
  • The length of a string can be automatically
    determined by the assembler by the symbol.
  • See page 65 of new book for details.

41
Define Word
  • Serves to allocate memory to one or more
    variables that are 16 bits long. Has the
    following format
  • name DW initialvalue ,initialvalue
  • The name is the name of the variable. Notice
    that it is optional.
  • initialvalue can be one or more 16-bit numeric
    values, a string constant, a constant expression
    or a question mark.

42
Define Word (cont)
  • If signed, it has a range of 32,767 to 32,768,
  • If unsigned, it has a range of 0 65,535.

43
Define Word (Example)
  • var dw 1,2,3 defines 3 words
  • signed1 dw -32768 smallest signed value
  • signed2 dw 32767 largest signed value
  • unsigned1 dw 0 smallest unsigned value
  • unsigned2 dw 65535 largest signed value
  • var-bin dw 1111000011110000b
  • var-hex dw 4000h
  • var-mix dw 1000h,4096,AB,0

44
Reverse Storage Format
  • The assembler reverses the bytes in a word when
    storing it in memory.
  • The lowest byte is placed in the lowest address.
  • It is re-reversed when moved to a 16-bit
    register.
  • value dw 2AB6h
  • B6 2A

45
Define Doubleword DD
  • Same as DB and DW, except the memory allocated
    is now 4 bytes (2 words, 32 bits).
  • name DD initialvalue ,initialvalue
  • The name is the name of the variable. Notice
    that it is optional.
  • initialvalue can be one or more 32-bit numeric
    values, either in dec., hex or bin. form, string
    const., a const. Expression, or ?

46
Multiple Values
  • A sequence of 32-bit numbers can be used as
    initialvalue.
  • They are then grouped together under a common
    label, as in a list.
  • The values must be separated by commas.

47
Reverse Order Format
  • As in define word, the bytes in doubleword are
    stored in reverse order as in DW.
  • For example,
  • var dd 12345678h
  • 78 56 34 12

48
Type Checking
  • When a variable is created, the assembler
    characterizes it according to its size (i.e.,
    byte, word, doubleword, etc.).
  • When a variable is later referenced, the
    assembler checks its size and only allows values
    to be stored that come from a register or other
    memory that matches in size.
  • Mismatched movements of data not allowed.

49
Data Transfer Instructions mov
  • The instruction mov is called the data transfer
    instruction.
  • A very important one in assembly - much
    programming involves moving data around.
  • Operands are 8- or 16-bit on the 8086, 80186 and
    80286.
  • Operands on the 80386 and beyond, they can also
    be 32-bits long.

50
Data Transfer Instructions mov (cont)
  • The format is
  • mov destination,source
  • The source and destination operands are
    self-explanatory.
  • The source can be an immediate value (a
    constant), a register, or a memory location
    (variable). It is not changed by the operation.
  • The destination can be a register or a memory
    location (variable).

51
Operands
  • Register Operands Transfer involving only
    registers. It is the fastest.
  • Source any register
  • Destination any register except CS and IP
  • Immediate Operands
  • Immediate value can be moved to a register (all
    but IP) or to memory.
  • Destination must be of same type as source.

52
Operands (cont.)
  • Direct operands
  • A variable may be one of the two operands, but
    not both. It does not matter which is the
    variable.

53
Limitations on operands
  • There are some limitations on mov
  • CS or IP not destination operand
  • Moving immediate data to segment registers.
  • Moving from segment register to segment register.
  • Source and destination operands of different
    types.
  • Immediate value as destination (obviously!!)
  • Memory to memory moves

54
Sequential Memory Allocation
  • Memory for variables is allocated sequentially by
    the assembler.
  • If we call DB several times, such as in
  • var1 db 10
  • var2 db 15
  • var3 db 20

55
Sequential Memory Allocation (cont)
  • var1 will be the first byte in the data segment
    of main memory.
  • This segment may be identified by the base
    segment and the offset.
  • var2 will occupy the next available memory
    location, or 1 byte away from the beginning of
    the data segment in memory.

56
Sequential Memory Allocation (cont)
  • var 3 will be 2 bytes away from this starting
    point.
  • This will be the case even if the memory
    locations are not labeled, such as in
  • db 10
  • db 20
  • db 30

57
Offsets
  • Many times, memory will be allocated, but not
    labeled.
  • This is typical of an array, when only the entire
    array is labeled, not each cell.
  • The address of the array is the address of the
    first element (position) of the array.
  • All subsequent cells are allocated by adding an
    offset to the address of the head element.

58
Offsets
  • This is also true when a list of elements is
    defined through DB, DW, or DD.
  • Example an array or list of 8-bit numbers whose
    memory location is called a-list.
  • To access the first element of a-list, we
    reference the location in memory corresponding to
    a-list.
  • To access any of the other elements of the array,
    we provide an offset to the address of array.
  • The second element at array1, the third at
    array2, the fourth at array3, etc.

59
Offsets
  • To move the value of the 5th element of the array
    to register AL
  • mov al array4
  • The size of the two operands must match.
  • Otherwise, an error may result.
  • Note that AL is used, not AX - 1 byte.

60
PTR Operator
  • Used to clarify an operands type. NOT a
    pointer.
  • It can be placed between the mov command and the
    operands, as for example Used for readability
    only. It does not change the size of the operand
    in any way.
  • mov word ptr count,10
  • mov byte ptr var2,5

61
XCHG Instruction
  • Allows the direct exchange of values between 2
    registers or between a register and a memory
    location.
  • Very fast
  • Needs to obey size constraints
  • Used in sorting.

62
Stack Operations
  • Already discussed what a stack is.
  • Each position in the stack is 2 bytes long only
    16-bit registers can be copied into the stack.
  • The bottom of the stack is in high memory, and
    it grows downward.
  • Typically, 256 bytes are allocated to the stack,
    enough for 128 entries.

63
Stack Operations (cont)
  • Identified by the SS register (stack segment),
    which identifies the address of the base location
    of the stack segment.
  • The stack pointer (SP register) indicates the
    address of the first element of the stack (top of
    the stack).

64
Push Operation
  • Puts something (a 16-bit element) at the top
    position of the stack.
  • Decrements stack pointer (it grows downward).
  • Can put the contents of a register or of memory
    (a variable)
  • In 80286 and later processors, it can also place
    an immediate value.

65
Push Operation
  • Has the following form
  • push ax
  • push ar1
  • push 1000h

66
Pop Operation
  • The opposite of the push operation.
  • Removes the top element in the stack.
  • Copies value of top element in stack to
    destination indicated in the statement.
  • Increments stack pointer.
  • Registers CS (code segment) and IP (instruction
    pointer) cannot be used as operands.

67
Pop Operation
  • Has the following form
  • pop ax
  • pop ar2

68
PUSHF and POPF
  • Special instructions that move and remove the
    contents of the Flags register onto and out of
    the stack.
  • These are used to preserve the contents of these
    registers in case it is changed and the old
    values are to be reinstated.
  • See page 56.

69
PUSHA (80186) and PUSHD (80386)
  • Pushes the contents of the registers AX, CX, DX,
    BX, original SP, BP, SI, and DI on the stack in
    this exact order.
  • PUSHD does the same for 32-bit registers.

70
POPA and POPD
  • Pops the same registers in the reverse order.

71
Arithmetic Instructions
  • Form the heart of any program, at any level of
    abstraction.
  • Integer arithmetic done in 8- or 16-bit operands.

72
Arithmetic Instructions
  • Floating point arithmetic done in one of three
    ways
  • 80x87 math coprocessor
  • software routines that emulate the coprocessor
  • software that converts floating point to integer,
    executes the instruction and then converts back
    to floating point. (not particularly useful in
    many cases).
  • Section concentrates on integer arithmetic

73
INC and DEC Instructions
  • Respectively increment and decrement the operand
    by one.
  • Form is as follows
  • inc al incr. 8-bit register by 1
  • inc bx incr. 16-bit register by 1
  • inc mem incr. memory location by 1
  • dec mem decr. memory location by 1
  • dec bx decr. 16-bit register by 1
  • dec al decr. 8-bit register by 1

74
INC and DEC Instructions
  • Faster than ADD and SUB instructions
  • All status flags are affected except the Carry
    Flag.

75
ADD Instruction
  • Used to add two numbers together.
  • Format is as follows
  • add destination,source
  • The high level language equivalent of
  • destination destination source

76
ADD Instruction
  • Takes 2 operands, a source and a destination.
  • Adds the contents of the operands.
  • Places the result in the destination.
  • Only one memory operand may be used.
  • Source may be immediate value, but not
    destination.

77
ADD Instruction (cont.)
  • The contents of the source remain unchanged.
  • A segment register may not be a destination.
  • All status flags are affected.
  • Sizes of operands must match (same size).

78
SUB Instruction
  • Subtracts a source operand from a destination
    operand and stores the result in the destination.
  • Format is as follows
  • sub destination,source
  • The high level language equivalent of
  • destination destination (-source)

79
SUB Instruction (cont.)
  • Takes the twos complement of source and adds it
    to the destination.
  • Only one of the operands may be a memory operand.
  • Size of operands must match (same size).
  • Segment register may not be the destination
    operand.

80
SUB Instruction (cont.)
  • Only one of the operands may be a memory operand.
  • Immediate values cannot be destinations.

81
Effects of SUB and ADD on Flag Registers
  • Why the flags?
  • If an operation such as ADD overflows (i.e.,
    number to be put in the destination exceeds the
    size of the destination), then overflow.
  • The flag may indicate that the value of the
    destination is meaningless.
  • The ADD instruction, therefore, affects both the
    Carry and Overflow flags.
  • Only 1 of these flags (if signed or if unsigned).

82
Flag Registers (cont.)
  • Zero Flag Set when the result of the
    instructions inc, dec, add or sub 0.
  • Sign Flag Set when the result of the
    instructions inc, dec, add or sub lt 0
  • Carry Flag Used with unsigned arithmetic only,
    even though the processor updates it even if
    operation is signed.
  • For example

83
Flag Example
  • mov ax,00FFh
  • add al,1 AX 0000h, CF1
  • 00FFh 0001h 0100h
  • Since the operand of the add instruction is 8-bit
    (AL register), this is an 8-bit operation.
  • Therefore, the operation looks more like
  • FFh 01h 100h

84
Flag Example (cont.)
  • This is an overflow, so the Carry Flag is set to
    indicate that the result was larger than the
    8-bit destination could store.
  • Could be fixed by using a 16-bit operation
    (e.g.,)
  • add ax,1 now its a 16-bit operation
  • AX 0100h CF 0

85
Flag Example (cont.)
  • If result of operation is too large for the
    destination operand, the Carry flag is set.
  • Can also occur in a subtraction operation when
    subtracting a larger operand from a smaller one
    (signed results are not permissible).
  • mov ax,5501h
  • sub al,2 AX 55FFh CF1
  • We subtracted 2 from 1, resulting in a negative
    number in an unsigned operation.

86
Flag Registers (cont.)
  • Overflow Flag Set by the processor regardless of
    the type of operation, but important only when
    operation is signed.
  • Signed results are useless when this flag is set
  • Can result when using add or sub
  • But not inc or dec.

87
Flag Registers - Example
  • If we add 126 2 128, in an 8-bit operation,
    this will represent an overflow.
  • This operation in binary numbers
  • 01111111 00000010 10000000
  • Equivalent to the twos complement of 128, which
    is completely incorrect, therefore meaningless in
    signed integers.
  • Therefore, OF 1.

88
Flag Registers - Example (cont.)
  • In the case of subtraction, it gets a little more
    complicated.
  • If we subtract 2 from 128
  • -128 2 -130 which is an overflow, as it does
    not fit in an 8-bit destination.
  • In binary, this looks like this
  • 10000000 (-128 in twos complement) 11111110
    (-2 in twos comp)
  • 1 01111110

89
Flag Registers - Example (cont.)
  • The first 1, of course, is a ninth digit, which
    does not fit into the 8-bit register.
  • Thus, the operation looks like it resulted in
  • 01111110
  • which is 126.
  • So, the OF 1.

90
Addressing Modes - the OFFSET
  • Five (5) different addressing modes in the intel
    8086 family of processors.
  • Introduce an operator called offset, which places
    the address (actually, the offset) of a variable
    in a register.
  • To move the address of a variable into a
    register, but do not know it directly, the OFFSET
    operator returns variables address

91
Addressing Modes - Example
  • The assembler always knows the address of each
    variable (i.e., label).
  • For example,
  • mov ax,offset avariable

92
Direct Addressing Mode
  • Returns the contents of memory at the offset of a
    variable.
  • The assembler keeps track of the offset of every
    label (variable).
  • Thus, by simply referencing the label, the
    contents of the memory location assigned to that
    label can be accessed.

93
Direct Addressing Mode (cont.)
  • The effective address (EA) is the offset (i.e.,
    distance) from the beginning of a segment.
  • A displacement is either the absolute address, or
    the offset of a variable.

94
Direct Addressing Mode - Example
  • count db 20
  • mov al,count AL 20 moved the contents
    of the label count into AL
  • Can also be done by referencing the memory
    address within brackets,
  • for example, 200

95
Indirect Addressing Mode
  • Placing the address of the label in a base or
    index register can create a pointer to a label.
  • Typically done to represent a complex data
    structure, such as an array or a structure.
  • We can increment the pointer to point to elements
    of the data structure.

96
Indirect Addressing Mode - Example
  • An array such as shown below.
  • A B C D E F G H I J K L M N
  • Let us define this array as a string in the
    following manner
  • astring db ABCDEFGHIJKLMN

97
Indirect Addressing Mode - Example
  • To access the 6th element (F), set the value of a
    register to the first element of the array
    (string).
  • mov bx,offset astring moved the offset
  • Then increment the memory address or offset, by
    5, such as
  • add bx,5 add 5 bytes to address in BX
  • mov dl,bx move content of BX into DL

98
Indirect Addressing Mode - Example
  • If the BX, SI or DI registers are used, the
    effective (absolute) address is understood to be
    an offset from the DS register (by default).
  • If BP register is used, it is understood to be an
    offset from the SS register.
  • One can change the default by placing before the
    register the base register from which the offset
    is calculated.

99
Indirect Addressing Mode - Example
  • For example
  • To use the BP register, but calculate its address
    from the DS register rather than from the SS
  • mov dl,dsbp looks in data segment DS

100
Based and Indexed Addressing Mode
  • A register value and a displacement can be used
    as addresses to access the contents of memory.
  • The displacement is either a number, or the
    offset of a label whose address (offset) is known
    by the assembler at compile time.

101
Based and Indexed Addressing Mode - Example
  • For example, to access the number 8
  • 5 7 1 6 2 8 7 9 0
  • anarray db 5,7,1,6,2,8,7,9,0
  • mov bx,5
  • mov al,anarraybx
  • or
  • mov al,anarraybx
  • or
  • mov al,anarray5

102
Base-Indexed Addressing Mode
  • Combining a base register and an index register
    forms the effective address.
  • Useful for defining 2 dimensional arrays.
  • Similar to the Base and indexed, except now
    instead of having a displacement that is either a
    constant or the address of a label, it is now the
    sum of the base and index registers.

103
Base-Indexed - Example
  • a2darray db 10,20,30,40,50
  • db 60, 70, 80, 90, A0
  • db B0, C0, D0, E0, F0
  • If we want to access the third element of the
    second row, we set a base segment to the first
    element of the second row, and then the offset is
    the third column.

104
Base-Indexed - Example (cont.)
  • For example, lets say we want to get the 80
  • 10 20 30 40 50 60 70 80 90 A0 B0 C0 D0 E0 F0
  • mov bx,offset a2darray
  • add bx,5 base1st element of row 2
  • mov si,2 offset 2
  • mov al,bxsi moves the value 80
    into AL

105
Base-indexed with Displacement Addressing Mode
  • The combination of the 2 addressing modes above.
  • Combines the displacement of a label with that of
    a base and index segments.
  • The same 80 value in the above array can be
    accessed with the operand
  • mov al, arraybxsi

106
Program Structure
  • Program divided into 3 primary segments
  • code segment (pointed to by register CS)
  • data segment (pointed to by register DS)
  • stack segment (pointed to by register SS)
  • Segments can range in size between 10h (16) bytes
    and 64K bytes.
  • Instructions or data located on these segments is
    referenced through the offsets from the base
    segment (displacements).

107
Program Structure (cont.)
  • Each segment is identified by the .stack, .code,
    and .data indicators.
  • Note that the SP register points to the next
    memory location after the end of the stack
    segment.
  • This is because the stack grows from high memory
    to low memory.

108
Program Structure (cont.)
  • The title directive identifies the program title
    (up to 128 characters long).
  • The dosseg directive tells the assembler to place
    the program segments in the standard order used
    by the high level languages.
  • The .model directive identifies the type of
    memory model to be used.

109
Program Structure (cont.)
  • The memory models are based on the idea that a
    16-bit address register can only represent
    approximately 64K bytes of memory range.
  • Thus, if we have a segment of memory that is less
    than or equal to 64K bytes, then we can easily
    access that location through a simple offset from
    the base segment.

110
Program Structure (cont.)
  • This means quicker addressing, since only 1
    instruction is necessary to load a 16-bit
    address.
  • If greater than 64K, however, then we must change
    the base segment register as well, as the offset
    cannot reach beyond 64K bytes.
  • This requires 2 machine instructions, one for the
    base segment, and one for the offset.

111
Program Structure (cont.)
  • The various models are the following
  • Tiny Code data lt 64K
  • Small Code lt 64K data lt 64K
  • Medium Data lt 64K Code any size
  • Compact Code lt 64K Data any size
  • Large No restrictions on either one, but arrays
    lt 64K
  • Huge No restrictions on any of the three

112
Program Structure (cont.)
  • The Tiny model does not result in an executable
    file, but rather, in a command file (.com).
  • All others result in .exe files.
  • The linker produces a map file which indicates
    the location of these segments.
Write a Comment
User Comments (0)
About PowerShow.com