INCLUDE Irvine32.inc
.DATA
source BYTE "I want an apple", 0
revstr BYTE SIZEOF source DUP(0)
revwrd BYTE SIZEOF source DUP(0)
ecxbkp DWORD ?
.CODE
main PROC
; Print the elimited source
mov edx, 0
mov edx, OFFSET source
L5:
cmp al,' '
jmp delet
mov source[esi],al
inc esi
delet:
loop L5
mov edx,esi
call WriteString
call Crlf
; Clear registers ;
mov esi, SIZEOF source
sub esi, 2 ; skip the NULL terminator byte
mov edi, 0
mov ecx, SIZEOF source
mov eax, 0
; reverse the string char by char from source to revstr ;
L1:
; move the character in source at index ESI into register AL ;
mov al, source[esi]
; move the character in AL into revstr at index EDI
mov revstr[edi], al
; Increment/Decrement pointers ;
dec esi
inc edi
loop L1
mov revstr[edi], 0 ; Add NUL character
; Print revstr ;
mov edx, 0
mov edx, OFFSET revstr
call WriteString
call Crlf
; Clear registers ;
mov esi, 0
mov edi, 0
mov ecx, SIZEOF revstr
mov eax, 0
mov ebx, 0
; reverse each word in revstr and store in revwrd ;
L2:
mov al, revstr[esi]
; Check if al is a space ;
cmp al, 32
je space
; Check if al is a NUL character ;
cmp al, 0
je ending
; Increment ESI if we haven't reached a space ;
inc esi
loop L2
space:
; Backup ECX and then set ECX = ESI ;
; Increment through (ECX - EDI) ;
mov ecxbkp, ecx
mov ecx, esi
sub ecx, edi
mov ebx, esi
dec esi ; skip the space character
L3:
; move the character in revstr at index ESI into AL ;
mov al, revstr[esi]
; move the character in AL into revwrd at index EDI ;
mov revwrd[edi], al
; Increment/Decrement pointers ;
dec esi
inc edi
loop L3
mov revwrd[edi], 32 ; Add space
; Restore L2-loop values and loop back into L2 ;
mov ecx, ecxbkp
mov esi, ebx
inc esi ; move 1 index foward
mov edi, ebx
inc edi ; move 1 index forward
loop L2
ending:
; Increment through (ECX - EDI) ;
mov ecx, esi
sub ecx, edi
mov ebx, esi
dec esi ; skip the space character
L4:
; move the character in revstr at index ESI into AL ;
mov al, revstr[esi]
; move the character in AL into revwrd at index EDI ;
mov revwrd[edi], al
; Increment/Decrement pointers ;
dec esi
inc edi
loop L4
mov revwrd[edi], 0 ; Add NUL character
; Print revwrd ;
mov edx, 0
mov edx, OFFSET revwrd
call WriteString
call Crlf
exit
main ENDP
END main
这个是代码,我想让它从I want an apple变成I want an apple。
就是要有一个空格在每个单词之间。
------解决思路----------------------
你这么多行的,说我那个仅仅十来行的东西太高级没看懂?
main PROC
mov edx, offset source
call WriteString
call Crlf
mov esi, offset source ; 指向源字符串
mov edi, offset revstr ; 处理后的放这里
cld
mov ah, 0 ; 存放前一字符,初始为 00
l_ch1:
lodsb ; 读入下一字符
test al, al ; 字符串结束标记字符?
jz l_ ; 是,转走结束处理
cmp al, ' ' ; 是空格字符?
jne l_st ; 否,转走存储到目的区
cmp ah, ' ' ; 是空格,那前一字符是空格?
je l_lp ; 是,转走去进行下一字符处理循环
l_st:
stosb ; 字符存储到目的区
mov ah, al ; 当前字符到前一字符用于下一字符处理
l_lp:
jmp l_ch1 ; 转去处理下一字符
l_:
stosb ;
mov edx, offset revstr
call WriteString
l_exit:
invoke ExitProcess, NULL