# Assembly Functions

Feb 5th, 2021
665
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include<iostream>
2. using namespace std;
3.
4. int abss(int value) {
5.     _asm {
6.         mov eax, value
7.         cmp eax, 0
8.         jge skip
9.         neg eax
10.         skip :
11.         mov value, eax
12.     }
13.     return value;
14. }
15.
16. int adder(int b, int c) {
17.     _asm {
18.         mov eax, b
20.         mov b, eax
21.     }
22.     return b;
23. }
24.
25. int power(int x) {
26.     _asm {
27.         mov eax, x
28.         iMul x
29.         mov x, eax
30.     }
31.     return x;
32. }
33.
34. // Write in Assembly function :  int perimeter_of_rectangle (int a, int b), which calculates perimeter of rectangle.
35. int perimeter_of_rectangle(int a, int b) {
36.     // int result;
37.     _asm {
38.         mov eax, a; // you can put ; if you like
39.         add eax, a; is comment
42.         mov a, eax; mov result, eax
43.     }
44.     return a; // return result;
45. }
46.
47. //Write in Assembly function : int area_of_rectangle(int a, int b) that calculates the area of rectangle.
48. int area_of_rectangle(int a, int b) {
49.     // int result;
50.     _asm {
51.         mov eax, a;
52.         mul b;
53.         mov a, eax;
54.     }
55.     // return result;
56.     return a;
57. }
58.
59. //Write in Assembly function : int perimeter_of_square(int a), which calculates perimeter of square.
60. int perimeter_of_square(int a) {
61.     _asm {
62.         mov eax, a;
63.         // ; mul 4; // you can not multiply by constant
64.         // processors works only with registers and memory
65.         mov ecx, 4;
66.         mul ecx;
67.         mov a, eax;
68.     }
69.     return a;
70. }
71.
72. //Write in Assembly function : int area_of_square(int a) that calculates the area of square.
73. int area_of_square(int a) {
74.     _asm {
75.         mov eax, a;
76.         mul a; // in this case a is in memory
77.         mov a, eax;
78.     }
79.     return a;
80. }
81.
82. //Write in Assembly function : int perimeter_of_triangle(int a, int b, int c), which calculates perimeter of triangle.
83. int perimeter_of_triangle(int a, int b, int c) {
84.     _asm {
85.         mov eax, a;
88.         mov a, eax;
89.     }
90.     return a;
91. }
92.
93. //Write in Assembly function : int perimeter_of_triangle2(int a), which calculates perimeter of equilateral  triangle
94. int perimeter_of_triangle2(int a) {
95.     _asm {
96.         mov eax, a;
97.         mov ecx, 3;
98.         mul ecx;
99.         mov a, eax;
100.     }
101.     return a;
102. }
103.
104. //Write in Assembly function : int perimeter_of_triangle3(int a, int b), which calculates perimeter of rectangular triangle.
105. int perimeter_of_triangle3(int a, int b) {
106.     int result = 0;
107.     int temp = 0;
108.     _asm {
109.         mov eax, a;
110.         mul a;
111.         mov ecx, eax;
112.         mov eax, b;
113.         mul b;
115.         mov temp, eax;
116.         mov eax, a;
118.         mov result, eax;
119.     }
120.     result += sqrt(temp);
121.     return result;
122. }
123.
124. //Write in Assembly function : int perimeter_of_triangle4(int a, int h), which calculates perimeter of triangle  from side length and height.
125. int perimeter_of_triangle4(int a, int h) {
126.     _asm {
127.
128.     }
129.     return a;
130. }
131.
132. int area_of_triangle(int a, int b, int c) {
133.     // P = sqrt(s*(s-a)*(s-b)*(s-c) ); s = (a + b + c)/2;
134.     int result = 0;
135.     _asm {
136.         mov eax, a;
139.         mov ecx, 2;
140.         mov edx, 0;
141.         div ecx;
142.         mov ecx, eax;
143.         sub ecx, a;
144.         mul ecx;
145.         add ecx, a; // get back to s
146.         sub ecx, b;
147.         mul ecx;
148.         add ecx, b; // get back to s
149.         sub ecx, c;
150.         mul ecx;
151.         mov result, eax;
152.
153.     }
154.     return sqrt(result);
155. }
156.
157. //Write in Assembly function : int area_of_cube(int a), which calculates area of cube.
158. int area_of_cube(int a) {
159.     _asm {
160.
161.     }
162.     return a;
163. }
164.
165.
166. int main()
167. {
168.     std::cout << "Hello world!\n";
169.     cout << abss(-5) << endl;
170.     cout << adder(3, 6) << endl;
171.     cout << power(5) << endl;
172. }
RAW Paste Data