Eel 3801
This presentation is the property of its rightful owner.
Sponsored Links
1 / 112

EEL 3801 PowerPoint PPT Presentation


  • 84 Views
  • Uploaded on
  • Presentation posted in: General

EEL 3801. Part III Assembly Language Programming. Assembly Language Programming. The basic element of an assembly program is the statement. Two types of statements: Instructions : executable statements that actually do something.

Download Presentation

EEL 3801

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Eel 3801

EEL 3801

Part III

Assembly Language Programming

EEL 3801C


Assembly language programming

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.

EEL 3801C


Assembly programming cont d

Assembly Programming (cont’d)

  • 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.

EEL 3801C


Assembly programming cont d1

Assembly Programming (cont’d)

  • Logical: > < = etc.

  • Input-output: read, print etc.

EEL 3801C


Statements

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]

EEL 3801C


Names labels

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.

EEL 3801C


Names labels1

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:

    count1db50; a variable (memory allocation directive db)

    count2equ100; a constant

EEL 3801C


Names labels2

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:movax,10

    movbx,0

    .

    .

    .

    label2:

    jmplabel1; jump to label1

EEL 3801C


Mnemonics

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.

EEL 3801C


Operands

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 “;”.

EEL 3801C


Elements of assembly language for the 8086 processor

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.

EEL 3801C


Elements of assembly language cont

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.

EEL 3801C


Elements of assembly language cont1

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

EEL 3801C


Elements of assembly language cont2

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:

EEL 3801C


Elements of assembly language cont3

Elements of Assembly Language… (cont.)

“a” – 1 byte long

‘b’ – 1 byte long

“stack overflow” – 14 bytes long

‘abc#?%%A’ – 8 bytes long

EEL 3801C


Example of simple assembly program

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

EEL 3801C


Example cont

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.

EEL 3801C


More complex example cont

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:

EEL 3801C


More complex example cont1

More Complex Example (cont.)

12:.code

13: main proc

14:mov ax,@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:mainendp

24:endmain

EEL 3801C


More complex example cont2

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.

EEL 3801C


More complex example cont3

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.

EEL 3801C


More complex example cont4

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.

EEL 3801C


More complex example cont5

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.

EEL 3801C


More complex example cont6

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 (@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.

EEL 3801C


More complex example cont7

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.

EEL 3801C


More complex example cont8

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.

EEL 3801C


More complex example cont9

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.

EEL 3801C


More complex example cont10

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.

EEL 3801C


More complex example cont11

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.

EEL 3801C


Specifics of alp data definition directives

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.

EEL 3801C


Data definition directives

Data Definition Directives

  • Variables are identified by labels. A label shows the starting location of a variable’s memory location. A variable’s offset is the distance from the beginning of the data segment to the beginning of the variable.

EEL 3801C


Data definition directives cont

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.

EEL 3801C


Data definition directives cont1

DB

Define byte

DW

Define word

Define doubleword

DD

DF DP

Define far pointer

DQ

Define quadword

DT

Define tenbytes

Data Definition Directives (cont.)

  • The amount of memory to be allocated is determined by the directive itself.

EEL 3801C


Define byte

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.

EEL 3801C


Define byte cont

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.

EEL 3801C


Define byte example

Define Byte - Example

chardb‘A’; ASCII character

signed1db-128; min signed value

signed2db127; max signed value

unsigned1db0; min unsigned value

unsigned2db255; max signed value

EEL 3801C


Define byte cont1

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

EEL 3801C


Define byte cont2

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.

EEL 3801C


Define byte cont3

Define Byte (cont)

  • A variable’s value may be left undefined. This can be done by placing a ‘?’ for each byte to be allocated (as in a list).

    count db ?

EEL 3801C


Define byte cont4

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.

EEL 3801C


Define word

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.

EEL 3801C


Define word cont

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.

EEL 3801C


Define word example

Define Word (Example)

vardw1,2,3; defines 3 words

signed1dw-32768; smallest signed value

signed2dw32767; largest signed value

unsigned1dw0; smallest unsigned value

unsigned2dw65535; largest signed value

var-bindw1111000011110000b

var-hexdw4000h

var-mixdw1000h,4096,’AB’,0

EEL 3801C


Reverse storage format

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

      B62A

EEL 3801C


Define doubleword dd

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 ?

EEL 3801C


Multiple values

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.

EEL 3801C


Reverse order format

Reverse Order Format

  • As in define word, the bytes in doubleword are stored in reverse order as in DW.

  • For example,

    var dd12345678h

    78 56 34 12

EEL 3801C


Type checking

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.

EEL 3801C


Data transfer instructions mov

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.

EEL 3801C


Data transfer instructions mov cont

Data Transfer Instructions – mov (cont)

  • The format is:

    movdestination,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).

EEL 3801C


Operands1

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.

  • EEL 3801C


    Operands cont

    Operands (cont.)

    • Direct operands

      • A variable may be one of the two operands, but not both. It does not matter which is the variable.

    EEL 3801C


    Limitations on operands

    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

    EEL 3801C


    Sequential memory allocation

    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

    EEL 3801C


    Sequential memory allocation cont

    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.

    EEL 3801C


    Sequential memory allocation cont1

    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

    EEL 3801C


    Offsets

    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.

    EEL 3801C


    Offsets1

    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 array+1, the third at array+2, the fourth at array+3, etc.

    EEL 3801C


    Offsets2

    Offsets

    • To move the value of the 5th element of the array to register AL:

      mov al array+4

    • The size of the two operands must match.

      • Otherwise, an error may result.

    • Note that AL is used, not AX - 1 byte.

    EEL 3801C


    Ptr operator

    PTR Operator

    • Used to clarify an operand’s 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

    EEL 3801C


    Xchg instruction

    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.

    EEL 3801C


    Stack operations

    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.

    EEL 3801C


    Stack operations cont

    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).

    EEL 3801C


    Push operation

    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.

    EEL 3801C


    Push operation1

    Push Operation

    • Has the following form:

      push ax

      push ar1

      push 1000h

    EEL 3801C


    Pop operation

    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.

    EEL 3801C


    Pop operation1

    Pop Operation

    • Has the following form:

      pop ax

      pop ar2

    EEL 3801C


    Pushf and popf

    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.

    EEL 3801C


    Pusha 80186 and pushd 80386

    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.

    EEL 3801C


    Popa and popd

    POPA and POPD

    • Pops the same registers in the reverse order.

    EEL 3801C


    Arithmetic instructions

    Arithmetic Instructions

    • Form the heart of any program, at any level of abstraction.

    • Integer arithmetic done in 8- or 16-bit operands.

    EEL 3801C


    Arithmetic instructions1

    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

    EEL 3801C


    Inc and dec instructions

    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

    EEL 3801C


    Inc and dec instructions1

    INC and DEC Instructions

    • Faster than ADD and SUB instructions

    • All status flags are affected except the Carry Flag.

    EEL 3801C


    Add instruction

    ADD Instruction

    • Used to add two numbers together.

    • Format is as follows:

      add destination,source

    • The high level language equivalent of:

      destination = destination + source

    EEL 3801C


    Add instruction1

    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.

    EEL 3801C


    Add instruction cont

    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).

    EEL 3801C


    Sub instruction

    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)

    EEL 3801C


    Sub instruction cont

    SUB Instruction (cont.)

    • Takes the two’s 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.

    EEL 3801C


    Sub instruction cont1

    SUB Instruction (cont.)

    • Only one of the operands may be a memory operand.

    • Immediate values cannot be destinations.

    EEL 3801C


    Effects of sub and add on flag registers

    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).

    EEL 3801C


    Flag registers cont

    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 < 0

    • Carry Flag: Used with unsigned arithmetic only, even though the processor updates it even if operation is signed.

      • For example:

    EEL 3801C


    Flag example

    Flag Example

    mov ax,00FFh

    add al,1; AX = 0000h, CF=1

    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

    EEL 3801C


    Flag example cont

    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 it’s a 16-bit operation

      ; AX = 0100h CF = 0

    EEL 3801C


    Flag example cont1

    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).

      movax,5501h

      sub al,2; AX = 55FFh CF=1

    • We subtracted 2 from 1, resulting in a negative number in an unsigned operation.

    EEL 3801C


    Flag registers cont1

    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.

    EEL 3801C


    Flag registers example

    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 two’s complement of –128, which is completely incorrect, therefore meaningless in signed integers.

    • Therefore, OF = 1.

    EEL 3801C


    Flag registers example cont

    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 two’s complement) + 11111110 (-2 in twos’ comp)

        = 1 01111110

    EEL 3801C


    Flag registers example cont1

    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.

    EEL 3801C


    Addressing modes the offset

    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 variable’s address

    EEL 3801C


    Addressing modes example

    Addressing Modes- Example

    • The assembler always knows the address of each variable (i.e., label).

    • For example,

      mov ax,offset avariable

    EEL 3801C


    Direct addressing mode

    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.

    EEL 3801C


    Direct addressing mode cont

    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.

    EEL 3801C


    Direct addressing mode example

    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]

    EEL 3801C


    Indirect addressing mode

    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.

    EEL 3801C


    Indirect addressing mode example

    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”

    EEL 3801C


    Indirect addressing mode example1

    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

    EEL 3801C


    Indirect addressing mode example2

    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.

    EEL 3801C


    Indirect addressing mode example3

    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,ds:[bp]; looks in data segment DS

    EEL 3801C


    Based and indexed addressing mode

    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.

    EEL 3801C


    Based and indexed addressing mode example

    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,anarray[bx]

      or

      mov al,[anarray+bx]

      or

      mov al,[anarray+5]

    EEL 3801C


    Base indexed addressing mode

    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.

    EEL 3801C


    Base indexed example

    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.

    EEL 3801C


    Base indexed example cont

    Base-Indexed - Example (cont.)

    • For example, let’s 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; base=1st element of row 2

      mov si,2 ;offset = 2

      mov al,[bx+si] ; moves the ;value 80 into AL

    EEL 3801C


    Base indexed with displacement addressing mode

    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, array[bx+si]

    EEL 3801C


    Program structure

    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).

  • EEL 3801C


    Program structure cont

    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.

    EEL 3801C


    Program structure cont1

    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.

    EEL 3801C


    Program structure cont2

    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.

    EEL 3801C


    Program structure cont3

    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.

    EEL 3801C


    Program structure cont4

    Program Structure (cont.)

    • The various models are the following:

      • Tiny:Code + data <= 64K

      • Small:Code <= 64K; data <= 64K

      • Medium:Data <= 64K; Code any size

      • Compact:Code <= 64K; Data any size

      • Large:No restrictions on either one, but arrays <= 64K

      • Huge:No restrictions on any of the three

    EEL 3801C


    Program structure cont5

    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.

    EEL 3801C


  • Login