c++ - 使用交换方法在程序集中反转外部数组 - x86 MASM

标签 c++ arrays assembly masm

我正在做一个项目,我们需要传递一个 char 类型的数组作为参数并反转数组。我觉得我已经非常接近完成它了,但我仍然停留在实际的交换过程中。

对于我的 .asm 中的交换函数,我使用了与在 C++ 中相同的方法(使用未使用的寄存器作为临时寄存器,然后交换前面和后面。)我不明白的是我将如何去做更改该地址的实际内容。我假设执行以下操作会“更改”目标地址的内容:

mov eax,[edx]

然而,这并没有按计划进行。在我运行 for 循环再次遍历数组后,一切都保持不变。

如果有人能指出我正确的方向,那就太好了。我已经为下面的代码提供了尽可能多的评论。

此外,我在一个 .asm 文件中完成了所有这些工作;但是,我的教授希望我为以下每个函数创建 3 个单独的 .asm 文档:swap、reverse 和 getLength。我试图在 reverse.asm 中包含其他 2 个 .asm 文件,但它一直给我一个错误。

汇编代码开始:

.686
.model flat

.code

_reverse PROC 
    push ebp
    mov ebp,esp ;Have ebp point to esp

    mov ebx,[ebp+8] ;Point to beginning of array
    mov eax,ebx
    mov edx,1
    mov ecx,0
    mov edi,0
    jmp getLength

getLength:
    cmp ebp, 0          ;Counter to iterate until needed to stop
    je setup

    add ecx,1
    mov ebp,[ebx+edx]
    add edx,1
    jmp getLength

setup:                  ;This is to set up the numbers correctly and get array length divided by 2
    mov esi,ecx
    mov edx,0
    mov eax,ecx
    mov ecx,2
    div ecx

    mov ecx,eax
    add ecx,edx         ;Set up ecx(Length of string) correctly by adding modulo if odd length string
    mov eax,ebx
    dec esi

    jmp reverse

reverse:                ;I started the reverse function by using a counter to iterate through length / 2
    cmp edi, ecx
    je allDone

    mov ebx,eax         ;Set ebx to the beginning of array
    mov edx,eax         ;Set edx to the beginning of array
    add ebx,edi         ;Move ebx to correct index to perform swap
    add edx,esi         ;Move edx to the back at the correct index

    jmp swap            ;Invoke swap function

swap:
    mov ebp,ebx         ;Move value to temp
    mov ebx,[edx]       ;Swap the back end value to the front
    mov edx,[edx]       ;Move temp to back

    inc edi             ;Increment to move up one index to set up next swap
    dec esi             ;Decrement to move back one  index to set up for next swap

    jmp reverse         ;Jump back to reverse to setup next index swapping

allDone:
    pop ebp
    ret

_reverse ENDP

END

C++代码开始:

#include <iostream>
#include <string>

using namespace std;

extern "C" char reverse(char*);

int main()
{
    const int SIZE = 20;
    char str1[SIZE] = { NULL };

    cout << "Please enter a string: ";
    cin >> str1;

    cout << "Your string is: ";

    for (int i = 0; str1[i] != NULL; i++)
    {
        cout << str1[i];
    }

    cout << "." << endl;

    reverse(str1);

    cout << "Your string in reverse is: ";

    for (int i = 0; str1[i] != NULL; i++)
    {
        cout << str1[i];
    }

    cout << "." << endl;

system("PAUSE");
return 0;
}

最佳答案

因此,经过多个小时的修补和环顾四周,我终于弄清楚如何正确地复制一个字节。如果有人需要它以供将来引用,我将在下面发布我的 .asm 代码和评论。

我实际上是将当前地址的内容移动到一个 32 位寄存器中。在我将它从 mov ebx,[eax] 更改为 mov bl,[eax] 后,它正确地复制了值。

我只会发布我遇到困难的代码,所以我不会将整个项目交给其他学生。

下面的 ASM 代码:

swap:
mov bl,[edx]        ;Uses bl since we are trying to copy a 1 byte char value
mov bh,[eax]        ;Uses bh since we are trying to copy a 1 byte char value

mov [edx],bh        ;Passing the value to the end of the array
mov [eax],bl        ;Passing the value to the beginning of the array

inc eax             ;Moving the array one index forward
dec edx             ;Moving the array one index backwards

dec ecx             ;Decreasing the counter by one to continue loop as needed

jmp reverse         ;Jump back to reverse to check if additional swap is needed

感谢所有提供帮助的人。

关于c++ - 使用交换方法在程序集中反转外部数组 - x86 MASM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513679/

相关文章:

assembly - MASM 不会插入带有某些 CPU 和 FPU 指令组合的 x87 WAIT 前缀

assembly - 为什么我不能在 MARS 中使用 li.s?

c++ - 代码编译,但没有控制台输出

java - 如何修复java中的dispatchUncugthException

c# - 从 WRL COM 组件获取托管回调

c - 在 c 中查找数组中元素数量的问题

javascript - 检查 Javascript 数组中是否存在属性值为 'x' 的对象

assembly - 不能在寻址方式下减去寄存器?

c++ - IPV6双栈模式检查ipv6内核模块是否加载

c++ - C++ 预处理器中的 R 和 L 有什么特别之处?