Two sum: Difference between revisions

1,174 bytes added ,  4 years ago
added X86 Assembly
(added MiniScript example)
(added X86 Assembly)
Line 2,221:
{{out}}
<pre>1, 3</pre>
 
=={{header|X86 Assembly}}==
{{trans|Python}}
{{works with|nasm}}
<lang asm>
section .data
outputArr dd 0,0,0
inputArr dd 5,0,2,11,19,90
section .text
global _main
_main:
mov ebp, esp
mov eax, 21 ;num we search for
push inputArr
call func
add esp, 4
ret
 
func:
mov esi, [ebp - 4];get arr address from stack
add esi, 4 ;esi now points to the first element instead of the length
mov edx, 0 ;i
mov ecx, [esi - 4] ;j
dec ecx ;counting starts from 0
looping:
cmp edx, ecx ;while i < j
jge return
mov ebx, [esi + edx * 4]
add ebx, [esi + ecx * 4] ;inputArr[i] + inputArr[j]
cmp ebx, eax ;inputArr[i] + inputArr[j] (==|<|else) eax
je end ;==
jl i ;<
dec ecx ;else j--
jmp looping
i:
inc edx ;i++
jmp looping
end:
mov eax, 2 ;if we find a combination our array has a length of 2
mov [outputArr], eax ;length is in the first 4 byte cell
mov [outputArr + 4], edx ;i
mov [outputArr + 8], ecx ;j
return:
mov eax, outputArr ;address of outputArr is returned in eax
ret
</lang>
 
=={{header|zkl}}==
13

edits