Strings, - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Strings,

Description:

A string is a series of bytes or words stored in successive memory locations ... MOVS. move string data. CMPS. compare string data. REP. repeat a string instruction ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 39
Provided by: anniegro
Category:
Tags: movs | strings

less

Transcript and Presenter's Notes

Title: Strings,


1
Chapter 5
  • Strings,
  • Procedures,
  • and Macros

2
Definition
  • A string is a series of bytes or words stored in
    successive memory locations
  • were most familiar with strings of ASCII
    character codes
  • Operations on strings
  • moving in memory
  • comparing strings

3
String Instructions
  • MOVS
  • move string data
  • CMPS
  • compare string data
  • REP
  • repeat a string instruction
  • REPE/REPZ REPNE/REPNZ
  • repeat a string instruction as long as condition
    is true
  • SCAS
  • scan a string for a specified character

4
Moving a String
  • Algorithm
  • Initialize source pointer
  • Initialize destination pointer
  • Initialize counter
  • Repeat
  • Copy byte from source to destination
  • Increment source pointer
  • Increment destination pointer
  • Decrement counter
  • Until counter 0

5
Special Registers
  • SI Register
  • this is the source index pointer
  • DI Register
  • this is the destination index pointer
  • well use these to point to the start of our
    source string and destination string, respectively

6
Assembly Code
  • DATA SEGMENT
  • test_msg DB This is a test string to move
  • DB 100 DUP(?) block of text
  • new_loc DB 14 DUP(0) destination of move
  • DATA ENDS
  • CODE SEGMENT
  • assume CSCODE, DSDATA
  • START
  • MOV AX, DATA initialize
    segment register
  • MOV DS, AX
  • LEA SI, test_msg point SI at source string
  • LEA DI, new_loc point DI at destination
    string
  • MOV CX, 14 initialize counter to source
    string len
  • TEST
  • MOV AX, SI move byte from SI to DI
  • MOV DI, AX
  • INC SI
  • INC DI
  • DEC CX

7
Cleaning up our Code
  • MOVSB
  • this single instruction will perform all the
    operations specified in the loop
  • it copies a byte from location pointed to by SI
    to location pointed to by DI
  • increments SI
  • increments DI
  • doesnt automatically decrement counter though
  • REP
  • precede MOVSB instruction with this
  • will decrement CX and repeat instruction until CX
    0
  • Replace our loop with
  • REP MOVSB

8
Not Finished
  • Setting the Direction
  • the direction determines whether we are
    incrementing SI DI or decrementing SI DI
  • if not set (0), we increment
  • CLD clear the direction flag
  • if set (1), we decrement
  • STD sets the direction flag
  • Need to initialize the ES(Extra Segment) register
  • string instructions use the ES as the base for DI
    offsets
  • MOV AX, DATA initialize ES with the same value
    as DS
  • MOV ES, AX

9
Checklist for using MOVSB instruction
  • Initialize the ES register
  • MOV ES, AX
  • SI initialized to start of source string
  • LEA SI, test_msg
  • DI initialized to start of destination string
  • LEA DI, new_loc
  • Direction cleared (or setdepends on impl)
  • CLD
  • CX initialized with the number of elements in the
    source string
  • MOV CX, 14

10
Comparing Strings
  • Initial steps are the same as when using
    MOVSBsee checklist
  • Use CMPSB
  • compares the byte pointed at by SI to byte
    pointed at by DI, and sets the appropriate flags
    in ST
  • takes care of incrementing SI DI
  • Can precede with REP to continue until CX 0,
    or...
  • REPE to continue as long as bytes are equal and
    CX ! 0

11
Procedures
  • Why?
  • To avoid writing the same sequence of
    instructions every time you need them
  • Code readability and clarity
  • Top-down design
  • How?
  • CALL instruction
  • RET instruction

12
The CALL Instruction
  • CALL print_char
  • takes the address of the procedure in memory as
    an operand
  • stores the current value contained in IP on the
    stack
  • this is the return address, because it is the
    address of the instruction to be executed after
    completing the procedure
  • loads the instruction pointer with the starting
    address of the procedure

13
Calling Mechanisms
  • within-segment NEAR call
  • the procedure being called is in the same segment
  • intersegment FAR call
  • the procedure being called is in another segment
  • both NEAR and FAR calls can be direct or indirect
  • direct - adds the 16-bit signed displacement to
    the IP
  • indirect - the instruction pointer is replaced
    with the 16-bit value from a specified register
    or memory location

14
The RET Instruction
  • copies the word at the top of the stack,
    hopefully the value of the instruction pointer,
    from the stack back to the IP
  • this returns execution to the caller of the
    procedure
  • for a return from a far procedure, it also needs
    to pop the value of the CS register
  • it does this by incrementing the SP by 2 and copy
    the word starting there into CS
  • the assembler automatically codes a near RET for
    a near PROC and a far RET for a far PROC

15
The Stack
  • the stack is a segment of memory used for
  • storing return addresses
  • saving the contents of registers for the calling
    program
  • hold data or addresses that will be used by the
    procedure
  • recall the SS and the SP registers
  • SS holds the value of the upper 16 bits of the
    starting address of the stack segment
  • SP holds the value of the offset of the last word
    written to the stack

16
Using the Stack
  • To write a word to the stack - PUSH
  • the SP register is decremented by 2
    automatically, before writing the word to the
    stack
  • soyou need to initialize the SP register the top
    of the memory you have set aside as the stack
    segment, rather than the bottom
  • Retrieve a word from the stack - POP
  • the word starting at SP is copied to the
    specified destination
  • the SP register is incremented by 2 automatically

17
An Example
  • Assume SS 7000H and SP 0050H
  • 001D CALL foo
  • 0020
  • SP gets decremented by 2
  • the word 0020H is written to the memory locations
    starting at (SS SP) 7004EH
  • RET is executed
  • word starting at SP is popped into the IP
    register automatically
  • SP incremented, so SP 0050H, which is the top
    of the stack

18
Instructions to set up a stack
  • STACK_SEG SEGMENT STACK
  • DW 40 DUP(0)
  • STACK_TOP LABEL WORD
  • STACK_SEG ENDS
  • CODE SEGMENT
  • Assume CSCODE, SSSTACK_SEG
  • MOV AX, STACK_SEG Initialize stack
  • MOV SS, AX segment
    register
  • LEA SP, STACK_TOP Initialize
    stack pointer
  • CODE ENDS
  • END

19
Checklist
  • Declare the stack segment
  • STACK_SEG SEGMENT STACK
  • STACK_SEG ENDS
  • Declare the size of the stack
  • DW 40 DUP(0) this stack is 40 words big
  • Attach a name to the highest location in the
    stack
  • STACK_TOP LABEL WORD
  • this declares STACK_TOP as a label to the next
    even address after the words set aside for the
    stack
  • Inform the assembler that you are using a stack
  • ASSUME SSSTACK_SEG
  • Initialize the SS register
  • MOV AX, STACK_SEG
  • MOV SS, AX
  • Initialize the SP register
  • LEA SP, STACK_TOP

20
Writing a Procedure
  • WAIT_1MS PROC NEAR
  • MOV CX, 23F2H
  • HERE LOOP HERE
  • RET
  • WAIT_1MS ENDP
  • WAIT_1MS is the name of the procedure specified
    by PROC
  • its a near procedure specified by NEAR
  • ENDP specifies the end of the procedure

21
PUSH POP
  • PUSH register/memory
  • decrements the SP by 2
  • copies the contents of the 16-bit register or
    memory location to memory at the new SP location
  • you can PUSH
  • any 16-bit general purpose register
  • any of the base or pointer registers
  • any of the segment registers
  • any word from memory
  • POP register/memory
  • copies a word from the TOP to the specified
    16-bit register or memory location
  • increments SP by 2
  • you can POP
  • to any register, except CS
  • to any memory location

22
A Stack Map
  • Shows diagrammatically the effects of a set of
    instructions on the stack and SP
  • MULTO PROC NEAR
  • PUSHF
  • PUSH AX
  • PUSH BX
  • PUSH CX
  • POP CX
  • POP BX
  • POP AX
  • POPF
  • RET
  • MULTO ENDP

23
Preserving Registers
  • Whose responsibility is it anyway?
  • The callee (procedure being called)
  • preferred
  • it knows exactly which registers it needs to use
  • less clutter in main program
  • independent of which registers are used by
    caller
  • The caller (calling program or procedure)

24
Passing Parameters
  • Passing in Registers
  • move the parameter to a register, and do not push
    that register onto the stack
  • Passing in General Memory
  • directly access the parameters by name
  • MOV AL, SOME_VAR
  • Passing using Pointers
  • copy the address of the parameter to a register,
    usually SI or DI before calling the procedure
  • LEA SI, SOME_VAR
  • Passing using the Stack
  • push the parameters on the stack before calling
    the procedure
  • use the BP register as a second pointer into the
    stack

25
Problems with Each Method
  • Passing in registers
  • the number of registers is limited
  • Passing in general memory
  • always using the same memory location
  • nonreentrant
  • Passing using pointers
  • most versatile, because you can pass pointers to
    anywhere in memory
  • Passing using the stack
  • can be difficult to maintain the stackstack
    overflow
  • can use RET ltsome constantgt increment SP

26
Choosing a Method
  • For simple procedures with few parameters...
  • use registers
  • When dealing with arrays
  • use a register to store starting address
  • Globals are bad even in assembly
  • this is the case where you are directly accessing
    a memory location
  • For procedures that will be called from a high
    level language program, or recursive procs
  • use the stack

27
Reentrant Procedures
  • A reentrant procedure can be interrupted, used,
    and then re-entered without losing or overwriting
    anything useful
  • To be reentrant
  • a procedure must push the flags and registers
    onto the stack before doing anything else
  • a procedure should use only the registers or the
    stack to pass parameters

28
Why is reentrancy important?
  • Normal program execution can be interrupted with
    instructions to call a specified procedure
  • this is an interrupt service procedure
  • The interrupt service procedure could call the
    procedure that was executing when the interrupt
    occurred
  • if were using the stack correctly, there are no
    problems
  • if were directly using memory locations, were
    in trouble!

29
Recursive Procedures
  • A recursive procedure is a procedure that calls
    itself
  • useful for specific apps, such as ones that deal
    with data in a tree format
  • Factorial
  • if n 1 then factorial 1
  • else factorial n (factorial of (n - 1))

30
Writing Calling Far Procedures
  • Writing
  • PROCEDURES SEGMENT
  • MULTIPLY_32 PROC FAR
  • ASSUME CSPROCEDURES
  • NOP
  • RET
  • MULTIPLY_32 ENDP
  • PROCEDURES ENDS
  • Calling
  • the assembler takes care of translating a far
    procedure call

31
Using Multiple Assembly Units
  • You can create a program that is made up of
    multiple assembly units
  • can write each module and assemble, test, and
    debug as a unit
  • then you link all the obj files from the modules
    into an executable file
  • You need to use some new keywords
  • PUBLIC
  • EXTRN

32
Public Extrn
  • SMART_DIVIDE.ASM
  • PUBLIC SMART_DIVIDE
  • PROCEDURES SEGMENT PUBLIC
  • SMART_DIVIDE PROC FAR
  • ASSUME CSPROCEDURES, DSDATA
  • SMART_DIVIDE ENDP
  • PROCEDURES ENDS
  • END
  • MAIN_PROG.ASM
  • PROCEDURES SEGMENT PUBLIC lets the assembler
    know that
  • EXTRN SMART_DIVIDE FAR smart_divide is of
    type far proc
  • PROCEDURES ENDS and is located in the segment
  • procedures

33
More on PUBLIC
  • Make a segment PUBLIC whenever you want it to be
    linked with other segments of the same name in
    other modules
  • this has the effect of concatenating the segments
    in successive memory locations
  • Make a variable PUBLIC whenever you want it to be
    accessible from other assembly modules
  • PUBLIC DIVISOR

34
More on EXTRN
  • Use EXTRN to tell the assembler that the data
    item is not in the present module
  • EXTRN SMART_DIVIDEFAR
  • EXTRN DIVISORWORD
  • EXTRN CORRECTION_FACTORABS
  • I can connect these using a comma
  • EXTRN SMART_DIVIDEFAR, DIVISORWORD
  • Enclose the EXTRN statement with
  • SEGMENT_NAME SEGMENT PUBLIC
  • SEGMENT_NAME ENDS
  • this tells the assembler and linker where the
    data item is located

35
Macros
  • Whenever we have a group of instructions that
    will be used multiple times, we can
  • use a procedure
  • use a macro
  • A macro is a group of instructions that are
    bracketed and given a name at the start of a
    program
  • use MACRO and ENDM to specify a macro
  • accomplished by define in C/C
  • To call a macro, just specify the name of the
    macro, like you would an instruction mnemonic

36
Macro Example
  • PUSH_ALL MACRO this macro push all the flags
  • PUSHF onto the stack
  • PUSH AX it can be useful when writing
  • PUSH BX functions
  • PUSH CX
  • PUSH DX we also might want to write
  • PUSH BP a macro called POP_ALL
  • PUSH SI
  • PUSH DI
  • PUSH DS
  • PUSH ES
  • PUSH SS
  • ENDM

37
Macro Parameters
  • Parameters are specified in the macro definition
  • MOVE_ASCII MACRO NUMBER, SOURCE, DESTINATION
  • ENDM
  • Parameters are passed as part of the calling
    statement
  • MOVE_ASCII 03DH, BLOCK_START, BLOCK_DEST

38
Procedures vs. Macros
  • Procedures
  • Use CALL and RET mechanism
  • Requires the use of a stackOverhead
  • Machine code for instructions are only put in
    memory(CS) once
  • Parameters are passed in registers, memory
    locations, or on the stack
  • Macros
  • Called by specifying the macro name
  • The assembler inserts the lines of code
  • denoted by a in the listing
  • Appropriate for shorter segments of code
  • Parameters are passed as part of the calling
    statement
Write a Comment
User Comments (0)
About PowerShow.com