Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. asmIff.c:
  2.  
  3. #include <stdio.h>
  4.  
  5. int test(int a) {
  6. if ( a == 1 ) {
  7. return 1;
  8. } else if ( a > 1 ) {
  9. return 2;
  10. } else {
  11. return 0;
  12. }
  13. }
  14.  
  15. int main(int argc, char **argv) {
  16.  
  17. int i, j, t;
  18. i = argc;
  19.  
  20. t = test(i);
  21.  
  22. printf("%d\n", t);
  23. }
  24.  
  25. asmSwi.c:
  26.  
  27. #include <stdio.h>
  28.  
  29. int test(int a) {
  30. switch (a) {
  31. case 1:
  32. return 0;
  33. case 2:
  34. return 1;
  35. default:
  36. return 2;
  37. }
  38. }
  39.  
  40. int main(int argc, char **argv) {
  41.  
  42. int i, t;
  43. i = argc;
  44.  
  45. t = test(i);
  46.  
  47. printf("%d\n", t);
  48. }
  49.  
  50. asmIff.s:
  51.  
  52. .section __TEXT,__text,regular,pure_instructions
  53. .macosx_version_min 10, 12
  54. .globl _test
  55. .p2align 4, 0x90
  56. _test: ## @test
  57. .cfi_startproc
  58. ## BB#0:
  59. pushq %rbp
  60. Lcfi0:
  61. .cfi_def_cfa_offset 16
  62. Lcfi1:
  63. .cfi_offset %rbp, -16
  64. movq %rsp, %rbp
  65. Lcfi2:
  66. .cfi_def_cfa_register %rbp
  67. movl %edi, -8(%rbp)
  68. cmpl $1, -8(%rbp)
  69. jne LBB0_2
  70. ## BB#1:
  71. movl $0, -4(%rbp)
  72. jmp LBB0_5
  73. LBB0_2:
  74. cmpl $2, -8(%rbp)
  75. jne LBB0_4
  76. ## BB#3:
  77. movl $1, -4(%rbp)
  78. jmp LBB0_5
  79. LBB0_4:
  80. movl $2, -4(%rbp)
  81. LBB0_5:
  82. movl -4(%rbp), %eax
  83. popq %rbp
  84. retq
  85. .cfi_endproc
  86.  
  87. .globl _main
  88. .p2align 4, 0x90
  89. _main: ## @main
  90. .cfi_startproc
  91. ## BB#0:
  92. pushq %rbp
  93. Lcfi3:
  94. .cfi_def_cfa_offset 16
  95. Lcfi4:
  96. .cfi_offset %rbp, -16
  97. movq %rsp, %rbp
  98. Lcfi5:
  99. .cfi_def_cfa_register %rbp
  100. subq $32, %rsp
  101. movl %edi, -4(%rbp)
  102. movq %rsi, -16(%rbp)
  103. movl -4(%rbp), %edi
  104. movl %edi, -20(%rbp)
  105. movl -20(%rbp), %edi
  106. callq _test
  107. leaq L_.str(%rip), %rdi
  108. movl %eax, -28(%rbp)
  109. movl -28(%rbp), %esi
  110. movb $0, %al
  111. callq _printf
  112. xorl %esi, %esi
  113. movl %eax, -32(%rbp) ## 4-byte Spill
  114. movl %esi, %eax
  115. addq $32, %rsp
  116. popq %rbp
  117. retq
  118. .cfi_endproc
  119.  
  120. .section __TEXT,__cstring,cstring_literals
  121. L_.str: ## @.str
  122. .asciz "%d\n"
  123.  
  124.  
  125. .subsections_via_symbols
  126.  
  127. asmSwi.s:
  128.  
  129. .section __TEXT,__text,regular,pure_instructions
  130. .macosx_version_min 10, 12
  131. .globl _test
  132. .p2align 4, 0x90
  133. _test: ## @test
  134. .cfi_startproc
  135. ## BB#0:
  136. pushq %rbp
  137. Lcfi0:
  138. .cfi_def_cfa_offset 16
  139. Lcfi1:
  140. .cfi_offset %rbp, -16
  141. movq %rsp, %rbp
  142. Lcfi2:
  143. .cfi_def_cfa_register %rbp
  144. movl %edi, -8(%rbp)
  145. movl -8(%rbp), %edi
  146. movl %edi, %eax
  147. subl $1, %eax
  148. movl %edi, -12(%rbp) ## 4-byte Spill
  149. movl %eax, -16(%rbp) ## 4-byte Spill
  150. je LBB0_1
  151. jmp LBB0_5
  152. LBB0_5:
  153. movl -12(%rbp), %eax ## 4-byte Reload
  154. subl $2, %eax
  155. movl %eax, -20(%rbp) ## 4-byte Spill
  156. je LBB0_2
  157. jmp LBB0_3
  158. LBB0_1:
  159. movl $0, -4(%rbp)
  160. jmp LBB0_4
  161. LBB0_2:
  162. movl $1, -4(%rbp)
  163. jmp LBB0_4
  164. LBB0_3:
  165. movl $2, -4(%rbp)
  166. LBB0_4:
  167. movl -4(%rbp), %eax
  168. popq %rbp
  169. retq
  170. .cfi_endproc
  171.  
  172. .globl _main
  173. .p2align 4, 0x90
  174. _main: ## @main
  175. .cfi_startproc
  176. ## BB#0:
  177. pushq %rbp
  178. Lcfi3:
  179. .cfi_def_cfa_offset 16
  180. Lcfi4:
  181. .cfi_offset %rbp, -16
  182. movq %rsp, %rbp
  183. Lcfi5:
  184. .cfi_def_cfa_register %rbp
  185. subq $32, %rsp
  186. movl %edi, -4(%rbp)
  187. movq %rsi, -16(%rbp)
  188. movl -4(%rbp), %edi
  189. movl %edi, -20(%rbp)
  190. movl -20(%rbp), %edi
  191. callq _test
  192. leaq L_.str(%rip), %rdi
  193. movl %eax, -24(%rbp)
  194. movl -24(%rbp), %esi
  195. movb $0, %al
  196. callq _printf
  197. xorl %esi, %esi
  198. movl %eax, -28(%rbp) ## 4-byte Spill
  199. movl %esi, %eax
  200. addq $32, %rsp
  201. popq %rbp
  202. retq
  203. .cfi_endproc
  204.  
  205. .section __TEXT,__cstring,cstring_literals
  206. L_.str: ## @.str
  207. .asciz "%d\n"
  208.  
  209.  
  210. .subsections_via_symbols
  211.  
  212. Executions comparing the output of the two programs (to prove that they are the same:
  213.  
  214. R5057499:C rpn01$ ./asmIff
  215. 0
  216. R5057499:C rpn01$ ./asmIff one
  217. 1
  218. R5057499:C rpn01$ ./asmIff one two
  219. 2
  220. R5057499:C rpn01$ ./asmIff one two three
  221. 2
  222. R5057499:C rpn01$ ./asmSwi
  223. 0
  224. R5057499:C rpn01$ ./asmSwi one
  225. 1
  226. R5057499:C rpn01$ ./asmSwi one two
  227. 2
  228. R5057499:C rpn01$ ./asmSwi one two three
  229. 2
  230. R5057499:C rpn01$
  231.  
  232. Comparing the number of source lines, both in C and S:
  233.  
  234. R5057499:C rpn01$ diff <(wc -l asmIff.c) <(wc -l asmSwi.c)
  235. 1c1
  236. < 21 asmIff.c
  237. ---
  238. > 22 asmSwi.c
  239. R5057499:C rpn01$ diff <(wc -l asmIff.s) <(wc -l asmSwi.s)
  240. 1c1
  241. < 74 asmIff.s
  242. ---
  243. > 82 asmSwi.s
  244. R5057499:C rpn01$
  245.  
  246. Executing each program 10,000 times via the time command to compare the times yields no significant win for either:
  247.  
  248. R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmIff one > /dev/null ; done )
  249.  
  250. real 0m30.988s
  251. user 0m8.056s
  252. sys 0m8.469s
  253. R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmSwi one > /dev/null ; done )
  254.  
  255. real 0m30.584s
  256. user 0m7.965s
  257. sys 0m8.310s
  258. R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmIff one > /dev/null ; done )
  259.  
  260. real 0m30.650s
  261. user 0m7.977s
  262. sys 0m8.337s
  263. R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmSwi one > /dev/null ; done )
  264.  
  265. real 0m30.777s
  266. user 0m7.979s
  267. sys 0m8.363s
  268. R5057499:C rpn01$
Add Comment
Please, Sign In to add comment