3 - assemby language programming for microprocessor

Upload: febrian-abdilah

Post on 01-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    1/42

    Fundamental of Assembly LanguageProgramming (for Microprocessor)

    Prima Dewi PurnamasariMicroprocessor

    Electrical Engineering Department

    Universitas Indonesia

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    2/42

    Computer Language

    High Level language Pascal, C, C++, Java, etc

    Low Level Language

    Assembly

    Machine Codes 010010001010100101010 in binary

    1234 FFAB 1234 H in hexadecimal

    2 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    3/42

    Why Assembly?

    Assembly has several features that make it a good choice manysome situations.

    1. It's fast  –  Assembly programs are generally faster than

    programs created in higher level languages. Often,programmers write speed-essential functions in assembly.

    2. It's powerful  –  You are given unlimited power over your

    assembly programs. Sometimes, higher level languages haverestrictions that make implementing certain things difficult.

    3. It's small  –  Assembly programs are often much smaller

    than programs written in other languages. This can be veryuseful if space is an issue.

    3 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    4/42

    Preparation for Assembly Programming

    Basically you will need: Program editor as simple as Notepad

    Assembler

    1. MASM http://www.masm32.com/.

    2.

    TASM

     Made by Borland, a commercial product3. NASM http://sourceforge.net/projects/nasm/ 

    Be careful in writing your programs, because it runs directlyon your microprocessor!

    4 Microprocessor (c) Prima Dewi Purnamasari 2011

    http://www.masm32.com/http://sourceforge.net/projects/nasm/http://sourceforge.net/projects/nasm/http://www.masm32.com/

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    5/42

    Steps to Create a

    Program

    5 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    6/42

    Creating an Assembly Language Program

    An assembly language program should be written with anytext editor and have the extension filename.asm.

    The assembler and Linker

    The assembler program converts a symbolic source module 

    (file) into a hexadecimal object file  The linker program executes as the second part of ML, reads the

    object files, created by the assembler program, and links them intoa single execution file (.EXE)

    6 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    7/42

     

    7 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    8/42

    MASM32

    8 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    9/42

     TASM

    9 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    10/42

    Emulator

    anemulator 

     is hardware and/or software that duplicates (oremulates) the functions of a first computer system in a different

    second computer system, so that the behavior of the second

    system closely resembles the behavior of the first system.

    10 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    11/42

    Emu8086

    11 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    12/42

    Individual Assignment

    Download and install emu8086 (trial)

    http://www.emu8086.com/ 

    Find corresponding tutorial on how to use it (available on the

    Internet!), self study!

    12 Microprocessor (c) Prima Dewi Purnamasari 2011

    http://www.emu8086.com/http://www.emu8086.com/

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    13/42

    Group assignment

    Each group is responsible to bring at minimum 1 laptop(with emu8086 installed) to class every session

    13 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    14/42

    Assembly Program Structure

    14 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    15/42

    15 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    16/42

    LIST File, generated

    automatically after

    program successfullyassembled

    MachinecodesMemoryAddress

    16 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    17/42

    Writing Structure

    NEXT: MOV AX, [BX] ; comment

    1= label, followed by “:” 

    2= opcode 3= operand

    4= comment, preceded with”;” 

    1 432

    17 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    18/42

    Writing Structure

    Each statement in an assembly language program consistsof four parts or fields.

    The leftmost field is called the label.

    used to store a symbolic name for the memory location it

    represents

    All labels must begin with a letter or one of the following

    special characters: @, $, -, or ?.

    a label may have any length from 1 to 35 characters

    The label appears in a program to identify the name of a

    memory location for storing data and for other

    purposes.

    18 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    19/42

    The next field to the right is the opcode field. designed to hold the instruction, or opcode

    the MOV part of the move data instruction is an exampleof an opcode

    Right of the opcode field is the operand  field. contains information used by the opcode

    the MOV AL,BL instruction has the opcode MOV andoperands AL and BL

    The comment field, the final field, contains a comment aboutthe instruction(s).

    comments always begin with a semicolon (;)

    19 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    20/42

    Try it in emulator! Click “View” and look the changes in every menu list: 

    registers

    Data

    Screen Flags

    etc

    20 Microprocessor (c) Prima Dewi Purnamasari 2011

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    21/42

    Computer Data Formats

    Microprocessor (c) Prima Dewi Purnamasari 201121

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    22/42

    Computer Data Formats

    ASCII and Unicode Data Binary Coded Decimal (BCD)

    Byte-Sized Data

    Word-Sized Data

    Doubleword-Sized Data

    Real Numbers

    Microprocessor (c) Prima Dewi Purnamasari 201122

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    23/42

    ASCII Data

    American Standard Code for Information Interchange(ASCII) data represent alphanumeric characters in thememory of a computer system (Table 1.7)

    The standard ASCII code is a 7-bit code with the eighth and

    MSB used to hold parity in some systems ASCII are most often stored in memory using a special

    directive to the assembler program called define byte(s) or DB 

    Microprocessor (c) Prima Dewi Purnamasari 201123

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    24/42

    Microprocessor (c) Prima Dewi Purnamasari 201124

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    25/42

    BCD Data

    Binary-Coded Decimal (BCD) information is stored ineither packed or unpacked forms

    Packed BCD data are stored as two digits per byte

    Unpacked BCD data are stored as one digit per byte

    The range of a BCD digit extends from 00002 to 10012 or0-9 decimal

    Table 1.9 shows some decimal numbers converted to bothpacked ad unpacked BCD

    Microprocessor (c) Prima Dewi Purnamasari 201125

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    26/42

     

    Microprocessor (c) Prima Dewi Purnamasari 201126

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    27/42

    Byte-Sized Data

    Byte-size data are stored as unsigned and signed integers Negative signed numbers are stored in the 2’s complement

    form

    Whenever a number is 2’s complement, its sign changes from

    negative to positive or positive to negative See example 1-22, 1-23

    Microprocessor (c) Prima Dewi Purnamasari 201127

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    28/42

     

    Microprocessor (c) Prima Dewi Purnamasari 201128

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    29/42

    Define byte (DB) directive is used to store 8-bit data

    in memory

    Microprocessor (c) Prima Dewi Purnamasari 2011 29

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    30/42

    Word-sized Data

    A word (16-bits) is formed with two bytes of data

    The LSB is always stored in the lowest-numbered memorylocation, the MSB in the highest (i.e., little endian format) — used with Intel family of microprocessor

    An alternate method (i.e., big endian format) is used with theMotorola family of micro-processors

    Microprocessor (c) Prima Dewi Purnamasari 201130

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    31/42

    Word-sized Data

    Fig 1.11(a) & (b) shows the weight of each bit position in aword of data

    Microprocessor (c) Prima Dewi Purnamasari 201131

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    32/42

     

    Microprocessor (c) Prima Dewi Purnamasari 201132

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    33/42

    Example 1.25 shows several signed and unsigned word-sizeddata stored in memory using the assembler program

    Note that define word(s) directive or DW causes theassembler to store words in the memory

    Microprocessor (c) Prima Dewi Purnamasari 201133

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    34/42

     

    Microprocessor (c) Prima Dewi Purnamasari 201134

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    35/42

    Doubleword-sized Data

    Doubleword-sized data requires four bytes of memory(32-bit number)

    Doubleword-sized data appear as a product after amultiplication and also as a dividend before a division

    Fig. 1-12 shows the form used to store doublewords in thememory and the binary weights of each bit position

    Microprocessor (c) Prima Dewi Purnamasari 201135

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    36/42

     

    Microprocessor (c) Prima Dewi Purnamasari 201136

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    37/42

    To define doubleword-sized data, use assembler

    directive define doubleword or DD

    Microprocessor (c) Prima Dewi Purnamasari 2011 37

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    38/42

    Real Numbers

     A real number (floating-point number) contains two parts:a mantissa, significant, or fraction and an exponent

    Fig. 1-13 and example 1-27 depicts both the 4-byte (singleprecision) and 8-byte (double precision) forms of real

    numbers

    Microprocessor (c) Prima Dewi Purnamasari 201138

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    39/42

     

    Microprocessor (c) Prima Dewi Purnamasari 201139

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    40/42

    The exponent is stored as a biased exponent

    an exponent of 23  is represented as a biased exponent of 127+3or 130 (82H) in the single- precision form or as 1026 (402H) in thedouble-precision form

    Microprocessor (c) Prima Dewi Purnamasari 201140

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    41/42

     

    Microprocessor (c) Prima Dewi Purnamasari 201141

  • 8/9/2019 3 - Assemby Language Programming for Microprocessor

    42/42

    Reference/Text Book

    “The Intel Microprocessors”, 8th

     Edition, Brey, Barry, B.,Prentice Hall, USA, 2009