My assignment for school is to loop through a sequence of characters in a string and swap them such that the end result is the original string in reverse.
I have written 3 assembly functions and one cpp function but on the function below I am getting a few errors when I try to run the program and I'm not sure how to fix it. I will post both the cpp code and assembly code below with the errors pointed out, if anyone could point out what my mistake is I would appreciate it a lot!
My c code is below
#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;
}
Below is my assembly code
.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
On the line close to the beginning where it reads push ebp
I'm getting an error that says
invalid instruction operands
and towards the end where it reads pop ebp
I'm getting an error where it says the same thing.
Not sure if this is big but I'm also getting a syntax error on the very first line of code that reads .model flat
.
CodePudding user response:
Based on reproducing the symptoms, I diagnose the problem as: this is 32-bit x86 assembly (clearly), but it was treated as x64 assembly, and that didn't work.
- the
.model
directive is not valid for x64, so there is a syntax error there. - pushing and popping 32-bit registers is not encodeable in x64, so there are invalid operand errors there.
If this is in a project in Visual Studio, set the "platform" for either the whole solution or this individual project to x86/win32 (it has different names in different places, but set it to 32-bit).