【问题标题】:Reverse String MASM反向字符串 MASM
【发布时间】:2021-03-15 01:37:47
【问题描述】:

我有这个代码设计为在“originString”中获取一个字符串,并将其以相反的顺序放在“destinationString”中。我不知道如何使destinationString 填满,而不仅仅是保存一个值,然后还用writeString 打印出来,而不仅仅是writeChar。这是我所拥有的。

INCLUDE     Irvine32.inc
INCLUDELIB  Irvine32.lib

.386
.model flat, stdcall
.stack 4096

ExitProcess PROTO, dwExitCode:DWORD

.data
originString        BYTE    "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0
destinationString   BYTE    SIZEOF originString DUP(?), 0
counter             BYTE    1




.code
main PROC
    ; YOUR CODE GOES HERE...
    ; Write a procedure that copies the contents of 'originString' to
    ;  'destinationString', but in reverse order.  You should use the
    ;  provided initialization value for 'originString' to test, but your
    ;  solution must work even if the contents or 'originString' are changed.
    MOV EAX, LENGTHOF originString
    MOV ECX, EAX

countLoop_BEGIN:
                    
    MOVZX EAX, counter
    MOV EBX, LENGTHOF originString
    SUB EBX, EAX
    MOV AL, BYTE PTR [originString + (EBX-1)]
    cmp destinationString, ' '
    je   end_shifting
    inc EBX
    MOV destinationString, AL
    MOVZX EDX, destinationString
    CALL WriteString
    ;CALL Crlf
    INC counter

    LOOP countLoop_Begin    ; If ECX IS NOT EQUAL to 0, decrement ECX by 1 and JMP to 'countLoop_Begin'.
                            ; If ECX IS EQUAL to 0, do not jump and move onto the instruction following LOOP.

    MOV destinationString, AL
    MOV EDX, OFFSET destinationString
    CALL WriteString
    CALL Crlf
    end_shifting:    
        ret


    INVOKE ExitProcess, 0

main ENDP



END main

【问题讨论】:

    标签: assembly masm irvine32 masm32


    【解决方案1】:

    我不知道如何让destinationString 填满而不是只保留一个值

    在您的循环中,指令MOV destinationString, AL 始终写入destinationString 的第一个位置。为什么不像从 originString 读取的工作方式那样索引写入?您也可以使用 counter 来索引写入。

    这是快速修复:

    countLoop_BEGIN:
        MOVZX EDX, counter                  ; 1,2,3,...
        MOV   EBX, LENGTHOF originString
        SUB   EBX, EDX
        MOV   AL, [originString + (EBX-1)]
        MOV   [destinationString + (EDX-1)], AL
        INC   counter
        DEC   ECX
        JNZ   countLoop_Begin
    

    然后也用 writeString 打印出来,而不仅仅是 writeChar。

    要使 WriteString 工作,您仍然需要将目标字符串以零结尾。

        MOV   byte [destinationString + EDX], 0
        MOV   EDX, OFFSET destinationString
        CALL  WriteString
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 2010-09-06
      • 2016-09-15
      • 1970-01-01
      相关资源
      最近更新 更多