Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1.  
  2. # LEAVE THIS PART ALONE
  3. .text
  4. .globl add_array
  5. .def add_array; .scl 2; .type 32; .endef
  6.  
  7. # See the declaration in add_array.h
  8. # This function takes two parameters:
  9. # -- an array (i.e. a 64-bit pointer in the %rcx register)
  10. # -- the size of the array (i.e. a 32-bit integer in
  11. # the %edx register.
  12. # It returns the sum of the elements of the array. The return
  13. # value, being 32 bits, must be placed in the %eax register.
  14.  
  15. add_array:
  16.  
  17. # Note: You can overwrite the 64-bit registers %rax, %rcx, %rdx, %r8,
  18. # %r9, %r10, %r11 (as well as their 32-bit halves, %eax, %ecx,
  19. # %edx, %r8d, %r9d, %r10d, %r11d) as you like. These are the
  20. # "caller-saved" registers.
  21.  
  22. # hint: to access a[i], use scaled indexed addressing
  23. # mode: "(base_reg,index_reg,scale)" -- see page 180 of Bryant & O'Hallaron
  24. # If the base_reg is a 64-bit register (e.g. %rcx holding the address of
  25. # the array), then the index_reg (e.g. holding i) must also be a 64-bit
  26. # register.
  27.  
  28. pushq %rbp # LEAVE THIS ALONE
  29. movq %rsp, %rbp # LEAVE THIS ALONE
  30.  
  31. movl $0, %r9d # sum = 0
  32. movq $0, %r10 # i = 0 (use a 64-bit register, see hint above)
  33.  
  34. _LOOPER:
  35.  
  36. cmpq %rdx, %r10 # compare i to size
  37. jge _BREAK # if i is not less than size, jump out of loop
  38.  
  39. addl (%r10,%rcx,4),%r9d # sum += a[i]
  40. incq %r10 # i++
  41. jmp _LOOPER # jump to top of loop
  42.  
  43. _BREAK:
  44.  
  45. movl %r9d,%eax
  46.  
  47. popq %rbp # LEAVE THIS ALONE
  48. retq # LEAVE THIS ALONE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement