彙編中的字符串

移動字符串

  • MOVS 指令
    • 子指令
      • MOVSB 移動一個字節
      • MOVSW 移動兩個字節
      • MOVSL 移動4個字節
    • 說明
      • 默認源操作數在ESI,目的操作數在EDI
    • 設置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自動減少
  • REP 前綴
    • 示例
# 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
  • 使用REP
    • 示例
# 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寄存器比較四字節
    • 說明
      • 目的字符地址默認存儲在EDI寄存器
    • 示例
# 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章