Hi, I've been stuck on this for a while and need some help. My program is supposed to do what this blurb of c++ does:

int add(short * a, const short *b, const short *c, int n)
{
   for (int i=0; i<n; i++)
      a[i] += b[i] + c[i] + max(b[i],c[i]);
   return count-of-overflows;
}

It is supposed to return a 1 if the short a overflows, otherwise it returns 0. The value of a only matters if there is no overflow. Needless to say, it doesn't work how I want it to. Here's what I have so far:

of.s

.text
.global _add
_add:
	// prolog
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ebx
	pushl	%esi
	pushl	%edi

	// 8(%ebp)  - a (arg1) pointer to value of a[0]
	// 12(%ebp) - b (arg2) pointer to value of b[0]
	// 16(%ebp) - c (arg3) pointer to value of c[0]
	// 20(%ebp) - n (arg4) # of iterations
  // ------------------------------------------
		
	mov 8(%ebp), %ebx	         // ebx = *a
	mov 12(%ebp), %ecx	         // ecx = *b
	mov 16(%ebp), %edx         // edx = *c
	mov $0, %esi		// i = 0
	mov $0, %eax		// overflow count = 0
    begin:
	cmp %esi, 20(%ebp)                // if(%esi [i] < 20(%ebp) [n])
	jle quit                          // continue, else jump out of loop
	movb (%ebx, %esi, 2), %al         // %al = a[i]
         addb (%ecx, %esi, 2), %al         // %al += b[i]
	jo overflow      	               // if the OF flag is set, jump to overflow:
	addb (%edx, %esi, 2), %al	      // %al += c[i]
	jo overflow      	               // if the OF flag is set, jump to overflow:
         mov (%edx, %esi, 2), %edi	      // mov the value c[i] into %edi
	cmp (%ecx, %esi, 2), %edi	      // if b[i]>c[i]
	jl else                          // {
	    addb (%ecx, %esi, 2), %al    //   %al += b[i]
	    jo overflow      	     //   if the OF flag is set, jump to overflow:
	    jmp skip                     // }
	else:   			     // else c[i]>=b[i]  
	    addb (%edx, %esi, 2), %al    // {  %al += c[i]}                  
         skip:                                                   	
             mov %al, (%ebx, %esi, 2)     // a[i] = %al
             jmp next
         
         overflow:         
            inc %eax		     //increment overflow counter				              
	next:
            inc %esi	              // i++
	   jmp begin                    //jump back to begin:
    quit:
	
	
	
	// ------------------------------------------

	// return %eax, which should = 1 if overflow occured, otherwise it should = 0

	// epilog 
	popl	%edi
	popl	%esi
	popl	%ebx	
	popl	%ebp
	ret

I use the following .cpp file in conjunction with the command: g++ -Wall testof.cpp of.s -o testof.exe

It fails the assert in line 29

testof.cpp

#include <stdio.h>
#include <memory.h>
#include <assert.h>

extern "C" int add(short *a,const short *b,const short *c,int n);

short a[10000];
short b[10000];
short c[10000];

int main()
{
    int result;

    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(b));

    // simplest test
    result = add(a,b,c,0);
    assert(result==0);   

    // there are others 
    // just one
    a[7] = 5;
    b[7] = 9;
    c[7] = 3;
    result = add(a+7,b+7,c+7,1);
    assert(result==0);    //<-------------*this is where it fails*
    assert(a[7] == 26);
    assert(b[7] == 9);
    assert(c[7] == 3);

    printf("you did it!\n");
}

Any help at all would be very appreciated. If you can't help, then thanks for at least reading this far!

p.s. i know the comments are wrong, they should be #, not //. that's not the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.