CS 221 IT 221 Lecture 06 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CS 221 IT 221 Lecture 06

Description:

Display 'names' for variables and routines ... Logical (and, or, xor, not, test) Shifts and rotates (shl, shr, sal, sar, ror, rol) ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 25
Provided by: csN2
Category:

less

Transcript and Presenter's Notes

Title: CS 221 IT 221 Lecture 06


1
CS 221/ IT 221Lecture 06
  • Assembly Instructions
  • and Intel 80x86
  • Dr. Jim Holten

2
Assembly Language Overview
  • Assemblers
  • Inline assembly in C programs
  • Debuggers
  • Machine Code
  • Memory Maps
  • Assembly Mnemonics

3
Assemblers
  • Masm.exe MS Windows, requires Visual C
  • ML.exe Detmers disk, MS Windows
  • as Linux and other OSs, assembles to many
    architectures (IA64 especially)
  • nasm Linux, widely used 80x86 assembler
  • MANY others, as people write their own.

4
Assemblers (cont)
  • Input assembly language code
  • Assembler directives
  • Code and data definition mnemonics
  • Converts to relocatable machine code (object
    files), which may have external references to
    other routines and data
  • Linker combines with other object files to define
    external references
  • Loader reads into memory, relocates to memory
    locations, and starts its execution.

5
Assembly Inline in C Program
  • Start with normal C code file
  • Use the assembly function call
  • __asm__ __volatile__(ltstring containing assembly
    codegt)
  • Any C variables to be referenced inside the
    assembly code need to be declared, for example
  • static int apples asm(apple) 25
  • The compile line needs to be told to use intel
    assembly conventions
  • gcc masmintel test.c o test
  • OR generate the assembly source code file (test.s
    here)
  • gcc masmintel test.c S

6
Debuggers
  • Load the code (uses the loader, but does not run
    the code yet)
  • Give interactive access to the code and data
    locations and their contents
  • Allow interactive control of the code execution
    through breakpoints and single stepping
  • May have a GUI (Windbg and IDEs) or be
    command-line driven (gdb)

7
Windbg Windows Debugger
  • GUI-based IDE for Microsoft systems (only)
  • Loads files from ML.exe and reassembles them
    using ML.exe as requested
  • Allows interactive control of program execution
  • Allows interactive viewing of intermediate
    processor, register, and memory states
  • Emulates and controls the execution as needed

8
gdb (GNUs Linux debugger)
  • Command line-based
  • Enables interactive control
  • Enables code loading and execution
  • Allows breakpoints and single stepping
  • View or modify variable values and register
    contents at any stop
  • View code as desired
  • Compatible with embedded assembly in C

9
Review For ReadingDebugger Outputs
  • Binary 1010111011010001100001101001
  • Octal
  • 1 010 111 011 010 001 100 001 101 001
  • 1 2 7 3 2 1 4 1 5 1
  • Hexadecimal
  • 1010 1110 1101 0001 1000 0110 1001
  • A E C 1 8 6 9
  • Character codes
  • Floating point

10
Other Debugging Support
  • Assembler listing of the generated machine code
  • as a test.s gttest_s.txt
  • od get an octal dump of a file
  • -x -- output hex format
  • -c -- output ASCII characters (if convertible)
  • od xc test.o
  • Find ways to print intermediate values to a file
  • Store into C variables and print
  • Call C fprintf routine

11
Machine Code
  • Initializing the code (load, initial data values)
  • Code and data in memory in BINARY
  • Debugging the code break, step, view
  • Really large programs gt really large problems

12
Machine CodeOrganizing It In Memory
  • Instructions (.code or .text segment or
    .section)
  • Data (and stack) segments (or .section)
  • Flow control Following the sequences of
    execution?
  • Allowing room for large data what data types,
    how much space?
  • Memory mapping keeping track of it all by
    addresses

13
Machine CodeDoes It Run Right?
  • Routine starts and ends
  • Loop starts and ends
  • Jumps and their destinations
  • Data locations
  • Reserving room for future (run-time) data
  • Arrays
  • Stacks
  • Heaps

14
Machine Code Debugging Code
  • Address switches and front panel lights?
  • Emulators
  • Viewing memory locations (machine
    representations)
  • Single step
  • Run with breakpoints
  • Modern debuggers -- emulators plus
  • Data conversion for display
  • Display names for variables and routines
  • Disassemble the machine code instructions to
    assembly mnemonics

15
Intel 80x86 Assembly
  • Pseudo code for mapping and organization
  • Mnemonics for easy recognition of references to
    operations and registers
  • Name assignments for easy recognition of
    variables and function blocks of code
  • Standardizes file organization for specifying
    programs

16
Mnemonics
  • Control for the assembler
  • Names for operators
  • Names for registers
  • Names for flags
  • Names for data memory addresses
  • Names for code memory addresses
  • Acronyms for combinations of microcode flags

17
Control for the Assembler
  • Code sequence and memory mapping
  • Start of code and data
  • Ordered memory placement
  • End of code and data
  • Other control constructs
  • Embedded mode control
  • Macro definitions and evocations
  • Designations of stack, data, and code segments
  • Comments

18
Mnemonics Operators
  • Moving data among registers and memory locations
    (mov)
  • Arithmetic (add, sub, inc, dec, neg, mul, imul,
    div, idiv)
  • Jumps, conditional jumps (pg 118), calls, and
    returns (jmp, js, jns, jz, jnz, jc, jnc, , call,
    ret)
  • Comparisons, conditionals, and flags (cmp, etc.)
  • Manipulating the stack (push, pop, pushad, popad,
    , call, ret)

19
Mnemonics Operators (cont)
  • Logical (and, or, xor, not, test)
  • Shifts and rotates (shl, shr, sal, sar, ror, rol)
  • Floating point arithmetic
  • I/O and interrupts
  • Extended instructions

20
Mnemonics Registers
  • General data registers

21
Mnemonics Registers (cont)
  • esi source index register
  • edi destination index register
  • esp stack pointer
  • ebp stack base pointer

22
Status and Results Flags
  • EFLAGS register access

23
MnemonicsMemory Locations/Addresses
  • Stack start and reserved locations (.STACK)
  • Code segment (.CODE, label on code lines)
  • Data locations (.DATA, label on data
    declarations)
  • Jump point (label on code line)
  • Loop back point (label on code line)
  • Procedure declaration (PROC and ENDP)

24
Difficulties to Overcome
  • There are multiple assembly language conventions
    for the same 80x86 architecture, even for a
    single assembler.
  • Proprietary control means NO compatibility
    between assembly language or run-time
    environments, even with the same hardware.
  • Open source is free but less controlled.
  • I am working out a usable combination for our
    class, meanwhile try what is available and feel
    free to ask questions!
Write a Comment
User Comments (0)
About PowerShow.com