1

I am new to assembly language programming and facing a problem that might be obvious to experienced assembly language users. I have a 100 byte buffer and I need to find the sum of every nth byte for n = 1 to 5 and store the result in a array of 5 integers. I need to use inline assembly in my c++ code to do this. I wrote following code:

void main()
{
    char *buffer = new char[100];
    int *result = new int[5]();

    for (int i = 0; i < 100; i++)
        buffer[i] = i;

    char *start = buffer;
    for (int k = 0; k < 20; k++)
    {
        for (int j = 0; j < 5; j++)
        {
        __asm
            {
                mov eax, start[j]
                mov ebx, result[j]
                add eax, ebx
                mov result[j], eax

            }

        }
        start += 5;
    }
}

So at the end, result[0] should have sum of buffer[0], buffer[5], buffer[10] .... , result[1] will have sum of buffer[1], buffer[6], buffer[11] .... and so on. I am getting Access Violation error at the first assembly instruction itself (mov eax, start[j]). Can anyone help me find out the mistake I have done? Also it would be great if someone can help me to write the entire loop and summation part in assembly language

4
  • Are you sure that is what you want? Because that is not, what I would understand as the sum of every nth byte.
    – dornhege
    Oct 7, 2014 at 13:23
  • How much of the task is to be done in assembly? You are only adding numbers in assembly. Oct 7, 2014 at 13:45
  • This is simplified version of what I want to do. For now, I would be happy if I can write the loop and summation in assembly. I understand that there would not be much performance advantage compared to normal c++ code, but the real scenario where I want to use this concept may result in some advantage
    – Rahul
    Oct 7, 2014 at 14:23
  • Do you really want inline assembly or would you be happy with NASM? Also, what about using static arrays instead of dynamic arrays?
    – Z boson
    Oct 9, 2014 at 8:29

1 Answer 1

1

Obviously I don't know your actual intent, but I question the assumption that "the real scenario where I want to use this concept may result in some advantage."

It may not be 100% accurate to say that humans can no longer write efficient assembler for i386, but it's nearly true. If you are familiar with pipelining and out-of-order execution, you already understand why this is so. If you aren't familiar with these, you are already saying you don't know how to write efficient assembler.

That's not to say you shouldn't look at the assembler for your program's hotspots. But you should write the most efficient c code you can and benchmark it before trying to see if you can write something better in asm. Don't be surprised if you can't.

  • Remember that just because some tiny routine performs better in a tiny test program, that doesn't guarantee that it will do so when included back in the original program.
  • Or that it will perform better on all processors.
  • Or in new versions of the compiler.
  • And of course using asm means that moving to new platforms (like x64) will probably require the asm to get re-written, causing the people doing the work to curse your name.

That said, you could try benchmarking something like this. My guess is that it will be better, but that's just a guess.

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 100
#define MAXTOT 5

typedef unsigned char BYTE;

int main()
{
    BYTE *buffer = (BYTE *)malloc(MAXSIZE);
    const BYTE *start = buffer;
    unsigned int t0, t1, t2, t3, t4;

    for (int i = 0; i < MAXSIZE; i++)
        buffer[i] = i;

    t0 = 0;
    t1 = 0;
    t2 = 0;
    t3 = 0;
    t4 = 0;

    for (int j=0; j < (MAXSIZE / MAXTOT); j++)
    {
        t0 += start[0];
        t1 += start[1];
        t2 += start[2];
        t3 += start[3];
        t4 += start[4];

        start += MAXTOT;
    }

    printf("%u %u %u %u %u\n", t0, t1, t2, t3, t4);

    free(buffer);

    return 0;
}

The loop looks like this in asm (using gcc -O2):

L3:
    movzbl  (%edx), %edi
    addl    $5, %edx
    addl    %edi, 44(%esp)
    movzbl  -4(%edx), %edi
    addl    %edi, %ebx
    movzbl  -3(%edx), %edi
    addl    %edi, %eax
    movzbl  -2(%edx), %edi
    addl    %edi, %ecx
    movzbl  -1(%edx), %edi
    addl    %edi, %esi
    cmpl    40(%esp), %edx
    jne     L3

This keeps as many of the 'results' in registers for the duration of the calculation as it can (rather that constantly reading/writing them all to memory like your existing code). Fewer loops also means fewer cmp instructions, and this only makes one pass thru the buffer instead of 5. Compiling for x64 (much easier now that there's no asm) gives even better code, since there are more registers available.

Obviously this falls apart if MAXTOT gets bigger. But I can only comment on the code I can see, and 5 is what you used.

FWIW.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.