移動字符串
MOVS 指令
子指令
MOVSB 移動一個字節
MOVSW 移動兩個字節
MOVSL 移動4個字節
說明
設置EDI的地址
movl $output, %edi
leal output, %edi
示例
# movstest.s - An example of the MOVS instructions
.section .data
value1:
.ascii "This is a test string.\n"
.section .bss
.lcomm output, 23
.section .text
.globl _start
_start:
nop
leal value1, %esi
leal output, %edi
movsb
movsw
movsl
movl $1, %eax
movl $0, %ebx
int $0x80
* 設置方向標識
* CLD 清除DF的標識, esi和edi自動增加
* STD 設置DF標識,esi和edi自動減少
# reptest.s - An example of the REP instruction
.section .data
value1:
.ascii "This is a test string.\n"
.section .bss
.lcomm output, 23
.section .text
.globl _start
_start:
nop
leal value1, %esi
leal output, %edi
movl $23, %ecx
cld
rep movsb
movl $1, %eax
movl $0, %ebc
int $0x80
存取字符串
LODS 指令
子指令
LODSB 取ESI一字節到AL寄存器
LODSW 取ESI兩字節到AX寄存器
LOADSL 取ESI四字節到EAX寄存器
STOS 指令
子指令D
STOSB 從AL寄存器取一字節存入EDI
STOSW 從AX寄存器取兩字節存入EDI
STOSL 從EAX寄存器取四個字節存入EDI
示例
# stostest.s - An example of using the STOS instruction
.section .data
space:
.ascii " "
.section .text
.globl _start
_start:
nop
leal space, %esi
leal buffer, %edi
movl $256, %ecx
cld
load sb
rep stosb
movl $1, %eax
movl $0, %ebx
int $0x80
#convert.s - Converting lower to uppper case
.section .data
string1:
.asciz "This is a TEST, of the conversion program!\n"
length:
.int 43
.section .text
.globl _start
_start:
nop
leal string1, %esi
mov %esi, %edi
movl length, %ecx
cld
loop1:
loadsb
cmpb $'a', %al
jl skip
cmpb $'z', %al
jg skip
subb $0x20, %al
skip:
stosb
loop loop1
end:
pushl $string1
call printf
addl $4, %esp
pushl $0
call exit
比較字符串
CMPS指令
子指令
CMPSB 比較一個字節的值
CMPSW 比較兩個字節的值
CMPSL 比較四個字節的值
說明
默認ESI存源字符,EDI存目的字符串
DF決定字符串的方向
示例
# cmpstest.s - A simple example of the CMPS instruction
.section .data
value1:
.ascii "Test"
value2:
.ascii "test"
.section .text
.globl _start
_start:
nop
movl $1, %eax
leal value1, %esi
leal value2, %edi
cld
cmpsl
je equal
movl $1, %ebx
int $0x80
equal:
movl $0, %ebx
int $0x80
# cmpstest.s - An example of using the REPE CMPS instruction
.section .data
value1:
.ascii "This is a test of the CMPS instructions"
value2:
.ascii "This is a test of the CMPS Instructions"
.section .text
.globl _start
_start:
nop
movl $1, %eax
leal vlaue1, %esi
leal value2, %edi
movl $39, %ecx
cld
repe cmpsb
je equal
movl %ecx, %ebx
int $0x80
equal:
movl $0, %ebx
int $0x80
# strcomp.s - An example of comparing strings
.section .data
string1:
.ascii "test"
length1:
.int 4
string2:
.ascii "test1
length2:
.int 5
.section .text
.globl _start
_start:
nop
lea string1, %esi
lea string2, %edi
movl length1, %ecx
movl length2, %eax
cmpl %eax, %ecx
ja longer
xchg %ecx, %eax
longer:
cld
repe cmpsb
je equal
jg greater
less:
movl $1, %eax
movl $255, %ebx
int $0x80
greater:
movl $1, %eax
movl $1, %ebx
int $0x80
equal:
movl length1, %ecx
movl length2, %eax
cmpl %ecx, %eax
jg greater
jl less
movl $1, %eax
movl $0, %ebx
int $0x80
掃描字符串
SCAS 指令
子指令
SCASB 和AL寄存器比較一字節
SCASW 和AX寄存器比較兩字節
SCASL 和EAX寄存器比較四字節
說明
示例
# scastest.s - An example of the SCAS instruction
.section .data
string1:
.ascii "This is a test - a long text string to scan."
length:
.int 44
string2:
.ascii "-"
.section .text
.globl _start
_start:
nop
leal string1, %edi
leal string2, %esi
movl length, %ecx
loadsb
cld
repne scasb
jne notfound
subw length, %cx
neg %cx
movl $1, %eax
movl %ecx, %ebx
int $0x80
notfound:
movl $1, %eax
movl $0, %ebx
int $0x80
# strsize.s - Finding the size of a string using the SCAS instruction
.section .data
string1:
.asciz "Testing, one, two, three, testing.\n"
.section .text
.globl _start
_start:
nop
leal string1, %edi
movl $0xffff, %ecx
movb $0, %al
cld
repne scasb
jne notfound
subw $0xffff, %cx
neg %cx
dec %cx
movl $1, %eax
movl %ecx, %ebx
int $0x80
notfound:
movl $1, %eax
movl $0, %ebx
int $0x80