简体   繁体   中英

Adding an array in embedded x86 assembly?

I'm new to assembly coding and embedding it in C++, the thing I'm trying to do is add the integers in an array using assembly. This is the code i have so far:

#include <iostream>
#include <stdio.h>
int x [] = {5,4,3,2,1};
int sumArray(int [5]);
int main()
{
    sumArray(x);
    printf_s("The sum of the array is %d");
}
int sumArray(int [5])
{
    __asm
    {
        mov edi,OFFSET sumArray
        mov ecx,5
        mov eax,0
    L1:
        add eax,[edi]
        add edi, TYPE sumArray
        loop L1
    }
}

An original problem I was having with was with mov ecx I had it as

mov ecx,LENGTHOF sumArray

but it wouldn't compile so I changed it to 5 and it compiled. So now when I run the program it breaks. I used F11 in Visual Studio to go line by line to see at what line the program breaks, and program breaks when its going through the loop a second time.

So if anyone can help me figure out how I can go about fixing it I would appreciate it.

It seems to me you have it broken quite a bit. First of all, you have a function named sumArray with an unnamed argument. But inside the function, you are referring to sumArray as if it were the name of the array argument. Then, you need to understand the way C(++) passes arrays as arguments: they are (always) passed by reference, as a pointer to the first array member. And it also means the function (in general) does not know the length of the array (unless you set it to a fixed-size). So, you usually pass the length in another argument. Which means we have the following:

int sumArray(int arr[], int len)
{
    __asm
    {
        mov edi, arr
        mov ecx, len
        xor eax, eax
    L1:
        add eax, [edi]
        add edi, 4
        loop L1
    }
}

Note that we are not trying to get an offset of the array, that would get us to the pointer, we need to get the value of the pointer, ie the address of the first array item. Also, note I have hardcoded the element size (4), there is no point acting like we can work with anything, if at the previous line, we add 32-bit words. (The xor eax, eax is just another way to set a register to zero, to be honest, in today's CPUs, I am not sure if it is faster or not.)

And when testing this, do not forget to actually pass the result to the printf_s

The problem with your code seems to be you are using your sumArray function name instead of your actual array x , and thats why it crashes.

Isn't your asm supposed to look like these:

__asm
{
    mov edi,OFFSET x
    mov ecx,LENGTHOF x
    mov eax,0
L1:
    add eax,[edi]
    add edi, TYPE x
    loop L1
}

? (here I assume, that you are not mistaken about macro usage, as I never compiled anything using MASM, that seems to be used here, but I think you got the idea)

The another question is why you pass unnamed argument to sumArray if you don't actually use it, you'd better then pass the array as a named argument and it's length and make use of them in your assembly code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM