Essentials of 80x86 Assembly Language - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Essentials of 80x86 Assembly Language

Description:

Use a text editor like edit (at the MS-DOS prompt) or notepad, not a word processor ... Microsoft Assembler. For source example.asm, invoked at the command prompt with ... – PowerPoint PPT presentation

Number of Views:277
Avg rating:3.0/5.0
Slides: 35
Provided by: richard101
Category:

less

Transcript and Presenter's Notes

Title: Essentials of 80x86 Assembly Language


1
Chapter 2 Software Tools and Assembly Language
Syntax
2
2.1 Assembly Language Statements and Text Editors
3
Assembly Language Syntax
  • Example assembly language program -- adds 158
    to number in memory
  • Author R. Detmer
  • Date 10/2004
  • .386
  • .MODEL FLAT
  • ExitProcess PROTO NEAR32 stdcall,
    dwExitCodeDWORD
  • .STACK 4096 reserve 4096-byte stack
  • .DATA reserve storage for
    data
  • number DWORD -105
  • sum DWORD ?
  • .CODE start of main
    program code
  • _start
  • mov eax, number first number to
    EAX
  • add eax, 158 add 158
  • mov sum, eax sum to memory
  • INVOKE ExitProcess, 0 exit with
    return code 0
  • PUBLIC _start make entry
    point public
  • END end of source
    code

comments
directives
instructions
comments
4
Comments
  • Start with a semicolon ()
  • Extend to end of line
  • May follow other statements on a line

5
Instructions
  • Each corresponds to a single instruction actually
    executed by the 80x86 CPU
  • Examples
  • mov eax, numbercopies a doubleword from memory
    to the accumulator EAX
  • add eax, 158adds the doubleword representation
    of 158 to the number already in EAX, replacing
    the number in EAX

6
Directives
  • Provide instructions to the assembler program
  • Typically dont cause code to be generated
  • Examples
  • .386 tells the assembler to recognize 32-bit
    instructions
  • DWORD tells the assembler to reserve space for a
    32-bit integer value

7
Macros
  • Each is shorthand for a sequence of other
    statements instructions, directives or even
    other macros
  • The assembler expands a macro to the statements
    it represents, and then assembles these new
    statements

8
Typical Statement Format
  • name mnemonic operand(s) comment
  • In the data segment, a name field has no
    punctuation
  • In the code segment, a name field is followed by
    a colon ()
  • Some of these fields may be omitted in some
    statements

9
Identifiers
  • Identifiers used in assembly language are formed
    from letters, digits and special characters
  • Special characters are best avoided except for an
    underscore (_) with _start.
  • An identifier may not begin with a digit
  • An identifier may have up to 247 characters
  • Restricted identifiers include instruction
    mnemonics, directive mnemonics, register
    designations and other words which have a special
    meaning to the assembler

10
Program Format
  • Indent for readability, starting names in column
    1 and aligning mnemonics and trailing comments
    where possible
  • The assembler is not case-sensitive but good
    practice is to
  • Use lowercase letters for instructions
  • Use uppercase letters for directives

11
Creating a Assembly Language Source Code File
  • Use a text editor like edit (at the MS-DOS
    prompt) or notepad, not a word processor
  • Save program with .asm extension

12
2.2 The Assembler
13
MASM
  • Microsoft Assembler
  • For source example.asm, invoked at the command
    prompt withml /c /coff /Fl /Zi example.asm
  • Switches
  • /c compile only
  • /coff generate special file format
  • /Fl generate assembly listing
  • /Zi prepare for debugging
  • ml /? give all switches

Blanks are necessary
14
Output of Assembler
  • Object file, e.g., example.obj
  • Contains machine language statements almost ready
    to execute
  • Listing file, e.g., example.lst
  • Shows how MASM translated the source program

15
Listing File
locations of data relative to start of data
segment
8 bytes reserved for data, with first doubleword
initialized to -105
  • 00000000 .DATA
  • 00000000 FFFFFF97 number DWORD -105
  • 00000004 00000000 sum DWORD ?
  • 00000000 .CODE
  • 00000000 _start
  • 00000000 A1 00000000 R mov eax,
    number
  • 00000005 05 0000009E add eax,
    158
  • 0000000A A3 00000004 R mov sum, eax

object code for the three instructions
locations of instructions relative to start of
code segment
16
Parts of an Instruction
  • Instructions object code begins with the opcode,
    usually one byte
  • Example, A1 for mov eax, number
  • Immediate operands are constants embedded in the
    object code
  • Example, 0000009E for add eax, 158
  • Addresses are assembly-time must be fixed when
    program is linked and loaded
  • Example, 00000004 for mov sum, eax

17
2.3 The Linker
18
Functions of the Linker
  • Combines separately assembled modules into a
    single module, ready to be loaded into memory
  • Arranges the individual object modules
    end-to-end, fixing up addresses for the resulting
    load module
  • Load module is copied to memory when the program
    is actually executed, and additional address
    correction may take place at load time

19
Using the Linker
  • At the command prompt
  • link /debug /subsystemconsole /entrystart
  • /outexample.exe example.obj kernel32.lib
  • (entered as a single command)
  • This command links example.obj and any needed
    procedures from the library file kernel32.lib to
    produce the output file example.exe

20
Link switches
  • /outexample.exe specifies example.exe as the
    name of the executable program file
  • /entrystart identifies _start as the label of
    the program entry point
  • /debug tells the linker to generate files
    necessary for debugging, example.ilk and
    example.pdb
  • /subsystemconsole tells the linker to generate
    code for a console application, one that runs in
    a MS-DOS window

21
2.4. The Debugger
22
Functions of a Debugger
  • Allows a programmer to control execution of a
    program, pausing after each instruction or at a
    preset breakpoint
  • A programmer can examine the contents of
    variables in a high-level language, or registers
    or memory in assembly language
  • Useful both to find errors and to see inside a
    computer to find out how it executes programs

23
Using WinDbg (1)
  • Type Windbg at the command prompt
  • From the WinDbg menu bar choose File, then Open
    Executable. Select example.exe, or the name of
    your executable file
  • Press the step into button

24
Using WinDbg (2)
  • Click OK in the information window No symbolic
    Info for Debugee source code then appears in a
    Windbg child window behind the Command window
  • Minimize the Command window
  • Select View and then Registers to open a window
    that shows contents of the 80x86 registers

25
Using WinDbg (3)
  • Select View and Memory to open a window that
    shows contents of memory
  • Enter the starting memory address using the C/C
    address-of operator ()
  • For example, if the first item in the data
    section is number, you could use number as the
    starting address

26
Using WinDbg (4)
  • The instruction about to be executed is
    highlighted in yellow.
  • Press the step into button to execute each
    instruction one at a time
  • When an instruction causes a register value to
    change, the new value is shown in red

27
WinDbg Display
28
2.5. Data Declarations
29
BYTE Directive
  • Reserves storage for one or more bytes of data,
    optionally initializing storage
  • Numeric data can be thought of as signed or
    unsigned
  • Characters are assembled to ASCII codes
  • Examples
  • byte1 BYTE 255 value is FF
  • byte2 BYTE 91 value is 5B
  • byte3 BYTE 0 value is 00
  • byte4 BYTE -1 value is FF
  • byte5 BYTE 6 DUP (?) 6 bytes each with 00
  • byte6 BYTE 'm' value is 6D
  • byte7 BYTE "Joe" 3 bytes with 4A 6F 65

30
DWORD Directive
  • Reserves storage for one or more doublewords of
    data, optionally initializing storage
  • Examples
  • double1 DWORD -1 value is FFFFFFFF
  • double2 DWORD -1000 value is FFFFFC18
  • double3 DWORD -2147483648 value is 80000000
  • double4 DWORD 0, 1 two doublewords
  • Double5 DWORD 100 DUP (?) 100 doublewords

31
WORD Directive
  • Reserves storage for one or more words of data,
    optionally initializing storage

32
2.6 Instruction Operands
33
Types of Instruction Operands
  • Immediate mode
  • Constant assembled into the instruction
  • mov al,/ add eax, 135
  • Register mode
  • A code for a register is assembled into the
    instruction
  • mov sum,eax
  • Memory references
  • Several different modes

34
Memory References
  • Direct at a memory location whose address
    (offset) is built into the instruction
  • The memory references in the example program are
    direct
  • mov al,number1
  • Register indirect at a memory location whose
    address is in a register
  • add eax,edxthe destination is a doubleword
  • mov BYTE PTRebx,0PTR operator declares the
    size of operand
Write a Comment
User Comments (0)
About PowerShow.com