Detmer Ch2 Ppt

download Detmer Ch2 Ppt

of 49

Transcript of Detmer Ch2 Ppt

  • 8/10/2019 Detmer Ch2 Ppt

    1/49

    Chapter 3

    Elements of Assembly Language

    How to develop assembly Language Programs

    Directives in MASM

  • 8/10/2019 Detmer Ch2 Ppt

    2/49

    Contents

    Types and formats of statements in MASM

    Example program for how to assemble, link,and execute

    Structure of assembly language program

    Constant operands and instructionoperands

  • 8/10/2019 Detmer Ch2 Ppt

    3/49

    3.2 A Complete Example

    Purpose: prompt for two numbers and

    then find and display their sum.

  • 8/10/2019 Detmer Ch2 Ppt

    4/49

    Pseudocode design for the program1. Prompt for the first number;

    2. Input ASCII characters representing the first number;

    3. Convert the characters to a 2s complement doubleworld

    4. Store the first number in memory;

    5. Prompt for the second number;

    6. Input ASCII characters representing the first number;

    7. Convert the characters to a 2s complement double

    world8. Add the first number to the second number;

    9. Convert the sum to a string of ASCII characters;

    10. Display a label and the characters representing the sum;

  • 8/10/2019 Detmer Ch2 Ppt

    5/49

    ;Example assembly language program -- adds two

    numbers

    ;Author: R. Detmer

    ; Date: revised 7/97

    .386

    .MODEL FLAT

    Structure of the program(1)comment

    directives

    Accept 80386 instruction

    Flat memory model

  • 8/10/2019 Detmer Ch2 Ppt

    6/49

    Structure of the program(2)

    ExitProcess PROTONEAR32 stdcall, dwExitCode:DWORD

    INCLUDEio.h ; header file for input/output

    cr EQU 0dh ; carriage return characterLf EQU 0ah ; line feed

    Prototype a function()

    Copy file

    Equate symbols to values

  • 8/10/2019 Detmer Ch2 Ppt

    7/49

    .STACK 4096 ; reserve 4096-byte stack

    Structure of the program(3)

    Declare the Size of runtime stack

  • 8/10/2019 Detmer Ch2 Ppt

    8/49

    Structure of the program(3)

    .DATA ; reserve storage for data

    number1 DWORD ?

    number2 DWORD ?

    Starts the data segment

    Reserve a single

    doubleword of storageSymbolic

    name

  • 8/10/2019 Detmer Ch2 Ppt

    9/49

    Structure of the program(4)

    prompt1 BYTE "Enter first number: ", 0prompt2 BYTE "Enter second number: ", 0

    string BYTE 40 DUP(?)label1 BYTE cr, Lf, "The sum is "sum BYTE 11 DUP(?)

    BYTE cr, Lf, 0

    Reserve a byte of

    storage

    Repeat items

    in

    parentheses

  • 8/10/2019 Detmer Ch2 Ppt

    10/49

  • 8/10/2019 Detmer Ch2 Ppt

    11/49

    output prompt2 ; repeat for second numberinput string, 40atod string

    mov number2, eaxmov eax, number1 ; first number to EAXadd eax, number2 ; add second numberdtoa sum, eax ; convert to ASCII characters

    output label1 ; output label and sum

  • 8/10/2019 Detmer Ch2 Ppt

    12/49

    INVOKE ExitProcess, 0 ; exit with return code 0

    PUBLIC _start ; make entry point public

    END ; end of source code

    Call the procedure

    Make the label visible outside the file

    Marks the physical ends of the program

  • 8/10/2019 Detmer Ch2 Ppt

    13/49

    Template for assembly language

    ;Comments for your program

    .386

    .MODEL FLAT, stdcall

    includelib lib\kernel32.lib

    ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

    .STACK 4096 ; reserve 4096-byte stack

    .DATA ; reserve storage for data

    {;data define;}

    .CODE ; start of main program code

    start:{;your codes;}

    INVOKE ExitProcess, 0 ;exit with return code 0

    ret

    END start ;end of source code

  • 8/10/2019 Detmer Ch2 Ppt

    14/49

    3.1 Assembly Language

    StatementsLength: up to 512

    If not enough, use backslash(\) character at

    the end of a line

    Comments:

    Begin with the semicolon(;) and extends

    until the end of the line

  • 8/10/2019 Detmer Ch2 Ppt

    15/49

    Three types of functional assembly

    language statements InstructionsCan be translated into machine code( object

    code)DirectivesTell the assembler to take some action , not

    result in machine code

    macrosShorthand for a sequence of other

    statements

  • 8/10/2019 Detmer Ch2 Ppt

    16/49

    Components of a statement

    name: mnemonic operands ;comment

  • 8/10/2019 Detmer Ch2 Ppt

    17/49

    3.3 How to Assemble , Link and

    Run a Program

    Edit source code

    Assemble source code

    Link

    execute

  • 8/10/2019 Detmer Ch2 Ppt

    18/49

    Make executable file

    Assemble

    ml /c /coff /Zi example.asm

    LinkLink /subsystem:console /entry:start

    /out:example.exe /debug example.obj io.obj

    kernel32.libRun

    example

  • 8/10/2019 Detmer Ch2 Ppt

    19/49

    3.4 The Assemble Listing File

    Help to understand

    the assembly process

    Shows the sourcecode, the object code

    and additional

    information

    ml /c /coff /Zi /Fl example.asm

  • 8/10/2019 Detmer Ch2 Ppt

    20/49

    First part of listing file

    Microsoft (R) Macro Assembler Version 6.14.8444 09/15/06 08:11:53

    D:\masm32\HelloCon.asm Page 1 - 1

    Offset address ofeach directives or

    instructions

    Object code ofinstructions or

    value assigned by

    directives

    statements

    Representing data in hexdicimal

  • 8/10/2019 Detmer Ch2 Ppt

    21/49

  • 8/10/2019 Detmer Ch2 Ppt

    22/49

    3.5 Variable Operands

    Numeric operands

    Directives:

    Byte

    Word

    Dword

    Radix

    Changes the default number base

    Assembler assumes that a number is decimal

  • 8/10/2019 Detmer Ch2 Ppt

    23/49

    Constant operands

    EQU

    Symbol EQU value

    =

    Symbol = value

    Can be redefined

  • 8/10/2019 Detmer Ch2 Ppt

    24/49

    Model define directives

    Select Instruction set

    .386, .486, .586,386p, .mmx

    Option

    Option casemap:none

    mode definemodel memory mode, [call], [others]

  • 8/10/2019 Detmer Ch2 Ppt

    25/49

    Memory mode

    Memory

    mode

    Max.

    size of

    seg.

    File type OS Code seg. Data seg.

    Tiny 64KB .com DOS One seg.

    Small 64KB .exe DOS 1 1

    Medium 64KB .exe DOS 1 Multi.

    Compact 64KB .exe DOS Multi. 1

    Large 64KB .exe DOS Multi. Multi.

    Huge 64KB .exe DOS Multi. Multi.

    flat 4GB .exe(PE) Windows Same seg.

  • 8/10/2019 Detmer Ch2 Ppt

    26/49

    Include and includelib

    Include filename

    Insert other files in current file

    Includelib filename

    Tell the linker use library file named by

    filename.

    Insert function code in .exe file.

  • 8/10/2019 Detmer Ch2 Ppt

    27/49

    Declare function

    Function_name PROTO[call]

    [parameter1name:parameter1 type],

    Call:

    C, stdcall,

  • 8/10/2019 Detmer Ch2 Ppt

    28/49

    Data Area define

    .data

    Begin a data segment

    .data?Not initial data

    .const

    Define constant .stack [size]

    Define stack

  • 8/10/2019 Detmer Ch2 Ppt

    29/49

    Code area define

    .code

    Begin code segment

    END [LABEL]

    The last statement in the program.

    Label shows the entry point of the program.

  • 8/10/2019 Detmer Ch2 Ppt

    30/49

    Laboratory Exercise1

    Build the developing environment forprogramming using assembly language.

    Test HelloCon.asm and HelloWin.asm.

    Make a program complete the followingfunction: s=x+y. Use debugger to prove youprogram is correct.

    Report:Introduce your developing environment.

    Write your program. Introduce the debuggingstep that you used.

  • 8/10/2019 Detmer Ch2 Ppt

    31/49

    3.6 Instruction Operands

    Two operands

    Destination operands

    Source operands

    Constants

    Designate CPU registersReference memory locations

  • 8/10/2019 Detmer Ch2 Ppt

    32/49

    Addressing modes

    Giving the location of the data for eachmode

    Immediate addressing mode:in the instruction itself

    Register addressing mode:

    in a registerMemory addressing mode :

    at some address in memory

  • 8/10/2019 Detmer Ch2 Ppt

    33/49

    MOV instruction

    We use MOV instruction introduce

    addressing mode.

    MOV dest , src; src -> dest

  • 8/10/2019 Detmer Ch2 Ppt

    34/49

    Immediate addressing mode

    Operand itself is in the instruction.

    Eaxmple:

    MOV eax , 100; 100 -> eax

    Result: eax=00000064h

    Machine code: B8 00000064

    MOV al , / ; 2F -> al, B0 2FADD eax,135 ; eax+135->eax

    ;05 00000087

  • 8/10/2019 Detmer Ch2 Ppt

    35/49

    Register addressing mode

    Operand is in a register.

    MOV eax, ebx; ebx -> eax

    MOV eax, 100 ; 100 -> eax

    8-bit registers: AH, AL and DL

    16-bit registers: AX, BX and DI32-bit registers: EAX, EBX and EDI

    segment registers: CS, ES, DS, SS, FS and GS

  • 8/10/2019 Detmer Ch2 Ppt

    36/49

    Not allowed Never

    mix an 8-bit register with a 16-bit register, an8-bit register with a 32-bit register, or a 16-bit register

    with a 32-bit register because this is not allowed by

    the microprocessor and results in an error when

    assembled. A segment-to-segment register MOV instruction is not

    allowed. Changing the CS register with a MOV

    instruction is notallowed.

    MOV ES,DS ; not allowed

    MOV BL,DX ; not allowed

    MOV CS,AX ; not allowed

  • 8/10/2019 Detmer Ch2 Ppt

    37/49

    Memory addressing modes

    Operand is stored in memory.

    How to calculate memory addressDirect:

    memory address is built into the instruction

    Register indirect:memory address is in a register

  • 8/10/2019 Detmer Ch2 Ppt

    38/49

    Direct addressing mode

    Operand has the 32-bit address built into theinstruction.

    Programmer often codes a symbol that isassociated with a BYTE, DWORD, or WORDdirective in the data segment or with aninstruction in the code segment.

    The location corresponding to such a symbolwill be relocatable so that the assembler listingshows an assembly-time address that may beadjusted later.

  • 8/10/2019 Detmer Ch2 Ppt

    39/49

    ADD eax, number2;05 00000004

    Note:

    value BYTE 100

    MOV eax , value

    ;;;;;

    value EQU 100

    MOV eax , value

  • 8/10/2019 Detmer Ch2 Ppt

    40/49

  • 8/10/2019 Detmer Ch2 Ppt

    41/49

    Example for indirect addressing

    value DWORD 100

    MOV edx, value ; edx=100

    MOV edx , OFFSET value

    ; edx=offset address of valueMOV eax , [edx] ;eax=100

  • 8/10/2019 Detmer Ch2 Ppt

    42/49

    Register used for indirect mode

    Registers EAX, EBX, ECX, EDX, ESI, EDI

    can be used for register indirect

    addressing.(in data segment)

    Register EBPcan be used for register indirect

    addressing an address in stack segment.

    ESPalso can be used for register indirectaddressing.But we seldom use it.

    For 16-bit:only use BX, SI, DI, BP

  • 8/10/2019 Detmer Ch2 Ppt

    43/49

    Size of the memory operand

    When the size of the memory operand isambiguous, the PTR directive must be used togive the size to the assembler.

    MOV [ebx] , 0

    MOV BYTE PTR[ebx], 0

    MOV eax, [edx];no need use PTR,the size of dest

    operand is the same as the src operand.WORD PTR, DWORD PTR used for word

    or doubleword operands respectively.

  • 8/10/2019 Detmer Ch2 Ppt

    44/49

    Base-plus-index addressing

    For 16-bit:use one base register (BX or BP),

    and one index register (DI or SI) to indirectly

    address memory.

    MOV CX,[BX+DI] ;16-bit, CX = DS:[BX+DI]

    MOV CH,[BP+SI] ;8-bit, CH = SS:[BP+SI]

    For 32-bit:allows the combination of any two

    32-bit extended registers except ESP.

    MOV CL,[EDX+EDI] ;8-bit, CL=DS:[EDX+EDI]

    MOV [EAX+EBX],ECX;32-bit, DS:[EAX+EBX] =ECX

    R i t l ti dd i

  • 8/10/2019 Detmer Ch2 Ppt

    45/49

    Register relative addressing

    In register relative addressing, the data in asegment of memory are addressed by addingthe displacementto the contents of a base or anindex register (BX, SI, DI, or BP).

    MOV AX,[DI+100H] ;16-bit, AX = DS:[DI+100H]

    MOV ARRAY[SI],BL;8-bit, ARRAY[SI] = BL

    For 32-bit, any of the 32-bit general-purpose

    registers can be used in the register relativeaddressing.

    MOV DI,[EAX+10] ;16-bit, DI = DS:[EAX+10]

    MOV ARRAY[EBX],EAX;32-bit, ARRAY[EBX] = EAX

  • 8/10/2019 Detmer Ch2 Ppt

    46/49

    Base relative-plus-index addressing

    Adds a displacement, besides using a baseregister (BX or BP) and an index register (SI orDI), to form the memory address.

    MOV DH,[BX+DI+20H]MOV AX,FILE[BX+SI]

    MOV LIST[BP+DI],CL

    MOV LIST[BP+SI+4],DH

    MOV EAX,FILE[EBX+ECX+2] In 80386 and above, all the 32-bit extended

    registers are both base register and indexregister.

  • 8/10/2019 Detmer Ch2 Ppt

    47/49

    Scaled-index addressing

    This data-addressing mode is unique to the80386 and above. It uses one or two 32-bitregisters to access the memory. The secondregister is multiplied by a scaling factor (1, 2, 4,

    or 8).MOV EAX,[EBX+4*ECX]

    MOV [EAX+2*EDI+100H],CX

    MOV AL,[EBP+2*EDI-2]

    MOV EAX,ARRAY[4*ECX]

    MOV AL,[2*EBX]

    A scaling factor of 1X is implied and need not beincluded in the assembly language instruction.

  • 8/10/2019 Detmer Ch2 Ppt

    48/49

    3.7 Input/Output Using Macros

    Defined in IO.HRead this part youselves, and do the

    exercises. If you have any question,

    please put up your hand and ask me or

    discuss with others.

  • 8/10/2019 Detmer Ch2 Ppt

    49/49

    Exercises

    P45. Exercises3.1 1, 2

    P67. Exercises3.4 2, 3, 4

    P72. Exercises3.5 1-32

    P77. Exercises3.6 1-8

    P80. Exercises3.7 1-6