反转MASM中的字符串.得到一个奇怪的输出



我正在尝试反转汇编语言类中的一个字符串。我相信我有正确的代码,但输出只是"a"。这是代码:

; Reverses an input string of at least 10 characters
; Author:  Nathan Smith
; Created: 10/26/2018
; Revisions:
; Date:             Modified by:
INCLUDE Irvine32.inc
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
myString BYTE "abcdefghij", 0       ; original string
temp BYTE ?                         ; temporary string holder
.code
main PROC
mov esi, SIZEOF myString    ; source index register
mov ecx, SIZEOF myString    ; loop counter
movzx eax, myString         ; moves string to temp holder
mov temp, al
mov edi, 0                  ; destination index register
L1:
mov al, temp[esi]           ; moves last letter to al
mov myString[edi], al       ; moves letter from al to myString
dec esi                     ; decreases pointer by 1
inc edi                     ; increases destination pointer by 1
loop L1                     ; repeat for length of string
mov edx, OFFSET myString
call WriteString
INVOKE ExitProcess,0
main ENDP
END main

我不知道发生了什么事。

字符串的长度需要考虑零索引,因此;

mov   esi, SIZEOF myString - 1         ; Should equal 8
mov   ecx, SIZEOF myString             ; Should equal 9

终止字符0x0NULL需要写入temp的末尾。为了至少进行一点优化,编写4个NULL是可行的,因为EDI只是一个32位寄存器。

xor   edi, edi
mov   temp[ecx],edi                    ; Writing 4 chars
; Instead of
mov   BYTE temp[ecx], 0

在你的循环中唯一需要改变的是;

@@:
mov   al, myString [edi]               ; Read char from source
mov   temp[esi], al                    ; and write to destination
dec   esi
inc   edi
loop  @B

你的程序发生了什么事

movzx eax, myString
mov   temp, al

正在将源的第一个字符移动到目标的第一个位置,由于您未能考虑零索引,源的终止字符是下一个写入temp的字符

相关内容

最新更新