gpt4 book ai didi

c++ - 循环遍历一系列字符并在汇编中交换它们

转载 作者:行者123 更新时间:2023-12-05 09:26:05 27 4
gpt4 key购买 nike

我的学校作业是遍历字符串中的一系列字符并交换它们,使得最终结果是原始字符串的反向。

我已经编写了 3 个汇编函数和一个 cpp 函数,但是在下面的函数中,当我尝试运行该程序时出现了一些错误,我不确定如何修复它。我将在下面发布 cpp 代码和汇编代码并指出错误,如果有人能指出我的错误是什么,我将不胜感激!

我的c++代码如下

#include<iostream>
#include <string>

using namespace std;
extern"C"
char reverse(char*, int);

int main()
{
char str[64] = {NULL};
int lenght;

cout << " Please Enter the text you want to reverse:";
cin >> str;
lenght = strlen(str);

reverse(str, lenght);

cout << " the reversed of the input is: " << str << endl;

}

下面是我的汇编代码

.model flat

.code

_reverse PROC ;named _test because C automatically prepends an underscode, it is needed to interoperate

push ebp
mov ebp,esp ;stack pointer to ebp

mov ebx,[ebp+8] ; address of first array element
mov ecx,[ebp+12] ; the number of elemets in array
mov eax,ebx
mov ebp,0 ;move 0 to base pointer
mov edx,0 ; set data register to 0
mov edi,0

Setup:

mov esi , ecx
shr ecx,1
add ecx,edx
dec esi

reverse:

cmp ebp , ecx
je allDone

mov edx, eax
add eax , edi
add edx , esi

LoopMe:
mov bl, [edx]
mov bh, [eax]

mov [edx],bh
mov [eax],bl

inc edi
dec esi

cmp edi, esi
je allDone

inc ebp
jmp reverse

allDone:
pop ebp ; pop ebp out of stack
ret ; retunr the value of eax
_reverse ENDP

END

在接近开头的行上显示 push ebp 我收到一条错误消息

invalid instruction operands

在它读到 pop ebp 的末尾,我在它说同样的事情的地方遇到了一个错误。
不确定这是否很大,但我在读取 .model flat 的第一行代码中也遇到了语法错误。

最佳答案

根据重现的症状,我将问题诊断为:这是 32 位 x86 程序集(很明显),但它被视为 x64 程序集,这没有用。

  • .model 指令对 x64 无效,因此存在语法错误。
  • 压入和弹出 32 位寄存器在 x64 中不可编码,因此存在无效操作数错误。

如果这是在 Visual Studio 中的项目中,请将整个解决方案或此单个项目的“平台”设置为 x86/win32(它在不同的地方有不同的名称,但将其设置为 32 位)。

solution platform selector

关于c++ - 循环遍历一系列字符并在汇编中交换它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74507174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com