5 CPE 413 String Processing and Macros
5 CPE 413 String Processing and Macros
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
.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)
.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
.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
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
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
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
< > 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.
% Expansion operator. Eexpands text macros or converts constant expressions into their
text representations.
Macro expansion:
tempStr BYTE " EDX=",0
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
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.
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: