AreWe

da opravq push/pop

May 16th, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5. using std::cin;
  6.  
  7. //eax - function return
  8. //edx - general purpose trashable register
  9. //ecx - general purpose trashable register; Use for array lengths when looping!
  10. //edi - use for array
  11.  
  12. //imul op1, op2 - saved product of two in op1.
  13. //xor op, op - wipe
  14. //sub op1, op2 - saves op1 - op2 in op1
  15.  
  16. int multAsm(int x, int y)
  17. {
  18. _asm
  19. {
  20. push edx
  21. push ecx
  22. mov edx, x
  23. mov ecx, y
  24. imul edx, ecx
  25. mov eax, edx
  26. pop ecx
  27. pop edx
  28. }
  29. }
  30.  
  31. int sumAsm(int x, int y) {
  32. _asm
  33. {
  34. push edx
  35. push ecx
  36. mov edx, x
  37. mov ecx, y
  38. adc edx, ecx
  39. mov eax, edx
  40. pop ecx
  41. pop edx
  42. }
  43. }
  44.  
  45.  
  46. int sumArray(int arr[], int len)
  47. {
  48. __asm
  49. {
  50. mov edi, arr
  51. mov ecx, len
  52. xor eax, eax // sum
  53. L1 :
  54. add eax, [edi]
  55. add edi, 4
  56. loop L1 //this decrements ecx and loops until it reaches 0
  57. }
  58. }
  59.  
  60. int findMax(int arr[], int len) {
  61. __asm
  62. {
  63. mov edi, arr
  64. mov ecx, len
  65. mov eax, [edi] //max
  66.  
  67. L1 :
  68. cmp eax, [edi]
  69. jl Replace
  70.  
  71. add edi, 4
  72. loop L1
  73.  
  74. jmp Done
  75.  
  76. Replace : // saves the current value as the max one
  77. mov eax, [edi]
  78. jmp L1
  79.  
  80. Done :
  81. }
  82. }
  83.  
  84.  
  85. int main()
  86. {
  87.  
  88. cout << "x*y (asm) = " << multAsm(8, 6) << endl;
  89.  
  90. cout << "x + y = " << sumAsm(5, 6) << endl;
  91.  
  92. int ar[] = { 9, 6, 7, 5, 4, 3, 2, 56, 1 };
  93. cout << sumArray(ar, 9) << endl;
  94. cout << findMax(ar, 9) << endl;
  95.  
  96. system("PAUSE"); return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment