1 Replies - 964 Views - Last Post: 26 October 2019 - 07:51 AM

#1 fearless_swami   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 24-September 18

Adding the the elements in an array, getting 0...

Posted 17 October 2019 - 11:54 PM

I have to sum all of the elements in an array, and I am getting the sum of the elements to be 0. I think it has to do with when I am adding the values to my sum variable (i.e. sum += a[i]). Here is my code below:

This is the array being used for testing:

int a[5] = {1,2,3,4,5};
printf("Result of add_array(a,5) is %d\n", add_array(a,5));



This is the assembly code:


        
	.text
	.globl	add_array
	.def	add_array;	.scl	2;	.type	32;	.endef

	
add_array:

	

	pushq	%rbp		# LEAVE THIS ALONE
	movq	%rsp, %rbp	# LEAVE THIS ALONE

	
	movl $0,%eax  	# sum = 0
	
	movq $0,%rcx 	# i = 0 (use a 64-bit register, see hint above)



TOP:
	
	cmpl %eax,%edx  # compare i to size



	jmp DONE  #if i is not less than size, jump out of loop

	leaq (%edx,%ecx,4), %eax   # sum += a[i]
	incq %rcx  # i++
	jmp TOP  # jump to top of loop

/* THIS IS OUTSIDE THE LOOP, NEED A LABEL */
DONE:

	/* Make sure result is in %eax */

	popq	%rbp	
	retq		








Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Adding the the elements in an array, getting 0...

#2 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 172
  • View blog
  • Posts: 1,114
  • Joined: 03-April 12

Re: Adding the the elements in an array, getting 0...

Posted 26 October 2019 - 07:51 AM

Hint: check your register usage.
And where is the adding supposed to take place?

This post has been edited by turboscrew: 26 October 2019 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1