-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasm.c
108 lines (98 loc) · 2.13 KB
/
asm.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include<string.h>
int gcd( int a, int b ) {
int result ;
/* Compute Greatest Common Divisor using Euclid's Algorithm */
__asm__ __volatile__ ( "movl %1, %%eax;"
"movl %2, %%ebx;"
"CONTD: cmpl $0, %%ebx;"
"je DONE;"
"xorl %%edx, %%edx;"
"idivl %%ebx;"
"movl %%ebx, %%eax;"
"movl %%edx, %%ebx;"
"jmp CONTD;"
"DONE: movl %%eax, %0;" : "=g" (result) : "g" (a), "g" (b)
);
return result ;
}
int sum(int a,int b)
{
int result = 0;
__asm__ __volatile (
"movl %1, %%eax;"
"addl %2, %%eax;"
: "=g" (result) : "g" (a), "g" (b)
);
return result;
}
int sub(int a,int b)
{
int result = 0;
__asm__ __volatile (
"movl %1, %%eax;"
"subl %2, %%eax;"
: "=g" (result) : "g" (a), "g" (b)
);
return result;
}
int mul(int a,int b)
{
int result = 0;
__asm__ __volatile (
"movl %1, %%eax;"
"imul %2, %%eax;"
: "=g" (result) : "g" (a), "g" (b)
);
return result;
}
int div(int a,int b)
{
int result = 0;
__asm__ __volatile (
"movl %1, %%eax;"
"movl $0, %%edx;"
"idivl %2, %%eax;"
: "=g" (result) : "g" (a), "g" (b)
);
return result;
}
void swap(int * a, int * b)
{
// //int temp = *a;
// //*a = *b;
// //*b = temp;
// int temp= 0;
// __asm__ __volatile (
// "movl offset %1, %%esi;"
// "movl $8, %%[%%esi];"
// : "=g" (temp) : "g" (a), "g" (b)
// );
// printf("\n temp = %d",*a);
}
int x(char ** str)
{
int len = strlen(*str);
int num =0;
char ch = 'a';
int result = 0;
__asm__ __volatile (
"lea %1,%%ebx;"
"movb 1(%%ebx), %%eax;"
: "=g" (ch) : "g" (*str), "g" (len)
);
printf("\n%c",ch);
printf("\nstring = %s",*str);
return 0;
}
int main()
{
char * str = "hello world";
x(&str);
printf("\n%s",str);
//printf("\n%d",sum(2,3));
//printf("\n%d",sub(6,3));
//printf("\n%d",mul(3,5));
//printf("\n%d",div(10,5));
return 0 ;
}