颠倒颠倒的词——组装
Reversing the Reversed Word - Assembly
我在组装作业时遇到了问题。这个程序的目标是反转每个字母,所以 CIS 335/535 is a great course
到我的程序所做的 esruoc taerg a si 535/533 SIC
但它也应该将它转换为 course great a is 335/535 CIS
我的程序没有因此我有最终输出评论。我的代码在最后一个块中喷出类似 cis 335/535 si a great course
的内容。
TITLE MASM Assignment 3 reverse a string word by word (main.asm)
; Description: Reverse a string character by character in-place then reverse word for word using nested loops
;
; Revision date:
INCLUDE Irvine32.inc
.data
source BYTE "CIS 335/535 is a great course",0
ecxbkp DWORD ? ;save ecx if necessary
.code
main PROC
mov edx,OFFSET source
call WriteString
call Crlf ; print\r\n
mov ecx, LENGTHOF source ;initialize loop counter
mov esi, OFFSET source ;esi starting address of string
mov edi, OFFSET source
dec edi
END_STRING:
inc edi
mov al,[edi]
cmp al,0 ;find zero byte
jnz END_STRING ;jump back to END_STRING if al is not 0
dec edi ;edi points to end of string
shr ecx, 1 ;ecx is loop count (shift one = length/2)
L1:
mov bl, [esi] ;load characters
mov al, [edi]
mov [esi], al ;swap characters
mov [edi], bl
inc esi ;update forward pointer by 1
dec edi ;decrement backward pointer by 1
loop L1 ;and loop
; display the string
mov edx,OFFSET source
call WriteString
call Crlf ; print\r\n
;Use nested loops to reverse word for word
mov ecx, LENGTHOF source ;set outer loop count ecx= entire length
mov esi, OFFSET source ;esi points to start of string
mov edi, OFFSET source
dec edi
mov ecxbkp, ecx ;save outer loop count
L2: ; go through beginning to end of string copy space character for length
inc edi
mov al, [edi] ;move edi address into al register
cmp al, ' ' ;find space character
loop L2
mov ecx, 16 ;modify inner loop count (length/2) (for swap)
dec edi
L3: ; reverse/swap word for word
mov bl,[esi]
mov al,[edi] ;load words
mov [esi], al
mov [edi],bl ;swap words
inc esi ;update forward pointer by 1
dec edi ;decrement backward pointer by 1
loop L3 ;and loop
mov ecxbkp, ecx ;restore outer loop count
; display the string
;mov edx,OFFSET source
;call WriteString
;call Crlf ; print\r\n
exit
main ENDP
END main
enter code here
一些让你上路的变化:
将loop L2
改为jne L2
不要在 ECX 中使用固定值 16,而是计算单词的确切长度。
将 EDI 中的位置保存在一个变量中,以便在 L3 循环中反转这个单词后能够继续下一个单词。
L2:
inc edi
mov al, [edi] ;move edi address into al register
cmp al, ' ' ;find space character
jne L2
mov SavedPointer, edi
lea ecx,[edi-1]
sub ecx,esi ;ECX is length of current word
我在组装作业时遇到了问题。这个程序的目标是反转每个字母,所以 CIS 335/535 is a great course
到我的程序所做的 esruoc taerg a si 535/533 SIC
但它也应该将它转换为 course great a is 335/535 CIS
我的程序没有因此我有最终输出评论。我的代码在最后一个块中喷出类似 cis 335/535 si a great course
的内容。
TITLE MASM Assignment 3 reverse a string word by word (main.asm)
; Description: Reverse a string character by character in-place then reverse word for word using nested loops
;
; Revision date:
INCLUDE Irvine32.inc
.data
source BYTE "CIS 335/535 is a great course",0
ecxbkp DWORD ? ;save ecx if necessary
.code
main PROC
mov edx,OFFSET source
call WriteString
call Crlf ; print\r\n
mov ecx, LENGTHOF source ;initialize loop counter
mov esi, OFFSET source ;esi starting address of string
mov edi, OFFSET source
dec edi
END_STRING:
inc edi
mov al,[edi]
cmp al,0 ;find zero byte
jnz END_STRING ;jump back to END_STRING if al is not 0
dec edi ;edi points to end of string
shr ecx, 1 ;ecx is loop count (shift one = length/2)
L1:
mov bl, [esi] ;load characters
mov al, [edi]
mov [esi], al ;swap characters
mov [edi], bl
inc esi ;update forward pointer by 1
dec edi ;decrement backward pointer by 1
loop L1 ;and loop
; display the string
mov edx,OFFSET source
call WriteString
call Crlf ; print\r\n
;Use nested loops to reverse word for word
mov ecx, LENGTHOF source ;set outer loop count ecx= entire length
mov esi, OFFSET source ;esi points to start of string
mov edi, OFFSET source
dec edi
mov ecxbkp, ecx ;save outer loop count
L2: ; go through beginning to end of string copy space character for length
inc edi
mov al, [edi] ;move edi address into al register
cmp al, ' ' ;find space character
loop L2
mov ecx, 16 ;modify inner loop count (length/2) (for swap)
dec edi
L3: ; reverse/swap word for word
mov bl,[esi]
mov al,[edi] ;load words
mov [esi], al
mov [edi],bl ;swap words
inc esi ;update forward pointer by 1
dec edi ;decrement backward pointer by 1
loop L3 ;and loop
mov ecxbkp, ecx ;restore outer loop count
; display the string
;mov edx,OFFSET source
;call WriteString
;call Crlf ; print\r\n
exit
main ENDP
END main
enter code here
一些让你上路的变化:
将loop L2
改为jne L2
不要在 ECX 中使用固定值 16,而是计算单词的确切长度。
将 EDI 中的位置保存在一个变量中,以便在 L3 循环中反转这个单词后能够继续下一个单词。
L2:
inc edi
mov al, [edi] ;move edi address into al register
cmp al, ' ' ;find space character
jne L2
mov SavedPointer, edi
lea ecx,[edi-1]
sub ecx,esi ;ECX is length of current word