Assembly 10

Report
Assembly 10
Outline
•
•
•
•
•
1
movsb, movsw, movsd
rep movsb
DF and movsb
scasb, scasw, scasd
rep, repe, repne
movsb Instruction
• movsb does the following (in a single command):
1) moves byte [esi] to [edi];
2) increments esi
3) increments edi
• movsb assumes that esi and edi have already been assigned
2
msg: db “Hi There!!”,10
len: equ $-msg
; in .data
; in .data
cpy: resb len
; in .bss
mov esi, msg
mov edi, cpy
mov ecx, len
_loop:
movsb;
dec ecx
jnz _loop
; copy msg pointer to source reg
; copy cpy pointer to dest reg
; copy string length to ecx
; copy [esi] to [edi]; increments edi,esi
; loop logic
; loop logic
;print msg; print cpy; graceful exit;
msg: db “Hi There!!”,10
len: equ $-msg
cpy: resb len
mov esi, msg
mov edi, cpy
mov ecx, len
_loop:
movsb
dec ecx
jnz _loop
;print msg; print cpy; graceful exit;
UNIX> ./a.out
Hi There!!
Hi There!!
UNIX>
movsw, movsd
• movsw copies word strings from esi to edi
• movsd copies dword strings from esi to edi
• esi must point to the source string
• edi must point to the destination string
• esi and edi get incremented by 2 for words, 4 for dwords
5
Outline
•
•
•
•
•
6
movsb, movsw, movsd
rep movsb
DF and movsb
scasb, scasw, scasd
rep, repe, repne
rep movsb
• As before, rep prefix does the loop for you
• Assumes esi points to source string
• Assumes edi points to destination string
• Assumes ecx contains string’s length
• Length is number of items, NOT number of bytes
7
rep movsb
rep movsb does the following:
1)
2)
3)
4)
5)
8
copies [esi] to [edi];
increments esi
increments edi
decrements ecx
compares ecx to 0, jumps back if necessary
msg: db “Hi There!!”,10
len: equ $-msg
msg: db “Hi There!!”,10
len: equ $-msg
cpy: resb len
cpy: resb len
mov esi, msg
mov edi, cpy
mov ecx, len
_loop:
movsb
dec ecx
jnz _loop
mov esi, msg
mov edi, cpy
mov ecx, len
rep movsb
;print msg, cpy; graceful exit;
;print msg, cpy; graceful exit;
Outline
•
•
•
•
•
10
movsb, movsw, movsd
rep movsb
DF and movsb
scasb, scasw, scasd
rep, repe, repne
DF and movsb
• Directional Flag (DF) determines direction of copy
• DF clear -> goes “uphill” from low to high memory (default)
• DF set -> goes “downhill” from high to low memory
• edi and esi get decremented, not incremented
• Remember to point edi and esi to END of string
• Use cld to clear flag
• Use std to set flag
11
DF and movsb
• DF clear, traverses “uphill”, low to high memory (default)
low memory
high memory
• DF set, traverses “downhill”, high to low memory
low memory
12
high memory
Outline
•
•
•
•
•
13
movsb, movsw, movsd
rep movsb
DF and movsb
scasb, scasw, scasd
rep, repe, repne
scasb
•
•
•
•
•
“scan string by byte”
Use to search for a byte
al holds byte to search for
edi points to 1st byte of string
ecx holds string length
• If byte found, ZF set and edi points to one byte past item
14
msg: db “mjr#osu.edu”,10; in .data
len: equ $-msg
; in .data
; begin .text
print msg, len
; print message before editing
mov al, ‘#’
; byte to search for goes in al
mov edi, msg
; copy msg pointer to dest reg
mov ecx, len
; copy string length to ecx
_loop:
scasb;
; search for ‘#’ in edi
jz _found
; jump if zero flag set (‘#’ found in msg)
dec ecx
; loop logic
jnz _loop
; loop logic
jmp _exit
; if not found after loop, jump to exit
_found:
dec edi
; edi points one past found item, need to dec
mov byte [edi], [email protected]
; replace ‘#’ with [email protected]
;print msg; graceful exit;
msg: db “mjr#osu.edu”,10
len: equ $-msg
print msg, len
mov al, ‘#’
mov edi, msg
mov ecx, len
_loop:
scasb;
jz _found
dec ecx
jnz _loop
jmp _exit
_found:
dec edi
mov byte [edi], [email protected]
16
;print msg; graceful exit;
UNIX> ./a.out
mjr#osu.edu
[email protected]
UNIX>
if I change
“mjr#osu.edu”
to
“mjr$osu.edu”
in .data
UNIX> ./a.out
mjr$osu.edu
mjr$osu.edu
UNIX>
‘#’ not found/
replaced
scasw, scasd
• scasw : scan string for word
• ax holds word to be searched for
• If word found, ZF is set and edi points 2 bytes past found item
• scasd : scan string for dword
• eax holds dword to be searched for
• If dword found, ZF is set and edi points 4 bytes past found item
17
scasb and DF
• DF clear, traverses “uphill”, low to high memory (default)
low memory
high memory
• DF set, traverses “downhill”, high to low memory
low memory
18
high memory
Outline
•
•
•
•
•
19
movsb, movsw, movsd
rep movsb
DF and movsb
scasb, scasw, scasd
rep, repe, repne
rep and scasb
• scasb is conditional string instruction
• If item found, …
• However, rep is unconditional
• Loops until ecx is zero
• => rep prefix will not work for scasb
• We want loop to break if item found
20
repne and repe (Conditional rep)
• repne prefix: repeat while not equal
• E.g., repne scasb
• As long as al != [edi], repeat (ZF clear)
• When al == [edi], break (ZF set)
repne scasb; -> “Search edi for byte al”
• What if al not found? Then ZF is clear
• Use jnz after repne instruction:
• If ZF clear, jump past “found” logic
21
msg: db “mjr#osu.edu”,10
len: equ $-msg
print msg, len
mov al, ‘#’
mov edi, msg
mov ecx, len
repne scasb
jnz _exit
dec edi
mov byte [edi], [email protected]
;print msg; graceful exit;
22
; in .data
; in .data
; begin .text
; print message before editing
; byte to search for goes in al
; copy msg pointer to dest reg
; copy string length to ecx
; search for ‘#’ in edi
; if not found, ZF clear, jump to exit
; edi points one past found item
; replace ‘#’ with [email protected]
msg: db “mjr#osu.edu”,10
len: equ $-msg
print msg, len
mov al, ‘#’
mov edi, msg
mov ecx, len
repne scasb
jnz _exit
dec edi
mov byte [edi], [email protected]
;print msg; graceful exit;
UNIX> ./a.out
mjr#osu.edu
[email protected]
UNIX>
if I change
“mjr#osu.edu”
to
“mjr$osu.edu”
in .data
UNIX> ./a.out
mjr$osu.edu
mjr$osu.edu
UNIX>
‘#’ not found/
replaced
repne and repe (Conditional rep)
• repe prefix: repeat while equal
• E.g., repe scasb
• As long as al == [edi], repeat (ZF set)
• When al != [edi], break (ZF clear)
repe scasb; -> “Search for byte that does NOT equal al”
• What if a non-al byte is never found? Then ZF is set
• Use jz after repne instruction:
• If ZF set, jump past “non al byte found” logic
24
msg: db “
hello!”,10
len: equ $-msg
print msg, len
mov al, ‘ ’
mov edi, msg
mov ecx, len
repe scasb
jz _exit
dec edi
print edi, len
; graceful exit;
; in .data
; in .data
; begin .text
; print message before editing
; space ‘ ‘ goes in al
; copy msg pointer to dest reg
; copy string length to ecx
; search for first non-space
; search failed, ZF set, jump to exit
; edi points one past found item
; print msg starting at edi
msg: db “
hello!”,10
len: equ $-msg
print msg, len
mov al, ‘ ’
mov edi, msg
mov ecx, len
repe scasb
jz _exit
dec edi
print edi, len
; graceful exit;
UNIX> ./a.out
hello!
hello!
got rid of leading
white space
UNIX>
if I change
“
hello!”
to
“hello!”
in .data
UNIX> ./a.out
hello!
hello!
UNIX>
it still works…
Why?

similar documents