0% found this document useful (0 votes)
26 views52 pages

5 CPE 413 String Processing and Macros

The document discusses string processing and macros. It covers string representation using length or sentinel characters. It also covers various string instructions like move, compare, scan. String instructions can operate on bytes, words or doublewords. Direction and repetition prefixes can be used with string instructions. Examples demonstrate using string move instructions like movs, lods, stos to copy or initialize strings.

Uploaded by

Samuel jidayi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views52 pages

5 CPE 413 String Processing and Macros

The document discusses string processing and macros. It covers string representation using length or sentinel characters. It also covers various string instructions like move, compare, scan. String instructions can operate on bytes, words or doublewords. Direction and repetition prefixes can be used with string instructions. Examples demonstrate using string move instructions like movs, lods, stos to copy or initialize strings.

Uploaded by

Samuel jidayi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

String Processing and Macros

Prof. Christopher U. Ngene email: [email protected] 1


Outline
• String representation Examples
• Using string length * str_len
• Using a sentinel character * str-cpy
* str_cat
• String instructions * str_cmp
* str_chr
• Repetition prefixes
* str_cnv
• Direction flag
* str_mov
• String move instructions
• String compare instructions
• String scan instructions

Prof. Christopher U. Ngene email: [email protected] 2


String Representation
• Two types
• Fixed-length
• Variable-length
• Fixed length strings
• Each string uses the same length
• Shorter strings are padded (e.g. by blank characters)
• Longer strings are truncated
• Selection of string length is critical
• Too large ==> inefficient
• Too small ==> truncation of larger strings

Prof. Christopher U. Ngene email: [email protected] 3


String Representation…
• Variable-length strings
• Avoids the pitfalls associated with fixed-length strings
• Two ways of representation
• Explicitly storing string length (used in PASCAL)

string DB ‘Error message’


str_len DW $-string
• $ represents the current value of the location counter
• $ points to the byte after the last character of string
• Using a sentinel character (used in C)
• Uses NULL character
• Such NULL-terminated strings are called ASCIIZ strings

Prof. Christopher U. Ngene email: [email protected] 4


String Instructions
• Five string instructions
LODS LOaD String source
STOS STOre String destination
MOVS MOVe String source & destination
CMPS CoMPare String source & destination
SCAS SCAn String destination
• Specifying operands
• 32-bit segments:
DS:ESI = source operand ES:EDI = destination operand
• 16-bit segments:
DS:SI = source operand ES:DI = destination operand

Prof. Christopher U. Ngene email: [email protected] 5


String Instructions..
• Each string instruction
• Can operate on 8-, 16-, or 32-bit operands
• Updates index register(s) automatically
• Byte operands: increment/decrement by 1
• Word operands: increment/decrement by 2
• Doubleword operands: increment/decrement by 4
• Direction flag
• DF = 0: Forward direction (increments index registers)
• DF = 1: Backward direction (decrements index registers)
• Two instructions to manipulate DF
std set direction flag (DF = 1)
cld clear direction flag (DF = 0)

Prof. Christopher U. Ngene email: [email protected] 6


Repetition Prefixes
• String instructions can be repeated rep
by using a repetition prefix
• Two types
while (CX ≠ 0)
• Unconditional repetition execute the string
rep REPeat instruction
• Conditional repetition
CX := CX – 1
repe/repz REPeat while
Equal end while
REPeat while
Zero
repne/repnz REPeat while
• CX register is first checked
Not Equal • If zero, string instruction is not
REPeat while executed at all
Not Zero • More like the JCXZ instruction
Prof. Christopher U. Ngene email: [email protected] 7
Repetition Prefixes…

repe/repz repne/repnz
while (CX ≠ 0) while (CX ≠ 0)
execute the string
instruction execute the string
CX := CX-1
instruction
if (ZF = 0) CX := CX-1
then if (ZF = 1)
exit loop then
end if exit loop
end while
end if
• Useful with cmps and scas string
instructions end while
Prof. Christopher U. Ngene email: [email protected] 8
String Move Instructions
• Three basic instructions
• movs, lods, and stos
• Move a string (movs)
• Format
movs dest_string, source_string
movsb ; operands are bytes
Movsw ; operands are words
Movsd ; operands are doublewords
• First form is not used frequently
• Source and destination are assumed to be pointed by
DS:(E)SI and ES:(E)DI, respectively

Prof. Christopher U. Ngene email: [email protected] 9


String Move Instructions…
movsb --- move a byte string
ES:DI:= (DS:SI) ; copy a byte
if (DF=0) ; forward direction
then
SI := SI+1
DI := DI+1
else ; backward direction
SI := SI-1
DI := DI-1
end if

Flags affected: none


Prof. Christopher U. Ngene email: [email protected] 10
String Move Instructions: Example

.DATA
String1 DB 'The original string’, 0
strLen EQU $ - string1
string2 DB 80 DUP (?)
.CODE
.STARTUP
mov AX, DS ; set up ES
mov ES, AX ; to the data segment
mov CX, strLen ; strLen includes
NULL
mov SI, OFFSET string1
mov DI, OFFSET string2
cld ;
Prof.forward direction
Christopher U. Ngene email: [email protected] 11
String Move Instructions: LODS and LODSB
LODS LODSB
• Load a String (LODS) lodsb --- load a byte string
• Copies the value from the source AL := (DS:SI) ; copy a byte
string at if (DF=0) ; forward
direction
DS:(E)SI to then
• AL (lodsb) SI := SI+1
• AX (lodsw) else ; backward
• EAX (lodsd) direction
SI := SI-1
• Repetition prefix does not make end if
sense
• It leaves only the last value in AL,
AX, or EAX register Flags affected: none
Prof. Christopher U. Ngene email: [email protected] 12
String Move Instructions: Store a String (STOS)

Store a String (STOS) stosb --- store a byte string


• Performs the complementary ES:DI := AL ; copy a byte
operation if (DF=0) ; forward direction
• Copies the value in
then
• AL (lodsb)
• AX (lodsw) DI := DI+1
• EAX (lodsd) else ; backward direction
to the destination string at DI := DI-1
ES:(E)DI end if
• Repetition prefix can be used if you
want to initialize a block of memory
Flags affected: non
Prof. Christopher U. Ngene email: [email protected] 13
String Move Instructions: Example
Example: Initializes array1 with -1

.DATA
array1 DW 100 DUP (?)
.CODE
.STARTUP
mov AX, DS ; set up ES
mov ES, AX ; to the data segment
mov CX, 100
mov DI, OFFSET array1
mov AX, -1
cld ; forward direction
rep stosw
Prof. Christopher U. Ngene email: [email protected] 14
String Move Instructions…
• In general, repeat prefixes are not useful with lods and
stos
• Used in a loop to do conversions while copying
mov CX, strLen
Mov SI, OFFSET string1
Mov DI, OFFSET string2
cld
; forward direction
loop1:
lodsb
or AL, 20H
stosb
loop loop1
done
Prof. Christopher U. Ngene email: [email protected] 15
String Compare Instruction
• cmpsb --- compare two byte strings
• Compare two bytes at DS:SI and ES:DI and set flags

if (DF=0) ; forward
direction
then
SI := SI+1
DI := DI+1
else ; backward
direction
SI := SI-1
Flags affected:DI := DI-1 As per cmp instruction (DS:SI)-(ES:DI)
end if
Prof. Christopher U. Ngene email: [email protected] 16
String Compare Instruction…
.DATA
String1 DB 'abcdfghi',0
strLen EQU $ - string1
string2 DB 'abcdefgh',0
.CODE
.STARTUP
mov AX, DS ; set up ES
mov ES, AX ; to the data segment
mov CX,strLen
mov SI, OFFSET string1
mov DI, OFFSET string2
cld ;
forward direction
repe cmpsb
dec SI
dec DI ; leaves SI & DI pointing to the last
character that differs
Prof. Christopher U. Ngene email: [email protected] 17
String Compare Instruction…
.DATA
string1 DB 'abcdfghi',0
strLen EQU $ - string1 - 1
String2 DB 'abcdefgh',0
.CODE
.STARTUP
mov AX, DS ; set up ES
mov ES, AX ; to the data
segment
mov CX, strLen
mov SI, OFFSET string1 + strLen - 1
mov DI, OFFSET string2 + strLen - 1
std
; backward direction
repne cmpsb
inc SI ; Leaves SI & DI pointing to the first
character that matches
inc U. Ngene email: [email protected]
Prof. Christopher DI ; in the backward directio 18
String Scan Instruction
• scasb --- Scan a byte string
• Compare AL to the byte at ES:DI and set flags

if (DF=0) ; forward
direction
then
DI := DI+1
else ; backward
direction
DI := DI-1
Flags affected: As per cmp instruction (DS:SI)-(ES:DI)
end if
• scasw uses AX and scasd uses EAX registers instead of AL

Prof. Christopher U. Ngene email: [email protected] 19


String Scan Instruction: Example 1

.DATA
string1 DB 'abcdefgh’, 0
strLen EQU $ - string1
.CODE
.STARTUP
mov AX, DS ; set up ES
mov ES, AX ; to the data
segment
mov CX, strLen
mov DI, OFFSET string1
mov AL, ‘e’ ; character to be
searched
cld ;
forward
Prof. direction
Christopher U. Ngene email: [email protected] 20
String Scan Instruction: Example 2
.DATA
string1 DB ‘ abc’, 0
strLen EQU $ - string1
.CODE
.STARTUP
mov AX, DS ; set up ES
mov ES, AX ; to the data segment
mov CX, strLen
mov DI, OFFSET string1
mov AL, ‘ ’ ; character to be searched
cld ; forward
direction
repne scasb
dec DI
Prof. Christopher U. Ngene email: [email protected]
; leaves DI pointing to
21
Macros: Outline
• What are macros? • Repeat block directives
• Macros with parameters • REPT directive
• WHLE directive
• Macros vs procedures • IRP and IRPC directives
• Parameter passing
• Types of parameters • Conditional assembly
• Invocation mechanism • IF and IFE
• When are macros better? • IFDEF and IFNDEF
• IFB and IFNB
• Labels in macros • IFIDN and IFDIF
• Comments in macros • Nested macros
• Macro operators • Performance: Macros vs
• List control directives procedure
Prof. Christopher U. Ngene email: [email protected] 22
What are Macros?
• Macros provide a means to represent a block of text
(code, data, etc.) by a name (macro name)
• Macros provide a sophisticated text substitution
mechanism
• Three directives
• =
• Example: CLASS_SIZE = 90 (can be redefined later)
• EQU
• » Example: CLASS_SIZE EQU 90
• * MACRO

Prof. Christopher U. Ngene email: [email protected] 23


What Are Macros?...
• Macros can be defined with MACRO and ENDM
• Format
macro_name MACRO [parameter1, parameter2, ...]
macro body
ENDM
• A macro can be invoked using
macro_name [argument1, argument2, …]

Example: Definition
Invocation multAX_by_16 MACRO ...
mov AX, 27
sal AX, 4 multAX_by_16
...
Prof. Christopher U. Ngene email: [email protected] 24
ENDM
Macros with Parameters
• Macros can be defined with parameters
• More flexible
• More useful
• Example
mult_by_16 MACRO operand
sal operand,4
ENDM
• To multiply a byte in DL register
mult_by_16 DL
• To multiply a memory variable count
mult_by_16 count

Prof. Christopher U. Ngene email: [email protected] 25


Macros with Parameters…

Example: To exchange two memory words


Wmxchg MACRO operand1, operand2
xchg AX, operand1
xchg AX, operand2
xchg AX, operand1
ENDM
Example: To exchange two memory bytes
Bmxchg MACRO operand1, operand2
xchg AL, operand1
xchg AL, operand2
xchg AL, operand1
ENDM

Prof. Christopher U. Ngene email: [email protected] 26


Macros vs. Procedures
• Similar to procedures in some Parameter passing
respects • In macros, similar to a
• Both improve programmer procedure call in a HLL
productivity
• mult_by_16 AX
• Aids development of modular
code • In procedures, we have to push
• Can be used when a block of parameters onto the stack
code is repeated in the source push AX
program
call times16
• Some significant differences as • Macros can avoid
well • Parameter passing overhead
• Parameter passing • Proportional to the number of
• Types of parameters parameters passed
• Invocation mechanism • call/ret overhead

Prof. Christopher U. Ngene email: [email protected] 27


Macros vs. Procedures…
• Types of parameters • Invocation mechanism
• Macros allow more flexibility in the • Macro invocation
types of parameters that can be
passed • Done at assembly time by
• Result of it being a text substitution text substitution
mechanism • Procedure invocation
• Example • Done at run time
Shift MACRO op_code, operand, count •
Tradeoffs
op_code operand, count
ENDM Type of overhead Procedure Macro
Memory space Lower Higher
• can be invoked as
Execution time Higher Lower
Shift sal, AX, 3
Assembly time lower higher
• which results in the following
expansion
sal AX, 3
Prof. Christopher U. Ngene email: [email protected] 28
When Are Macros Better?
• Useful to extend the instruction set by defining macro-
instructions

times16 PROC
Invocation
push BP
mov BP,SP
push count
push AX
call times16
mov AX,[BP+4]
pop count Too much overhead
sal AX,4 Use of procedure is impractical
mov [BP+4],AX
pop AX
Prof. Christopher U. Ngene email: [email protected] 29
pop BP
When Are Macros Better?...
• Sometimes procedures cannot be used
• Suppose we want to save and restore BX, CX, DX, SI, DI, andBP
registers
• Cannot use pusha and popa as they include AX as well

save_regs MACRO
restore_regs MACRO
push BP
pop BX
push DI
pop CX
push SI
pop DX
push DX
pop
Prof. Christopher U. Ngene email: [email protected] SI 30
Labels in Macros
• Problem with the following macro definiton

to_upper0 MACRO
ch
cmp
ch, ’a’
jb
done
cmp
ch, ’z’
ja
The macro to_upper0 performs
done
the following:
if ((ch ≥ ‘a’) AND (ch ≤ ‘ z ‘))
sub
then ch := ch – 32 ch, 32
done:
end if ENDM
• If we invoke it more than once, we will have duplicate label done
Prof. Christopher U. Ngene email: [email protected] 31
Labels in Macros…
• Solution: Use LOCAL directive
• Format: LOCAL local_label1 [,local_label2,…]
to_upper MACRO Typically, the labels that
ch the assembler generates
to replace the local labels
LOCAL are of the form ??XXXX
done
cmp where XXXX is
ch, ’a’ between 0 and FFFFH
jb
To avoid conflict,
done do not use labels that
cmp begin with ??
ch, ’z’
ja
done
Prof. Christopher U. Ngene email: [email protected] 32
Comments in Macros
• We don’t want comments in a macro definition to appear every
time it is expanded
• The ;; operator suppresses comments in the expansions
;; Converts a lowercase letter to uppercase.

to_upper MACRO ch
LOCAL done
; case conversion macro
cmp ch, 'a’
;; check if ch >= 'a’
jb done
cmp ch, 'z’
;; and if ch >= 'z’
ja done
sub ch, 32
Prof. Christopher U. ;; then
Ngene [email protected]
email: := ch - 32 33
Comments in Macros..
• Invoking the to_upper macro by
mov AL, 'b’
to_upper AL
mov BL, AL
mov AH, '1’
to_upper AH
mov BH, AH

generates the following macro expansion in the next 2


slides

Prof. Christopher U. Ngene email: [email protected] 34


Comments in Macros: …
An example listing file showing macro expansions
17 0000 B0 62
mov AL, 'b’
18
to_upper AL
1 19
; case conversion macro
1 20 0002 3C 61
cmp AL, 'a’
1 21 0004 72 06
jb ??0000
1 22 0006 3C 7A
cmp AL, 'z’
1 23 U.0008
Prof. Christopher 77 02
Ngene email: [email protected] 35
Comments in Macros…
An example listing file showing macro expansions

28
to_upper AH
1 29
; case conversion macro
1 30 0010 80 FC 61
cmp AH, 'a’
1 31 0013 72 08
jb ??0001
1 32 0015 80 FC 7A
cmp AH, 'z’
1 33 0018 77 03
ja ??0001
1 34 001A 80 EC
Prof. Christopher U. Ngene email: [email protected]
20 36
Macro Operators
• Five operators

;; Suppress comment

& Substitution operator. Resolves ambiguous references to parameter names within a


macro

< > Literal-text string. groups one or more characters and symbols into a single text literal. It
prevents the preprocessor from interpreting members of the list as separate arguments.

! Literal-character. forces the preprocessor to treat a predefined operator as an


ordinary character.

% Expansion operator. Eexpands text macros or converts constant expressions into their
text representations.

• We have already seen ;; operator


• We will discuss the remaining four operators

Prof. Christopher U. Ngene email: [email protected] 37


Macro Operators…
• Substitute operator (&)
• Substitutes a parameter with the actual argument
• Syntax: &name
ShowRegister MACRO regName
.data
tempStr BYTE " &regName=",0
.
.
.code
ShowRegister EDX ; invoke the macro

Macro expansion:
tempStr BYTE " EDX=",0

Prof. Christopher U. Ngene email: [email protected] 38


Macro Operator: Literal-Text (<>)
• Treats the enclosed text as a single string literal rather than
separate arguments
• Syntax: <text>
range_error1 MACRO number,variable,range
err_msg&number DB '&variable: out of
range’, 0
range_msg&number DB 'Correct range is &range’,
0
ENDM
• Invoking with
range_error1 1, <Assignment mark>,<0 to 25>

produces
39
err_msg1 DB
Prof. Christopher U. Ngene email: [email protected] 'Assignment mark: out of
Literal-character operator (!)
• Treats the character literally without its default meaning
• Syntax: ! character
range_error2 MACRO number, variable, range
err_msg&number DB '&variable: out of range -
&range’,0
ENDM
• Invoking with
range_error2 3, mark, <can!'!'t be !> 100>
• Produces

err_msg3 DB 'mark: out of range - can''t be >


100’,0

Prof.Without
Christopherthe ! operator,
U. Ngene two single quotes
email: [email protected] will produce a single quote 40
Expression Evaluate operator (%)
• Expression is evaluated and its value is used to replace
the expression itself
• Syntax: %expression
init_arry MACRO element_size, name, size,
init_value
Name D&element_ size size DUP (init_value)
ENDM
• Assuming NUM_STUDENTS EQU 47
NUM_TESTS
EQU 7
Invoking with
init_array W,marks,
%NUM_STUDENTS*NUM_TESTS, -1
produces
marks
Prof. Christopher U. Ngene email: DW 329 DUP (-1)
[email protected] 41
Boolean Expressions
A Boolean expression can be formed using the following
operators:

• LT - Less than
• GT - Greater than
• EQ - Equal to
• NE - Not equal to
• LE - Less than or equal to
• GE - Greater than or equal to
Only assembly-time constants may be compared using these operators.

Prof. Christopher U. Ngene email: [email protected] 42


Repeat Block Directives
• Three directives to repeat a block of statements. These
directives can be used both inside and outside a macro
definition
• REPT
• WHILE
• IRP/IRPC
• Mostly used to define and initialize variables in a data
segment
• Each directive identifies the beginning of a block
• ENDM indicates the end of a repeat block

Prof. Christopher U. Ngene email: [email protected] 43


REPT directive
• Syntax:
REPT expression
macro-body
ENDM
macro-body is repeated expression times

mult_16 MACRO operand mult_16 MACRO


operand
REPT 4
sal operand,1
sal operand, 1
sal operand,1
ENDM
sal operand,1
ENDM
Prof. Christopher U. Ngene email: [email protected] 44
sal operand,1
WHILE directive
• Syntax:
WHILE expression
macro-body
ENDM
macro-body is executed until expression is false (0)

• Following code produces cubed data table


WHILE int_value LT NUM_ENTRIES
DW int_value*int_value*int_value
int_value = int_value+1
ENDM

Prof. Christopher U. Ngene email: [email protected] 45


IRP and IRPC directives
• IRP - Iteration RePeat
• IRPC - Iteration RePeat with Character substitution
• IRP directive
• Syntax:

IRP parameter, <argument1 [,argument2,…]>


macro-body
ENDM

• Angle brackets are required


• Arguments are gives as a list separated by commas
• During the first iteration argument1 is assigned to parameter, argument2
during the second iteration, ...
Prof. Christopher U. Ngene email: [email protected] 46
IRP and IRPC directives…

IRP example
.DATA
IRP value,
<9,6,11,8,13>
DB
value
ENDM
produces
.DATA
DB 9
DB 6
DB 11
DB 8
Prof. Christopher U. Ngene email: [email protected] 47
IRP and IRPC directives…
• IRPC directive
• Syntax:

IRPC parameter, string


macro-body
ENDM

• macro-body is repeated once for each character in string


• string specifies
• the number of iterations
• the character to be used in each iteration
• During the first iteration first character of string is assigned
toparameter, second character during the second iteration, ...
Prof. Christopher U. Ngene email: [email protected] 48
IRP and IRPC directives…
IRPC example
* To generate a sequence of DB statements in the order a, A, e, E,

defineDB MACRO value
DB value
ENDM

IRPC char, aeiou


defineDB '&char’
defineDB %'&char’ -32
ENDM
* Can also use
IRP char, <a,e,i,o,u>
in place of IRPC statement
Prof. Christopher U. Ngene email: [email protected] 49
Conditional Assembly
• MASM provide conditional directives that allow assembly of a
block of statements if the specified condition is true.
• Several conditional directives are available

IF/IFE Assembles if condition is true (IF) or false (IFE)

IFDEF/IFNDEF Assembles if symbol is defined (IFDEF) or undefined (IFNDEF)

IFB/IFNB Assembles if arguments are blank (IFB) or not blank


(IFNB)

IFIDN/IFDIF Assembles if arguments are same (IFIDN) or different


(IFDIF) - case sensitive

IFIDNI/IFDIFI Assembles if arguments are same (IFIDNI) or different (IFDIFI)


- case insensitive
Prof. Christopher U. Ngene email: [email protected] 50
Conditional Assembly
• The general syntax of conditional assembly directives is
IFxxx expression
True_statements
[ELSE
False_statements]
ENDIF
• Each condition assembly directive ends with an ENDIF and there
can be an optional ELSE clause present.
• Here are some operators that can be used in an expression:
Arithmetic operators: +, – , *, /, mod, unary + and
– Relational operators: EQ, GE, GT, LE, LT,
NE
Logical operators: NOT, AND, OR, XOR
Prof. Christopher U. Ngene email: [email protected] 51
Conditional Assembly Examples
• The objective here is to write a macro that can perfOIm left or
right shift operation.
shift MACRO operand, count
;; positive count => left shift
;; negative count => right shift
IFE count EQ 0
IF count GT 0 ;; left
shift
shl operand, count
ELSE
;; right shift
;; count is negative
shr operand, count
ENDIF
ENDIF
By defining this macro, we ENDM
have effectively defined a new macro-instruction
that can left or right shift efficiently using the processor shift family instructions.
Prof. Christopher U. Ngene email: [email protected] 52

You might also like