-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathint_sqrt.c
More file actions
47 lines (40 loc) · 918 Bytes
/
int_sqrt.c
File metadata and controls
47 lines (40 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
auther: Naman Tamrakar
date: 2023-07-19
level: easy
url: https://leetcode.com/problems/sqrtx/
question: Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
*/
#include <stdio.h>
#include <stdlib.h>
int mySqrt_c(int x){
long int i = 0;
while (i*i <= x)
i++;
return i-1;
}
__attribute__((naked))
int mySqrt(int x){
__asm__(
// long int i = 0;
"movl $0, %eax;"
"l1:;"
// while (i*i <= x)
"movq %rax, %rdx;"
"imul %rdx, %rdx;"
"cmp %rdi, %rdx;"
"jg end;"
// i++;
"incl %eax;"
"jmp l1;"
"end:;"
// return i-1;
"decl %eax;"
"ret;"
);
}
int main() {
int k;
scanf("%d", &k);
printf("%d", mySqrt(k));
}