MASM字符串反转 [英] MASM string reversal

查看:64
本文介绍了MASM字符串反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在解决这个问题,这可能是一个非常复杂的解决方案,但这是我想到的第一件事.

Alright, Im going about this, with what is probably a really complicated solution, but its the first thing that popped into my head.

我需要编写一个汇编语言程序,该程序可以反转源"字符串,而不使用目标"字符串(临时变量)..这是我的尝试.

I need to write an assembly language program that reverses a "source" string, without using a "target" string ( a temp variable ) .. this is my attempt at it.

INCLUDE Irvine32.inc
.data
source BYTE "This is the source string", 0
count DWORD ? 

.code
main PROC

 mov ecx, LENGTHOF source 

L1: 
 mov count, ecx     ; save our outer loop count
 mov al,[source+0]    ; get the first character in the string

 mov ecx,LENGTHOF source  ; set out inner loop count
 mov esi, OFFSET source
 inc esi
L2:


 mov bl,[esi]
 dec esi
 mov [esi],bl
 add esi, 2
 loop L2

 mov ecx, count
 loop L1


 mov  edx,OFFSET source
 call WriteString

 exit
main ENDP

END main

现在..的算法"基本上是这样的:从字符串中取出第一个字符,将所有其他字符向下移到字符数组中的一个空格处,然后将第一个取出的字符放到字符串的后面.数组.现在,我要说的太复杂了.实际上,我如何到达数组的后面.我想我需要另一个循环?我当然不需要三个循环,甚至不需要处理.

Now.. the "algorithm" for this is basically this: Take the first character out of the string, move all of the other characters down one space in the character array, put the character you first took out into the back of the array. Now, I'm to the point where this is much too complicated. Infact, how do I get to the back of the array.. I think I would need another loop? I certainly don't need three loops or even want to deal with that.

也许我在正确的轨道上,甚至都不知道.任何建议,技巧,代码或其他算法都将有所帮助!

Maybe I'm on the right track and don't even know it. any suggestions, tips, code or a different algorithm would help!

推荐答案

可以通过两个循环来实现.在执行完第一个循环之后,您必须再次执行一次,但长度要减少一个,以便将当前的第一个(最初是第二个)字符放在 second-last 位置,保留当前位置最后一个(最初是第一个)字符.然后继续直到长度降为零.

You could do it your way with two loops. After doing your first loop, you then have to do it again but with the length reduced by one, so that the current first (originally second) character gets put at the second-last position, leaving the current last (originally first) character alone. Then keep going until the length drops to zero.

但这是非常低效的,因为您有嵌套循环O(n 2 ).这是一种更好的算法,它仅使用一个循环O(n):

But that's pretty inefficient since you have the nested loops, O(n2). Here's a better algorithm that uses just one loop, O(n):

set spointer to point to the first character
set epointer to point to the last character
while epointer is greater than spointer:
    save away [spointer] (the character pointed to by spointer) somewhere
    copy [epointer] to [spointer]
    copy the saved character to [epointer]
    add one to spointer
    subtract one from epointer

基本上,它维护一个开始和结束指针,该指针最初会交换第一个字符和最后一个字符,然后将指针彼此相对移动.

It basically maintains a start and end pointer which initially swaps the first and last character, then the pointers are moved towards each other.

因此第二遍循环,您交换了倒数第二个和第二个字符,第三次交换了倒数第二个和第三个字符,依此类推.

So the second time through the loop, you swap the second and second-last character, the third time the third and third-last character and so on.

当指针相等(对于奇数长度的字符串)或开始指针大于结束指针(对于偶数长度的字符串)时,此过程停止.

This process stops when the pointers are equal (for an odd-length string) or the start pointer is greater than the end pointer (for an even-length string).

由于这可能是家庭作业(无论如何,您似乎都对x86有所了解),因此应执行将其转换为汇编程序的练习.

Since this may be homework (and you seem up to speed with x86 anyway), you should perform the exercise of converting that to assembler.

如果发现 not 不是作业,则可以使用下面的masm32代码作为基准.请不要假装将其作为您自己的教育工作,因为您几乎肯定会被吸引住.如果您自己解决转换问题,您将学到很多(作为作业非作业),如果您难以解决问题,我只是提供一些备用代码.

If it turns out not to be homework, you can use the masm32 code below as a baseline. Please don't try to pass this off as your own work for education since you'll almost certainly be caught out. You'll learn a great deal more (as homework or non-homework) if you tackle the conversion yourself, I'm just providing some fall-back code if you have trouble nutting it out.

.586
.model flat

.data
source byte "This is the source string", 0

.code
_main proc
    mov     esi, offset source   ; load up start pointer.

    mov     edi, offset source   ; set end pointer by finding zero byte.
    dec     edi
find_end:
    inc     edi                  ; advance end pointer.
    mov     al, [edi]            ; look for end of string.
    cmp     al, 0
    jnz     find_end             ; no, keep looking.
    dec     edi                  ; yes, adjust to last character.

swap_loop:
    cmp     esi, edi             ; if start >= end, then we are finished.
    jge     finished

    mov     bl, [esi]            ; swap over start and end characters.
    mov     al, [edi]
    mov     [esi], al
    mov     [edi], bl

    inc     esi                  ; move pointers toward each other and continue.
    dec     edi
    jmp     swap_loop

finished:
    int     3                    ; trap to enter debugger and check string.
                                 ; replace with whatever you need.
_main endp
end _main

这篇关于MASM字符串反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆