Assembly function for adding arrays of 32 bit elements not working properly

0

I'm trying to make an Assembly(FASM) function for C++ that adds 2 arrays with 32 bit elements, but it doesn't seem to be working properly. It returns {409, 0, 0} instead of {0x99999999, 0x06000001, 0x00000000}. What could be causing this?

C++ program that calls the function:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
extern "C"
{
    unsigned int __stdcall long_add(int* , int* , int*);
}

int main()
{
    int testOne[3] = { 0x12345678, 0x04000000, 0xFFFFFFFF };
    int testTwo[3] = { 0x87654321, 0x02000000, 0x00000001 };
    int testOut[3] = {};

    long_add(testOne, testTwo, testOut);

    scanf;

The Assembly function itself:

include 'include\win32a.inc'

format MS COFF 

public long_add as '_long_add@12'

section '.code' code readable executable

proc long_add IN_NUM1, IN_NUM2, OUT_NUM

start:

        mov ecx, 3
        clc
        mov edi, [IN_NUM1]
        mov esi, [IN_NUM2]
        
ADDING:

        mov eax, [edi+(ecx-1)*4]
        mov ebx, [esi+(ecx-1)*4]
        adc eax, ebx
        push eax
        loop ADDING
        
        mov esi, [OUT_NUM]
        mov ecx, 3
        
POPPING:
        
        pop eax
        mov [esi], eax
        inc esi
        loop POPPING
        
        ret
        endp
c++
assembly
fasm

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0