Created
January 1, 2023 16:34
-
-
Save Auctoris/cc6db639ddd4f4e7358204fc29996f85 to your computer and use it in GitHub Desktop.
LLL Leet Code Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Note: The returned array must be malloced, assume caller calls free(). | |
*/ | |
__attribute__ ((naked)) int* twoSum(int* nums, | |
int numsSize, | |
int target, | |
int* returnSize) | |
{ | |
/* Inline assembly version - using AT&T syntax... */ | |
__asm__(".intel_syntax noprefix;\ | |
push rbx;\ | |
sub rsp, 0x40;\ | |
;\ | |
/* Store our params on the stack... */;\ | |
/* Note - int's are dword & int* are qword. */;\ | |
/* rsp + 0x10 <- nums* */;\ | |
/* rsp + 0x18 <- numsSize */;\ | |
/* rsp + 0x20 <- target */;\ | |
/* rsp + 0x28 <- returnSize */;\ | |
/* rsp + 0x30 <- 'output indicies' */;\ | |
mov qword ptr[rsp + 0x10], rdi;\ | |
mov dword ptr[rsp + 0x18], esi;\ | |
mov dword ptr[rsp + 0x20], edx;\ | |
mov qword ptr[rsp + 0x28], rcx;\ | |
;\ | |
/* malloc memory... */;\ | |
mov rdi, 0x08;\ | |
call malloc;\ | |
;\ | |
/* store ptr to malloc-ed memory */;\ | |
mov qword ptr[rsp + 0x30], rax;\ | |
/* outer-loop - iterate over the nums */;\ | |
xor r8, r8;\ | |
outerloop%=:\ | |
;\ | |
/* inner-loop - iterate over the other nums */;\ | |
mov r9, r8;\ | |
add r9, 0x01;\ | |
innerloop%=:\ | |
;\ | |
/* Set rbx to point to the first pointer in nums */;\ | |
mov rbx, qword ptr [rsp + 0x10];\ | |
xor rax, rax;\ | |
;\ | |
/* Set rbx to point to r8th value of nums... */;\ | |
/* ...and add value to rax. */;\ | |
lea rbx, [rbx + r8 * 4];\ | |
add eax, dword ptr [rbx];\ | |
;\ | |
/* Do the same for the r9th value... */;\ | |
/* ...remembering to reload rbx. */;\ | |
mov rbx, qword ptr [rsp + 0x10];\ | |
lea rbx, [rbx + r9 * 4];\ | |
add eax, dword ptr [rbx];\ | |
/* Load target for comparison... */;\ | |
mov ebx, dword ptr[rsp + 0x20];\ | |
cmp ebx, eax;\ | |
;\ | |
/* If not equal – continue the loop */;\ | |
jne continue%=;\ | |
;\ | |
/* Otherwise prepare for returning the answers */;\ | |
/* Set rax to point to the output memory & copy */;\ | |
/* the lower half of r8 (r8d) to the first byte ... */;\ | |
/* ...and r9d to the second byte. */;\ | |
mov rax, qword ptr [rsp + 0x30];\ | |
mov dword ptr [rax], r8d;\ | |
lea rax, [rax + 4];\ | |
mov dword ptr [rax], r9d;\ | |
;\ | |
/* Put a 2 into returnSize... */;\ | |
mov rax, qword ptr [rsp + 0x28];\ | |
mov dword ptr [rax], 0x02;\ | |
jmp leave%=;\ | |
continue%=:\ | |
;\ | |
/* Increment & check the loop counters */;\ | |
inc r9;\ | |
cmp r9d, dword ptr[rsp + 0x18];\ | |
jle innerloop%=;\ | |
inc r8;\ | |
cmp r8d, dword ptr[rsp + 0x18];\ | |
jle outerloop%=;\ | |
;\ | |
/* Return the answers, via rax */;\ | |
leave%=:\ | |
mov rax, qword ptr [rsp + 0x30];\ | |
add rsp, 0x40;\ | |
pop rbx;\ | |
ret;\ | |
.att_syntax;" : :); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment