8086 assembly language programming i

Upload: mohammad-gulam-ahamad

Post on 14-Apr-2018

264 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 8086 Assembly Language Programming I

    1/46

  • 7/30/2019 8086 Assembly Language Programming I

    2/46

    Learning assembly language programming

    will help understanding the operations ofthe microprocessor To learn:

    Need to know the functions of various registers Need to know how external memory is organized

    and how it is addressed to obtain instructionsand data (different addressing modes) Need to know what operations (or the

    instruction set) are supported by the CPU. Forexample, powerful CPUs support floating-pointoperations but simple CPUs only support integer

    operations

    2

  • 7/30/2019 8086 Assembly Language Programming I

    3/46

    CConcept

    L Logic thinking P Practice

    Concept we must learn the basic syntax,such as how a program statement is written

    Logic thinking programming is problemsolving so we must think logically in orderto derive a solution

    Practice write programs

    3

  • 7/30/2019 8086 Assembly Language Programming I

    4/46

    The native language is machine language

    (using 0,1 to represent the operation) A single machine instruction can take up one or

    more bytes of code Assembly language is used to write the

    program using alphanumeric symbols (or

    mnemonic), eg ADD, MOV, PUSH etc. The program will then be assembled (similar to

    compiled) and linked into an executableprogram.

    The executable program could be .com, .exe,or .bin files

    4

  • 7/30/2019 8086 Assembly Language Programming I

    5/46

    5

    Program

    .asm

    Object file

    .obj

    Executable file

    .exe

    Assemble link

  • 7/30/2019 8086 Assembly Language Programming I

    6/46

    Machine code for mov AL, 00H

    B4 00 (2 bytes)

    After assembled, the value B400 will bestored in the memory

    When the program is executed, then thevalue B400 is read from memory, decodedand carry out the task

    6

  • 7/30/2019 8086 Assembly Language Programming I

    7/46

    Each instruction is represented by one assemblylanguage statement

    The statement must specify which operation(opcode) is to be performed and the operands Eg ADD AX, BX ADD is the operation AX is called the destination operand BX is called the source operand The result is AX = AX + BX When writing assembly language program, you

    need to think in the instruction level

    7

  • 7/30/2019 8086 Assembly Language Programming I

    8/46

    In c++, you can do A = (B+C)*100

    In assembly language, only one instructionper statement

    A = B ; only one instruction - MOVE

    A = A+C ; only one instruction - ADDA = A*100 ; only one instruction - Multiply

    8

  • 7/30/2019 8086 Assembly Language Programming I

    9/46

    General format for an assembly languagestatement

    Label Instruction Comment

    Start: Mov AX, BX ; copy BX into AX

    Start is a user defined name and you only put in a

    label in your statement when necessary!!!!

    The symbol : is used to indicate that it is a label

    9

  • 7/30/2019 8086 Assembly Language Programming I

    10/4610

  • 7/30/2019 8086 Assembly Language Programming I

    11/46

    In 8086, memory is divided into segments

    Only 4 64K-byte segments are active and these are:code, stack, data, and extra

    When you write your assembly language program for an8086, theoretically you should define the differentsegments!!!

    To access the active segments, it is via the segmentregister: CS (code), SS (stack), DS (data), ES (extra)

    So when writing assembly language program, youmust make use of the proper segment register orindex register when you want to access the memory

    11

  • 7/30/2019 8086 Assembly Language Programming I

    12/46

    In assembly programming, you cannotoperate on two memory locations in thesame instruction

    So you usually need to store (move) value ofone location into a register and then perform

    your operation After the operation, you then put the result

    back to the memory location Therefore, one form of operation that you

    will use very frequent is the store (move)

    operation!!! And using registers!!!!!

    12

  • 7/30/2019 8086 Assembly Language Programming I

    13/46

    In C++ A = B+C ; A, B, C are variables

    In assembly language A,B, C representingmemory locations so you cannot do A =B+C

    MOV AL, B ; move value of B into AL register ADD, AL, C ; do the add AL = AL +C

    MOV A, AL ; put the result to A

    13

  • 7/30/2019 8086 Assembly Language Programming I

    14/46

    AX, BX, CX,and DX these are the general purposeregisters but each of the registers also has specialfunction

    Example AX is called the accumulator to store result in arithmetic

    operations

    Registers are 16-bit but can be used as 2 8-bit storage Each of the 4 data registers can be used as the source or

    destination of an operand during an arithmetic, logic,shift, or rotate operation.

    In some operations, the use of the accumulator is

    assumed, eg in I/O mapped input and outputoperations

    14

  • 7/30/2019 8086 Assembly Language Programming I

    15/46

    In based addressing mode, base register BX isused as a pointer to an operand in the currentdata segment.

    CX is used as a counter in some instructions, eg.CL contains the count of the number of bits by

    which the contents of the operand must berotated or shifted by multiple-bit rotate DX, data register, is used in all multiplication

    and division, it also contains an input/outputport address for some types of input/output

    operations

    15

  • 7/30/2019 8086 Assembly Language Programming I

    16/46

    Stack is used as a temporary storage Data can be stored by the PUSH instruction

    and extracted by the POP instruction Stack is accessed via the SP (Stack Pointer)

    and BP (Base Pointer) The BP contains an offset address in the

    current stack segment. This offset address isemployed when using the based addressingmode and is commonly used by instructions

    in a subroutine that reference parametersthat were passed by using the stack

    16

  • 7/30/2019 8086 Assembly Language Programming I

    17/46

    Source index register (SI) and Destinationindex register (DI) are used to hold offsetaddresses for use in indexed addressing ofoperands in memory

    When indexed type of addressing is used,then SI refers to the current data segmentand DI refers to the current extra segment

    The index registers can also be used assource or destination registers in arithmeticand logical operations. But must be used in16-bit mode

    17

  • 7/30/2019 8086 Assembly Language Programming I

    18/46

    Data can be in three forms: 8-bit, 16-bit, or 32-bit (double word)

    Integer could be signed or unsigned and inbyte-wide or word-wide

    For signed integer (2s complement format), theMSB is used as the sign-bit (0 for positive, 1 for

    negative) Signed 8-bit integer 127 to 128, For signed word 32767 to 32768 Latest microprocessors can also support 64-bit

    or even 128-bit data In 8086, only integer operations are supported!!!

    18

  • 7/30/2019 8086 Assembly Language Programming I

    19/46

    19

    .code ; indicate start ofcode segment

    .startup ; indicate start of programmov AX, 0

    mov BX, 0000H

    mov CX, 0

    mov SI, AX

    mov DI, AX

    mov BP, AX

    END ; end of file

    The flow of the program is usually top-down and

    instructions are executed one by one!!!

  • 7/30/2019 8086 Assembly Language Programming I

    20/46

    20

    In general, an assembly program must include the code segment!!

    Other segments, such as stack segment, data segment are not

    compulsory

    There are key words to indicate the beginning of a segment as

    well as the end of a segment.Just like using main(){} in C++

    Programming

    ExampleDSEG segment data ; define the start of a data segment

    DSEG ENDS ; defines the end of a data segment

    Segment is the keyword DSEG is the name of the segment

    Similarly key words are used to define the beginning of a program,

    as well as the end.

  • 7/30/2019 8086 Assembly Language Programming I

    21/46

    21

    Example

    CSEG segment codeSTART PROC FAR ; define the start of a program (procedure)

    RET ; return

    START ENDP ; define the end of a procedure

    CSEG ends

    End start ; end of everything

    Different assembler may have different syntax for the definition

    of the key words !!!!!

    Startis just a name it could be my_prog, ABC etc

  • 7/30/2019 8086 Assembly Language Programming I

    22/46

    22

    Stacksg segmentpara stack

    . ; define the stack segment

    Stacksg ends

    Datasg segment para ; declare data inside the data segment

    Datasg ends

    Codesg segmentpara code

    Main proc far ;

    assume ss:stacksg, ds: datasg, cs:codesg

    mov ax, datasgmov ds, ax

    .

    mov ax, 4c00H

    int 21H

    Main endp

    Codesg endsend main

    End of everything

  • 7/30/2019 8086 Assembly Language Programming I

    23/46

    23

    To declare a segment, the syntax is:

    segment_nameSEGMENTalignment class

    Example StacksgsegmentPARA (this statement is used in previous slide)

    PARA define the alignment of the segment base address, the segment with a starting

    addressing that is evenly Divisible by 16. But the default value is also base address

    divisible by 16 so the key word PARA can be ignored!

  • 7/30/2019 8086 Assembly Language Programming I

    24/46

    data, code class entry. Is used to group

    related segments when linking. The linkerautomatically groups segments of the sameclass in memory

    PROC define procedures (similar to afunction) inside the code segment. Eachprocedure must be identified by an uniquename. At the end of the procedure, you

    must include the ENDP

    24

  • 7/30/2019 8086 Assembly Language Programming I

    25/46

    25

    FAR is related to program execution. When you request execution

    of a program, the program loader uses this procedure as the entry

    point for the first instruction to execute.

    Assume to associate, or to assign, the name of a segment with a segment register

    In some assembler, you need to move the base address of a segment directly into the

    segment register!!!END ends the entire program and appears as the last statement. Usually the name of the

    first or only PROC designated as FAR is put after END

  • 7/30/2019 8086 Assembly Language Programming I

    26/46

    If you are doing something simple then you donot need to define the segment

    Everything will be stored in the code segment

    26

  • 7/30/2019 8086 Assembly Language Programming I

    27/46

    start:

    mov DL, 0H ; move 0H to DL

    mov CL, op1 ; move op1 to CLmov AL, data ; move data to AL

    step:

    cmp AL, op1 ; compare AL and op1

    jc label1 ; if carry =1 jump to label1

    sub AL, op1 ; AL = ALop1inc DL ; DL = DL+1

    jmp step ; jump to step

    label1:

    mov AH, DL ; move DL to AH

    HLT ; Halt end of program

    data db 45 ; define a variable called data

    op1 db 6 ; define a variable called op1

    27

  • 7/30/2019 8086 Assembly Language Programming I

    28/46

    28

    Emu8086 (http:// www.emu8086.com) there is a trial version but

    it does not support all the features such as interrupt

    The emu8086 consists of a tutorial and the reference for acomplete instruction set

    Keil www.keil.com

    http://www.emu8086.com/http://www.emu8086.com/
  • 7/30/2019 8086 Assembly Language Programming I

    29/46

    29

    Data is usually stored in the data segment

    You can define constants, work areas (a chunk of memory )

    Data can be defined in different length (8-bit, 16-bit)8-bit then use DB 16-bit then use DW

    The definition for data:

    [name] Dn expression ; Dn is either DB or DW

    Name a program that references a data item by means of a name.

    The name of an item is otherwise optional

    Dn this is called the directives. It defines length of the data

    Expression define the values (content) for the data

  • 7/30/2019 8086 Assembly Language Programming I

    30/46

    30

    FLDA DB ? ; define an uninitialized item called FLDA 8-bit

    FLDB DB 25 ; initialize a data to 25

    Define multiple data under the same name (like an array)

    FLDC DB 21, 22, 23, 34 ; the data are stored in adjacent bytes

    FLDC stores the first value

    FLDC + 1 stores the second value

    You can do mov AL, FLDC+3

  • 7/30/2019 8086 Assembly Language Programming I

    31/46

    31

    DUP duplicate

    DUP can be used to define multiple storages

    DB 10 DUP (?) ; defines 10 bytes not initializeDB 5 DUP (12) ; 5 data all initialized to 12

    String :

    DB this is a test

    EQU this directive does not define a data item; instead, it definesa value that the assembler can use to substitute in other instructions

    (similar to defining a constant in C programming or using the #define )

    factor EQU 12

    mov CX, factor

  • 7/30/2019 8086 Assembly Language Programming I

    32/46

    Assembly language should be moreeffective and it will take up less memoryspace and run faster

    In real-time application, the use of assemblyprogram is required because program that is

    written in a high-level language probablycould not respond quickly enough

    You can also put assembly codes into yourC++ program in order to reduce the

    execution time!!!!

    32

  • 7/30/2019 8086 Assembly Language Programming I

    33/46

    The syntax for different microprocessor may bedifferent but the concept is the same so onceyou learn the assembly programming for one

    microprocessor, you can easily program otherkinds of system

    For example, programming the 8051 series isvery similar to the 8086

    33

  • 7/30/2019 8086 Assembly Language Programming I

    34/46

    Function of the addressing modes is toaccess the operands

    Available modes (9 modes): registeraddressing, immediate addressing, directaddressing, register indirect addressing,

    based addressing, indexed addressing,based indexed addressing, string addressing,and port addressing

    Addressing modes provide different ways

    of computing the address of an operand

    34

  • 7/30/2019 8086 Assembly Language Programming I

    35/46

    In c++, you can define an array, or a variable

    int x[10], y, *z; Then to access different elements, you can do

    Z = x ;

    *(x+2);

    x[0] = y

    How this can be done using assembly languageprogramming? This is via different addressing

    modes!!!!

    35

  • 7/30/2019 8086 Assembly Language Programming I

    36/46

    The operand to be accessed is specified asresiding in an internal register of the 8086

    Eg MOV AX, BX

    Move (MOV) contents of BX (the source

    operand), to AX (the destination operand) Both operands are in the internal registers

    36

  • 7/30/2019 8086 Assembly Language Programming I

    37/46

    37

  • 7/30/2019 8086 Assembly Language Programming I

    38/46

    38

    Pay attention to the value of IP and content of AX, BX

  • 7/30/2019 8086 Assembly Language Programming I

    39/46

    39

    Source operand is part of the instruction

    Usually immediate operands represent constant dataThe operands can be either a byte or word

    e.g MOV AL, 15

    15 is a byte wide immediate source operand

    Or it could be MOV AL, #15The immediate operand is stored in program storage

    memory (i.e the code segment)

    This value is also fetched into the instruction queue

    in the BIUNo external memory bus cycle is initiated!

  • 7/30/2019 8086 Assembly Language Programming I

    40/46

    40

  • 7/30/2019 8086 Assembly Language Programming I

    41/46

    41

  • 7/30/2019 8086 Assembly Language Programming I

    42/46

    Move a byte or word between a memorylocation and a register

    the locations following the instructionopcode hold an effective memory address

    (EA) instead of data The address is a 16-bit offset of the storage

    location of the operand from the currentvalue in the data segment register

    Physcial address = DS + offset The instruction set does not support a

    memory-to-memory transfer!

    42

  • 7/30/2019 8086 Assembly Language Programming I

    43/46

    Data is assumed to be stored in the datasegment so DS is used in calculating thephysical address!!!

    External memory bus cycle is needed to dothe read

    Example of direct addressing: mov AL, var1

    Where Var1 can be regarded as a variable

    43

  • 7/30/2019 8086 Assembly Language Programming I

    44/46

    Transfer a byte or word between a register and amemory location addressed by an index or baseregister Example MOV AL, [SI]

    SI index register

    The symbol [] always refer to an indirect addressing

    The effective address (EA) is stored either in apointer register or an index register

    The pointer register can be either base register BXor base pointer register BP

    The index register can be source index register SI, or

    destination index register DI The default segment is either DS or ES

    44

  • 7/30/2019 8086 Assembly Language Programming I

    45/46

    Eg MOV AX, [SI]

    Value stored in the SI register is used as theoffset address

    The segment register is DS in this example

    Meaning of the above is to move the datastored in the memory location : DS + SI tothe AX register

    In register indirect addressing mode, the EA(effective address) is a variable and dependson the index, or base register value

    Eg mov [BX], CL

    Which segment register will be used for the

    above operation 45

  • 7/30/2019 8086 Assembly Language Programming I

    46/46

    Address (in

    HEX) Content

    01236 1901235 1801234 2001233

    According to the memory map

    The result of the operation

    Mov [BX], CL will result in what???

    If CL = 88 and BX = 1233H and DS =0H

    Physical address = DS + BX = 01233H