I am using Visual Studio 2019 Community edition and I'm trying to run an Assembly function from a C program.
I get the following error:
Rebuild started...
1>------ Rebuild All started: Project: ArrayReverser, Configuration: Debug Win32 ------
1>Assembling GetValueFromASM.asm...
1>ArrayReverser.cpp
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp__srand referenced in function _main
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp__rand referenced in function _main
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp____acrt_iob_func referenced in function _printf
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp____stdio_common_vfprintf referenced in function __vfprintf_l
1>C:\Users\firas\source\repos\ArrayReverser\Debug\ArrayReverser.exe : fatal error LNK1120: 4 unresolved externals
1>Done building project "ArrayReverser.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
I have tried almost every solution I could find on the internet, but nothing worked, I hope someone could give an insight on what is happening.
here is the C Code:
#include <iostream>
#include <stdlib.h>
extern "C" void GetValueFromASM(int* y, const int *x, int n);
int main(int argc, char* argv[])
{
const int n = 10;
int x[n], y[n];
srand(41);
for (int i = 0; i < n; i++)
x[i] = rand() % 1000;
GetValueFromASM(y, x, n);
printf("\n---------------Array Reverser-------------\n");
for (int i = 0; i < n; i++)
{
printf("i: %5d y: %5d x: %5d\n", i, y[i], x[i]);
}
return 0;
}
and here is the Assembly Code:
.386
.model flat,c
.code
GetValueFromASM PROC
push ebp ;we pushed the register ebp onto the stack
mov ebp,esp
push esi
push edi
xor eax,eax
;now we load the parameters (int* y, const int* x, int n) onto edi,esi,ecx
mov edi,[ebp+8] ;bp - stack base Pointer Register
mov esi,[ebp+12]
mov ecx,[ebp+16]
; in order to reverse the array elemtns we use the following command
lea esi,[esi+ecx*4-4]
pushfd
std
@@: lodsd
mov [edi], eax
add edi, 4
dec ecx
jnz @B
popfd
mov eax,1
;Epilouge
pop edi
pop esi
pop ebp
GetValueFromASM endp
end
link to my project files: https://drive.google.com/file/d/1entjG8nHarDTW0AEmvKhv0Cvrw9v0BF2/view?usp=sharing