Week 6 Using the Assembler by 46CZmY

VIEWS: 8 PAGES: 52

									                   Week 6
             Using the Assembler

     Assembly Language for
     Intel-Based Computers,
     3rd edition
     Kip R. Irvine



11/16/2011
Introduction
 4.1 More about the Assembler and Linker
 4.3 JMP and Loop Instructions
 4.4 Indirect Addressing
 4.2 Operators and Expressions
 4.5 Debugging Workshop
 4.7 Using a Link Library
4.1 More about the Assembler and Linker
Source Listing File:
 See sample.asm
 Columns in sample.lst:
  – Offset in current segment
  – Machine language
  – Source code
4.1 More about the Assembler and Linker
Source Listing File:
 Special locations
   – A0 0000 R mov AX, value1            (Line 11)
     R Relocatable: The address is relative to the
        beginning of the data segment
   – A0 0000 E mov AX, value1 (assuming
     value1 is external variable)
     E External: The reference is to a variable,
        constant, or procedure that is outside the
        current module
4.1 More about the Assembler and Linker
Map File: sample.map
 It shows the sizes of the different segments
 The code (including main and library routines) is
  14h long
 The data begins at 20h and contains 2h bytes
 The stack begins at 30h and contains 100h bytes
 The map file lists all procedures included in the
  program
 Actually the first 100h bytes are reserved for the
  Program Segment Prefix (PSP)
4.1 More about the Assembler and Linker
Map File: sample.map
 Suppose the program loads at 20000h
 PSP   starts at 20000h
 Code starts at 20100h, ends at 20113h
 Data starts at 20200h, ends at 20201h
  (Data segments start at a word boundary)
 Stack starts at 20300h, ends at 20312h
  (Stack segments start at a "paragraph"
  boundary)
4.1 More about the Assembler and Linker
Map File: sample.map
 Suppose the program loads at 20000h
 Initially CS is 2010h, DS and ES are
  2000h, and SS is 2030h.
 Programs must include code to reset DS
  (and ES, if needed) to the beginning of the
  data segment:
    mov AX, @data
    mov DS, AX ; "Always" required
    mov ES, AX ; If needed
4.1 More about the Assembler and Linker
Assembly problems
 Problem: MASM may require several
  environmental variables
  Solution: Add to them to autoexec.bat
 Problem: It may require several commands to
  run MASM .
  Solution: Use a .bat file to run ML
4.1 More about the Assembler and Linker
MS-DOS Batch Files
 mlbat.bat
  rem @echo off
  rem ***bat file to setup and run ML***
  rem *** Set path for MASM ***
  set path=C:\MASM611\BIN;%path%
  rem *** run ml, the Masm assembler and linker
  ***
  c:\masm611\bin\ml %1 %2 %3 %4 %5 %6 %7
  %8
4.1 More about the Assembler and Linker
MS-DOS Batch Files
 Example: mlbat -Zi test.asm
 The path tells DOS where to look for programs.
 The path is saved, modified so DOS can find ml,
  and restored after ml is run
 Presetting savepath may be required
 %1, %2, … are the parameters from the original
  command
  %1 = -Zi %2 = test.asm
 The text (page 90) shows how to automatically
  run CodeView if the source file is valid
4.1 More about the Assembler and Linker
Memory Models
MODEL DESCRIPTION
Tiny    Code and data together <64K. .com
        files. Default of the Debug program
Small   Code and data each <= 64K
Medium Data <= 64K
Compact Code <= 64K
Large   Arrays <= 64K
Huge    Arrays > 64K allowed
Flat    No segments, 32 - bit addresses,
        Protected mode only
4.1 More about the Assembler and Linker
Target Processor Directives
 Include .8086, .186, .286, .386, 486, or
  .586 as desired
 .8086 is the default
 Instructions for newer processors cannot be
  used
 At least .386 is needed to use 32 bit
  registers
 Place these directives after the model
  directive (except in flat memory model)
4.2 operators and Expression
Arithmetic Operators and Expressions
 Each operator has a precedence level
 Operator precedence table is shown in P. 96
 Associativity:
  -operators shown in Table 4 (p.
  96)associate from left to right)
  -Example: count*3/5
4.2 operators and Expression
OFFSET directive
 OFFSET - the offset from the beginning of the
  segment
 Example code:
 Y     dw     4
           assume Y stored at location 124
  …
 mov bx, offset Y
                              bx = ____
 bx is a ______________ to Y
4.2 operators and Expression
SEG directive
 SEG – returns the segment part of a label or
  variable’s address.
 It is used when:
  - there are multiple data segments (e.g., large
  model)
  - use far operator
 Example code: assuming array in other seg.
 mov ax, seg array
 mov ds, ax
4.2 operators and Expression
PTR directive
 PTR – override the default size of an operand
 Example code:
 Y     db    FFh
 X     dw    20, 13
  …
 Inc    byte ptr Y
 mov   ax, Y                ax =
 Inc    word ptr Y
 mov   ax, Y                 ax =
4.2 Operators and expressions
The LABEL directive
 It gives a name and a size to an existing storage
  location. It does not allocate storage.
 It must be used in conjunction with byte, word,
  dword, qword...
.data                               Data segment
  bData label byte                0 0A           bData
  wData dw 0Ah                    1 00           wData
  .code
    …
    mov AL, wData ; illegal
    mov AL, bData ; works fine
    mov AX, wData ; works fine
 bData is just an alias for the first byte of the storage
  location wData
4.2 Operators and expressions
The Type Operator
 It returns the size, in bytes, of a variable:
  .data
  var2 DW 1, 2, 3
  var4 DD 4
  .code
    mov BX, TYPE var2            ;BX = 2
    mov BX, TYPE var4            ;BX= 4
  Handy for array processing. Ex: If SI points to
  an element of var2, then to make SI point to the
  next element, we can simply write:
          add SI, TYPE var2
4.3 JMP and LOOP instructions
Transfer of Control
 Unconditional branch: The new location
  is always loaded into the IP. Example:
  JMP        (jump)
 Conditional branch: The new location is
  loaded into the IP only if some condition is
  satisfied. Example:
  JZ         (jump if zero)
4.3 JMP and LOOP instructions
JMP instruction
 Instruction JMP
 Formats:
   – jmp SHORT destination
     1 byte offset -128 to +127 from updated IP
   – jmp NEAR PTR destination
     1 word offset to anywhere in current segment
   – jmp FAR PTR destination
     jump to another segment. Segment goes into
     CS, offset into IP
 Destination location should be identified by a
  label
4.3 JMP and LOOP instructions
JMP instruction
  SHORT and NEAR PTR are relative jumps,
   FAR PTR jumps give absolute address.
  For efficiency, the IP is incremented before the
   offset for the jump is processed. Offsets are from
   the instruction following the jump.
  Most jumps are very short. jmp SHORT is most
   common
  MASM automatically picks SHORT or NEAR
   PTR so we generally just write
      JMP       ALabel
4.3 JMP and LOOP instructions
JMP NEAR PTR                           Current
                                     instruction
IP
 001B B8 0064            mov    AX, 100
     001E   EB 06         jmp    L01
     0020   83 C0 0A L00: add    AX, 10
     0023   83 C0 05       add   AX, 5
     0026   8B D8    L01: mov     BX, AX
               IP       0020
               Offset     06
               New IP   0026
  4.3 JMP and LOOP instructions

  JMP instruction: Example
 0005  E9 0100           jmp   L1
 0008 100 [00]           db     100h DUP (0)
 0108 EB 04          L1: jmp    L2
 010A 01 02 03 04        db     1,2,3,4
 010E B4 01          L2: mov    AH, 1
 0110 CD 21               int   21h
 0112 A2 0002 R           mov   Char, AL
 0115 EB F7               jmp   L2
 Jump calculations
 Old IP     0008     010A      0117
 Offset   +0100     + 04      +FFF7
 New IP     0108     010E      010E
4.3 JMP and LOOP instructions
LOOP, LOOPW, LOOPD
 The loop instructions are the easiest way to set up
  a loop
 They use CX or ECX as the counter
 Action: decrement CX or ECX. Jump if the new
  register value is not 0
 Offset is one byte long (-128 to +127)
 LOOP uses CX if in 16 bit mode, ECX if in 32 bit
  mode
 LOOPW uses CX, LOOPD uses ECX
4.3 JMP and LOOP instructions
LOOP Example            Question:Why do we
                        calculate the sum in
 Add 5 + 4 + 3 + 2 + 1
                        AX instead of in Sum?
 C or C++
  sum = 0;
  for (int i = 5; i > 0; i--)
      sum = sum + i;
 Assembler
           mov AX, 0 ; set sum = 0
           mov CX, 5 ; set CX = 5
  Begin: add AX, CX ; Add CX to sum
           loop Begin ; repeat
           mov Sum, AX; save the Sum
4.3 JMP and LOOP instructions
LOOP: Usage
 Remember that 32 bit mode and LOOPD use ECX
 IF CX (or ECX) is zero initially, there are many
  repeats!
 Initially set CX to the number of repetitions
  desired
 MASM does not automatically take care of the
  situation if the loop is over 128 bytes long.
4.4 Indirect Addressing
Review: Addressing Modes
 From chapter 3:
 Immediate:     Specify value in instruction
 Register:      Specify register with value
 Direct:        Specify memory variable
 Direct-offset: Specify memory variable +/-
                 displacement. (Actually this
                 is same as Direct in
                 machine language)
4.4 Indirect Addressing
Indirect Addressing Modes
 Problem: We want to add 50 word values
            in a list called List
  Solution: ???
        mov AX, 0
        add AX, List
        add AX, List+2
        ...
        add AX, List+98
 What if there were 5000 words?
 Solution: Put the offset of List in a register. Add
  the value whose offset is in the register. Point
  register to next value and repeat as needed.
4.4 Indirect Addressing
Indirect Addressing Mode
 Indirect addressing mode
   – Notation: [reg]
     where reg is BX, BP, SI,        23
     or DI in 8088.                  25 600
     In 32 bit mode, AX, CX,         27
     or DX can also be used BX= 25   29
   – Example: mov AX, AX = 600       31
     [BX]                            33
      • Move the value whose         35
        address is in BX to AX
      • Move the value pointed
        by BX to AX
4.4 Indirect Addressing
Indirect Addressing Modes: Example
 .data
  List dw 1, 3, 10, 6, 2, 9, 2, 8, 9
  Number = ($ - List)/2
  .code
      …
  ; sum values in list
      mov AX, 0 ; sum = 0
      mov CX, Number ; number of values
      mov SI, OFFSET List ; ptr to List
  L3: add AX, [SI] ; add value
      add SI, 2 ; point to next value
      loop L3     ; repeat as needed
4.4 Indirect Addressing
Indirect Addressing Modes
 The absolute memory address is formed by adding
  the offset to 10h times a segment register
     Offset in     Segment register          Type
       BX                 DS                 Base
       BP                 SS                 Base
       SI                 DS                 Index
       DI                 DS                 Index
 Problem: Print the characters in String one at a
  time. Solution: Next slide
4.4 Indirect Addressing
Indirect Addressing Modes
 String      db "the words"
 Long = $ - String
 …
 ; Print string 1 character at a time (function 2, int
 21h)
       mov CX, Long             ; CX <- number char
       mov SI, OFFSET String; point to String
 L9: mov DL, [SI]               ; get char.
       mov AH, 2                ; print char
       int 21h
       inc SI                   ; point to next char
       loop L9                  ; repeat
4.4 Indirect Addressing
Indirect Addressing Modes
 Overriding the default segment:
  mov al, cs:[si] ; offset from CS
  mov dl, ds:[bp] ; offset from DS
4.4 Indirect Addressing
Based and Indexed Addressing
 A displacement (constant) is added to the base or
  indexed value to get the offset
 Notation: Register added to offset
   – variable[reg]     Good notation if reg holds
   – [reg+variable]    the "subscript". Remember
   – [variable+reg]    variables are just constants
 Notation: Register added to constant
   – constant[reg]    Good notations if the register
   – [reg+constant] holds the offset of the
   – [constant+reg] variable
4.4 Indirect Addressing
Based and Indexed Addressing
        mov   AX, List[SI]

                                  Memory
                          List
                          List+2
    SI    4               List+4  100
    AX 100                List+6
                          List+8
                          List+10
                          List+12
4.4 Indirect Addressing
Based and Indexed Addressing
 mov     SI, OFFSET List           Memory
  mov     AX, 4[SI]   List
        OFFSET List
                          List+2
 SI                       List+4    100
 AX 100                   List+6
                          List+8
                          List+10
                          List+12
4.4 Indirect Addressing
Indexed Addressing Modes:
 .data
 List dw 1, 3, 10, 6, 2, 9, 2, 8,
 9
 Number = ($ - List)/2
 .code
      ...
  mov AX, 0 ; sum = 0
  mov CX, Number ; number of values
  mov SI, 0 ; "Subscript“
L8: add AX, List[SI] ; add value
    add SI, 2; point to next value
    loop L8    ; repeat as needed
4.4 Indirect Addressing
Based - Indexed Addressing
 Add a base register and index register to get
  offset (32-bit mode allows using any two
  registers)
 Great for 2-D arrays
 Notation
      [reg + reg]
4.4 Indirect Addressing
Based - Indexed Addressing
 mov    BX, OFFSET List
  mov    SI, 4
                                    Memory
  mov    AX, [BX+SI]
                          List
                          List+2
   BX OFFSET List                   100
          +               List+4
   SI 4                   List+6
                          List+8
   AX 100                 List+10
                          List+12
4.4 Indirect Addressing
Based - Indexed with Displacement
 Like based-indexed but with a displacement
 Effective offset is reg + reg + displacement
 Notation:
  – constant[reg][reg]       Recall: variables are
  – [reg + reg + constant] constants to MASM
  – constant[reg + reg]
 Example:
  mov        Array[BX][SI], 10
4.4 Indirect Addressing
Based - Indexed with Displacement
 Array dw 11, 12, 13, 14, 15
       dw 21, 22, 23, 24, 25
       dw 31, 32, 33, 34, 35
 NumCol = 5
        …
           mov BX, NumCol
           mov SI, 3
           mov AX, Array[BX+SI]
       ; mov AX, Array[BX][SI]

AX    35
4.5 Debugging Workshop
Common problems
 Missing or misplaced
  – .CODE, .DATA, .STACK directives
  – PROC and ENDP
  – END or the correct label following END
 Mismatched operand sizes
  – Wrong register
  – Missing PTR
 Using arithmetic operators when run time
  operations needed
4.5 Debugging Workshop
Common problems
 Using the wrong registers
 Using the wrong addressing mode
 The hardest errors to fix are due to
4.5 Debugging Workshop
Common problems using ML
 Must learn real meaning of error messages
 MASM understands long file names
  Link does NOT understand long file names
 Example:
         ml myExample.asm
 myExample assembles.The linker says it
 can't find myExample.obj even though it is
 in the directory!
4.7 Using a Link Library
Irvine.lib
 Irvine.lib contains many procedures.
   – I/O procedures
   – Random number generation
   – Timing information
   – Write DOS error messages
   – Get command line parameters
   – Disk operations
   – String operations
4.7 Using a Link Library
Using a link library
 Extern statements are required
  extern name:type, name:type, ...
 "name" is the name of a procedure, constant, or
  variable
 "type" is the type of that name
  proc       Default for procedure
  abs        EQU or = constant
  byte       8 bit variable
  word       16 bit variable
  dword 32 bit variable
4.7 Using a Link Library
Using Irvine.lib
 String example:
 You want to write a string variable Msg
 .data
 Msg db "This is a string", 0
 .code
 extern Writestring:proc, Crlf: proc
 ...
     mov DX, OFFSET Msg
     call Writestring
     call Crlf
4.7 Using a Link Library
Using Irvine.lib
 Signed word example:
  You want to read and write a signed word Val
  .data
  Val1       sword      ?
  .code
  extern Readint:proc, Writeint_signed:proc
       call Readint
       mov      Val, AX
       call Crlf
       …
       mov      AX, Val
       call Writeint_signed
       call Crlf
4.7 Using a Link Library
Using Irvine.lib
 Unsigned word example:
  You want to read and write a unsigned word Val50
  .data
  Val50        word     ?
  .code
  extern Readint:proc, Writeint:proc
       call Readint
       mov      Val50, AX
       call Crlf
       …
       mov      AX, Val50
       mov      BX, 16       ; write in base 16
       call Writeint
4.7 Using a Link Library

Using Irvine.lib
 character example:
  You want to read a char Letter and write its
  decimal value
  .data
  Letter byte          ?
  .code
  extern Waitchar:proc,
  Writebcd:proc
     call Waitchar;does not echo
     mov     Letter, AL
       …
     mov     AL, Letter
     call Writebcd;write hex value
     call Crlf
4.7 Using a Link Library
Using Irvine.lib
 The ml command:
  ml -Zi Example.asm irvine.lib
 This assumes that irvine.lib and
  Example.asm are in the current subdirectory
  and ml is on the path. -Zi is included
  assuming that Code View will be used.
 CSCE 380
 Department of Computer Science
  and Computer Engineering
 Pacific Lutheran University
 9/12/2001

								
To top