m p lab presentation

Upload: krishna-chaitanya-t

Post on 30-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 M P Lab Presentation

    1/63

    T. Krishna Chaitanya

    Micro Processors Lab Demo

  • 8/9/2019 M P Lab Presentation

    2/63

    Assembly Language Today

    A program written directly in assembly language has thepotential to be smallerand to run fasterthan a HLL program

    But it takes too long to write a large program in assemblylanguage

    Only time-critical procedures are written in assemblylanguage (optimization for speed)

    Assembly language are often used in embedded systemprograms stored in PROM chips

    Computer cartridge games, micro controllers,

    Assembly Language is a low level programming language

  • 8/9/2019 M P Lab Presentation

    3/63

  • 8/9/2019 M P Lab Presentation

    4/63

    General Purpose Registers

    8086 CPU has 8 general purpose registers, each register has its own name:

    AX - the accumulator register (divided into AH / AL).

    BX - the base address register (divided into BH / BL).

    CX - the count register (divided into CH / CL).

    DX - the data register (divided into DH / DL).

    SI - source index register.

    DI - destination index register.

    BP - base pointer.

    SP - stack pointer.

  • 8/9/2019 M P Lab Presentation

    5/63

    Segment Registers

    CS - points at the segment containing the current program.

    DS - generally points at segment where variables are defined.ES - extra segment register, it's up to a coder to define its usage.

    SS - points at the segment containing the stack.

  • 8/9/2019 M P Lab Presentation

    6/63

  • 8/9/2019 M P Lab Presentation

    7/63

    8086 Instruction - Basic Structure

    Label Operator Operand[s] ;Comment

    Label- optional alphanumeric string1st character must be a-z,A-Z,?,@,_,$Last character must be :

    Operator- assembly language instructionmnemonic: an instruction format for humansAssembler translates mnemonic into hexadecimal opcodeexample: mov is f8h

    Operand[s]- 0 to 3 pieces of data required by instructionCan be several different formsDelineated by commasimmediate, register name, memory data, memory address

    Comment- Extremely useful in assembler language

    These fields are separated by White Space (tab, blank, \n, etc.)

  • 8/9/2019 M P Lab Presentation

    8/63

    8086 Instruction - Example

    Label Operator Operand[s] ;Comment

    INIT: mov ax, bx ; Copy contents of bx into ax

    Label - INIT:Operator - movOperands - ax andbx

    Comment - alphanumeric string between; and \n

    Not case sensitive

    Unlike other assemblers, destination operand is firstmov is the mnemonicthat the assembler translates into an

    opcode

  • 8/9/2019 M P Lab Presentation

    9/63

    Assembler Language Segment Types

    Stack

    For dynamic data storage

    Source file defines size

    Must have exactly 1

    Data

    For static data Storage

    Source file defines size

    Source file defines content (optional)

    Can have 0 or more

    Code

    For machine Instructions

    Must have 1 or more

  • 8/9/2019 M P Lab Presentation

    10/63

    x86 Instruction Type Classifications

    DATATRANSFER

    General mov ax, [DA T1] ;ax gets contents of mem

    Strings cmpsb ;if DS:SI=ES:DI then ZF=1

    Special Purpose xchg ax, bx ;ax gets bx and bx gets ax

    ARITHMETIC/LOGIC

    Integer add ax, bx ;ax gets ax+bx

    ASCII, BCD aaa ;changes ASCII # to int.

    Floating Point fadd DA T ;ST get ST+DAT

    Logical and ax, bx ;ax gets ax A ND bx

    Shifting ror ax, 2 ;ax contents shifted-2 right CONTROL TRANSFER

    Branching jnz LABEL1 ;if ZF=1 then IP=LABEL1

    Interrupt int 21h ;invoke INT handler 21h

    Subroutine call SUB1 ;invoke subroutine, SUB1

    Modify Flag cli ;IF gets zero

    Halt Processor hlt ;need R ESET to run again

    No Operation nop ;

  • 8/9/2019 M P Lab Presentation

    11/63

    Data Transfer Instructions

    MOV target, source

    reg, reg mem, reg

    reg, mem

    mem, immed

    reg, immed

    Sizes of both operandsmust be the same

    reg can be any non-

    segment registerexcept IP cannot be the

    target register

    MOV's between a

    segment register and

    memory or a 16-bitregister are possible

  • 8/9/2019 M P Lab Presentation

    12/63

    DataAllocation Directives

    db define byte

    dw define word (2 bytes)dd define double word (4 bytes)

    dq define quadword (8 bytes)

    dt define tenbytes

    equ equate, assign numeric expression to a name

    Examples:

    db 100 dup (?) define 100 bytes, with no initial values for bytes

    db Hello define 5 bytes, ASCII equivalent of Hello.

  • 8/9/2019 M P Lab Presentation

    13/63

    Sample MOV Instructions

    b db 4Fh

    w dw 2048

    mov bl,dh

    mov ax,w

    mov ch,b

    mov al,255mov w,-100

    mov b,0

    When a variable is created with a

    define directive, it is assigned adefault size attribute (byte, word,

    etc)

    You can assign a size attribute

    using LABEL

    LoByte LABEL BYTE

    aWord DW 97F2h

  • 8/9/2019 M P Lab Presentation

    14/63

    MOVAX, 0B800h ; setAX to hexadecimal value of B800h.

    MOV DS,AX ; copy value ofAX to DS.

    MOV CL, 'A' ; set CL toASCII code of 'A', it is 41h.

    MOV CH, 1101_1111b ; set CH to binary value.

    MOV BX, 15Eh ; set BX to 15Eh.

    MOV [BX], CX ; copy contents of CX to memory at B800:015E

    RET ; returns to operating system.

  • 8/9/2019 M P Lab Presentation

    15/63

    eXCHanGe

    XCHGtarget, source

    reg, reg reg, mem

    mem, reg

    MOV and XCHG

    cannot perform memory

    to memory moves

    This provides an

    efficient means to swapthe operands

    No temporary storage is

    needed

    Sorting often requires

    this type of operation This works only with the

    general registers

  • 8/9/2019 M P Lab Presentation

    16/63

    Arithmetic Instructions

    ADD dest, source

    SUB dest, sourceINC dest

    DEC dest

    NEGdest

    Operands must be ofthe same size

    source can be a

    general register,memory location, or

    constant

    destcan be a register

    or memory location

    except operands cannotboth be memory

  • 8/9/2019 M P Lab Presentation

    17/63

    Program Segment Structure

    Data Segments

    Storage for variables Variable addresses are

    computed as offsets from

    start of this segment

    Code Segment

    contains executableinstructions

    Stack Segment

    used to set aside storagefor the stack

    Stack addresses are

    computed as offsets into

    this segment

    Segment directives.data.code

    .stack size

  • 8/9/2019 M P Lab Presentation

    18/63

    Program Skeleton

    .model small

    .stack 100H

    .data;declarations

    .codemain proc

    ;codemain endp

    ;other procsend main

    Select a memory model

    Define the stack size Declare variables

    Write code organize into procedures

    Mark the end of the

    source file optionally, define the entry

    point

  • 8/9/2019 M P Lab Presentation

    19/63

  • 8/9/2019 M P Lab Presentation

    20/63

  • 8/9/2019 M P Lab Presentation

    21/63

    Write programme on the screen

  • 8/9/2019 M P Lab Presentation

    22/63

  • 8/9/2019 M P Lab Presentation

    23/63

  • 8/9/2019 M P Lab Presentation

    24/63

    eg: File name: [ d:\ add.asm..]

  • 8/9/2019 M P Lab Presentation

    25/63

  • 8/9/2019 M P Lab Presentation

    26/63

  • 8/9/2019 M P Lab Presentation

    27/63

  • 8/9/2019 M P Lab Presentation

    28/63

    Next step is to create executable file using the linker:

  • 8/9/2019 M P Lab Presentation

    29/63

    This causes linker to create: ADD.EXE

  • 8/9/2019 M P Lab Presentation

    30/63

  • 8/9/2019 M P Lab Presentation

    31/63

  • 8/9/2019 M P Lab Presentation

    32/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    33/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    34/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    35/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    36/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    37/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    38/63

  • 8/9/2019 M P Lab Presentation

    39/63

  • 8/9/2019 M P Lab Presentation

    40/63

  • 8/9/2019 M P Lab Presentation

    41/63

    ;add two 8-bit numbersdata segment

    addend db 20h

    adder db 10h

    sum db ?

    data ends

    code segment

    assume cs:code, ds:data

    start: mov ax,data ;

    mov ds,ax ;ds

  • 8/9/2019 M P Lab Presentation

    42/63

    data segment

    n dw 5

    res dw ?

    data ends

    code segment

    assume cs:code,ds:data

    mov ax,data

    mov ds,ax

    mov ax,n

    sub ax,1mov bx,ax

    add ax,1

    yy: mul bx

    dec bx

    jz down

    loop yydown:mov res,ax

    mov ax,4c00h

    int 21h

    code ends

    end

  • 8/9/2019 M P Lab Presentation

    43/63

  • 8/9/2019 M P Lab Presentation

    44/63

  • 8/9/2019 M P Lab Presentation

    45/63

    Write programme on the screen

  • 8/9/2019 M P Lab Presentation

    46/63

  • 8/9/2019 M P Lab Presentation

    47/63

  • 8/9/2019 M P Lab Presentation

    48/63

  • 8/9/2019 M P Lab Presentation

    49/63

  • 8/9/2019 M P Lab Presentation

    50/63

    Next step is to create executable file using the linker:

  • 8/9/2019 M P Lab Presentation

    51/63

    This causes linker to create: ADD.EXE

  • 8/9/2019 M P Lab Presentation

    52/63

    Write programme on the screen

  • 8/9/2019 M P Lab Presentation

    53/63

  • 8/9/2019 M P Lab Presentation

    54/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    55/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    56/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    57/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    58/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    59/63

    Press F 7

  • 8/9/2019 M P Lab Presentation

    60/63

  • 8/9/2019 M P Lab Presentation

    61/63

  • 8/9/2019 M P Lab Presentation

    62/63

    ;add two 8-bit numbersdata segment

    addend db 20h

    adder db 10h

    sum db ?

    data ends

    code segment

    assume cs:code, ds:data

    start: mov ax,data ;

    mov ds,ax ;ds

  • 8/9/2019 M P Lab Presentation

    63/63

    data segment

    n dw 5

    res dw ?

    data ends

    code segment

    assume cs:code,ds:data

    mov ax,data

    mov ds,ax

    mov ax,n

    sub ax,1mov bx,ax

    add ax,1

    yy: mul bx

    dec bx

    jz down

    loop yy

    down:mov res,ax

    mov ax,4c00h

    int 21h

    code ends