我在學校的作業是遍歷字串中的一系列字符并交換它們,以便最終結果是原始字串的反向。
我已經撰寫了 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我收到一條錯誤訊息
無效的指令運算元
在它讀到的最后,pop ebp我在它說同樣的事情的地方遇到了一個錯誤。
不確定這是否很大,但我在讀取的第一行代碼中也遇到了語法錯誤.model flat。
uj5u.com熱心網友回復:
基于重現癥狀,我將問題診斷為:這是 32 位 x86 程式集(很明顯),但它被視為 x64 程式集,但沒有用。
- 該
.model指令對 x64 無效,因此存在語法錯誤。 - 壓入和彈出 32 位暫存器在 x64 中不可編碼,因此那里存在無效運算元錯誤。
如果這是在 Visual Studio 中的專案中,請將整個解決方案或此單個專案的“平臺”設定為 x86/win32(它在不同的地方有不同的名稱,但將其設定為 32 位)。

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/537475.html
標籤:C 部件x86masm
