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:


.global _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
	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]}                  
             mov %al, (%ebx, %esi, 2)     // a[i] = %al
             jmp next
            inc %eax		     //increment overflow counter				              
            inc %esi	              // i++
	   jmp begin                    //jump back to begin:
	// ------------------------------------------

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

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

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


#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;


    // simplest test
    result = add(a,b,c,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!

6 Years
Discussion Span
Last Post by jnr05a

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.