Skip to content

Instantly share code, notes, and snippets.

@codekittie
Created December 6, 2020 04:41
Show Gist options
  • Save codekittie/730e9c05d9b763e2a0a6b601728a8a91 to your computer and use it in GitHub Desktop.
Save codekittie/730e9c05d9b763e2a0a6b601728a8a91 to your computer and use it in GitHub Desktop.
Sum of positives in array
SECTION .text
global positive_sum
; eax = return value (positive sum)
; rdi = pointer to array of ints (4 bytes)
; esi = length of array in rdi
positive_sum:
xor eax, eax
loop:
cmp WORD [rdi], 0 ; check if [rdi] greater than 0
jle skip
add eax, [rdi]
skip:
cmp esi, 0 ; check if any nums left
je return ; if not, jump to return
add rdi, 4 ; add 4 to rdi, to go to next int in array
sub esi, 1 ; subtract one from our count, stored in esi
return:
ret
@seisvelas
Copy link

Hey Khety! Great to see how you're coming along as a badass asm coder (especially at 13!). Here is what I believe to be a working version:

positive_sum:
  xor eax, eax
loop:
  cmp WORD [rdi], 0 ; check if [rdi] greater than 0
  jle skip
  add eax, [rdi]
skip:
  cmp esi, 1 ; check if any nums left
  je return ; if not, jump to return
  
  add rdi, 4 ; add 4 to rdi, to go to next int in array
  
  sub esi, 1 ; subtract one from our count, stored in esi
  jmp loop

return:
  ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment