I have a problem with execution of the project compiled in eclipse Version: 2021-12 (4.22.0)
The program is just 2 files:
- function.asm
.code32
.global array
.section .text
array: pushl %ebp
movl %esp, %ebp
pushl %ecx
pushl %esi
movl 12(%ebp), %ecx
movl 8(%ebp), %esi
xorl %eax, %eax
lp: addl (%esi), %eax
addl $4, %esi
loop lp
popl %esi
popl %ecx
popl %ebp
ret
- main.cpp
#include <iostream>
using namespace std;
extern "C" int array(int a[], int length); // external ASM procedure
int main()
{
int a[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0}; // array declaration
int array_length = 10; // length of the array
int sum = array(a, array_length); // call of the ASM procedure
cout << "sum=" << sum << endl; // displaying the sum
}
The program is compiled without any problems
make all
Building file: ../src/function.asm
Invoking: GCC Assembler
as -o "src/function.o" "../src/function.asm"
Finished building: ../src/function.asm
Building file: ../src/main.cpp
Invoking: Cygwin C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.o" -o "src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp
Building target: first.exe
Invoking: Cygwin C++ Linker
g++ -o "first.exe" ./src/function.o ./src/main.o
Finished building target: first.exe
but when I execute I get the following error:
0 [main] first 1941 cygwin_exception::open_stackdumpfile: Dumping stack trace to first.exe.stackdump
And the stack dump looks as follows:
Exception: STATUS_ACCESS_VIOLATION at rip=0010040108D
rax=0000000000000000 rbx=00000000FFFFCC30 rcx=0000000000000001
rdx=000000000000000A rsi=0000000000401109 rdi=0000000000008000
r8 =000000060001803F r9 =0000000000000000 r10=00000000FFFFCA50
r11=0000000100401189 r12=0000000180248C20 r13=00000000FFFFCC77
r14=0000000000000000 r15=00000000FFFFCC77
rbp=00000000FFFFCB80 rsp=00000000FFFFCB70
program=C:\WORKSPACE.ICE\first\Debug\first.exe, pid 102, thread main
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame Function Args
000FFFFCB80 0010040108D (000FFFFCBE0, 00100401109, 000FFFFCC77, 00180333F78)
000FFFFCB80 0018027FB40 (00100401109, 000FFFFCC77, 00180333F78, 0018027FB40)
000FFFFCB80 000FFFFCBB0 (000FFFFCC77, 00180333F78, 0018027FB40, 000FFFFCC30)
000FFFFCB80 000FFFFCBE0 (00180333F78, 0018027FB40, 000FFFFCC30, 00300000001)
000FFFFCB80 00100401109 (00180333F78, 0018027FB40, 000FFFFCC30, 00300000001)
000FFFFCBE0 00100401109 (00000000020, 70700000006FF00, 0018004A7AA, 00000000000)
000FFFFCCD0 0018004A816 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000 00180048353 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0 00180048404 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace
The same program is compiled and executed without any problems under Linux but the assemblies are written in INTEL syntax like that:
global array ; required for linker and NASM
section .text ; start of the "CODE segment"
array: push ebp
mov ebp, esp ; set up the EBP
push ecx ; save used registers
push esi
mov ecx, [ebp+12] ; array length
mov esi, [ebp+8] ; array address
xor eax, eax ; clear the sum value
lp: add eax, [esi] ; fetch an array element
add esi, 4 ; move to another element
loop lp ; loop over all elements
pop esi ; restore used registers
pop ecx
pop ebp
ret ; return to caller
Under the Linux the 32-bit code is compiled using:
nasm -f elf32 function.asm
g++ -m32 main.cpp function.asm
Could anybody please help me to identify where I go wrong?
Thanks Marek