Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- asmIff.c:
- #include <stdio.h>
- int test(int a) {
- if ( a == 1 ) {
- return 1;
- } else if ( a > 1 ) {
- return 2;
- } else {
- return 0;
- }
- }
- int main(int argc, char **argv) {
- int i, j, t;
- i = argc;
- t = test(i);
- printf("%d\n", t);
- }
- asmSwi.c:
- #include <stdio.h>
- int test(int a) {
- switch (a) {
- case 1:
- return 0;
- case 2:
- return 1;
- default:
- return 2;
- }
- }
- int main(int argc, char **argv) {
- int i, t;
- i = argc;
- t = test(i);
- printf("%d\n", t);
- }
- asmIff.s:
- .section __TEXT,__text,regular,pure_instructions
- .macosx_version_min 10, 12
- .globl _test
- .p2align 4, 0x90
- _test: ## @test
- .cfi_startproc
- ## BB#0:
- pushq %rbp
- Lcfi0:
- .cfi_def_cfa_offset 16
- Lcfi1:
- .cfi_offset %rbp, -16
- movq %rsp, %rbp
- Lcfi2:
- .cfi_def_cfa_register %rbp
- movl %edi, -8(%rbp)
- cmpl $1, -8(%rbp)
- jne LBB0_2
- ## BB#1:
- movl $0, -4(%rbp)
- jmp LBB0_5
- LBB0_2:
- cmpl $2, -8(%rbp)
- jne LBB0_4
- ## BB#3:
- movl $1, -4(%rbp)
- jmp LBB0_5
- LBB0_4:
- movl $2, -4(%rbp)
- LBB0_5:
- movl -4(%rbp), %eax
- popq %rbp
- retq
- .cfi_endproc
- .globl _main
- .p2align 4, 0x90
- _main: ## @main
- .cfi_startproc
- ## BB#0:
- pushq %rbp
- Lcfi3:
- .cfi_def_cfa_offset 16
- Lcfi4:
- .cfi_offset %rbp, -16
- movq %rsp, %rbp
- Lcfi5:
- .cfi_def_cfa_register %rbp
- subq $32, %rsp
- movl %edi, -4(%rbp)
- movq %rsi, -16(%rbp)
- movl -4(%rbp), %edi
- movl %edi, -20(%rbp)
- movl -20(%rbp), %edi
- callq _test
- leaq L_.str(%rip), %rdi
- movl %eax, -28(%rbp)
- movl -28(%rbp), %esi
- movb $0, %al
- callq _printf
- xorl %esi, %esi
- movl %eax, -32(%rbp) ## 4-byte Spill
- movl %esi, %eax
- addq $32, %rsp
- popq %rbp
- retq
- .cfi_endproc
- .section __TEXT,__cstring,cstring_literals
- L_.str: ## @.str
- .asciz "%d\n"
- .subsections_via_symbols
- asmSwi.s:
- .section __TEXT,__text,regular,pure_instructions
- .macosx_version_min 10, 12
- .globl _test
- .p2align 4, 0x90
- _test: ## @test
- .cfi_startproc
- ## BB#0:
- pushq %rbp
- Lcfi0:
- .cfi_def_cfa_offset 16
- Lcfi1:
- .cfi_offset %rbp, -16
- movq %rsp, %rbp
- Lcfi2:
- .cfi_def_cfa_register %rbp
- movl %edi, -8(%rbp)
- movl -8(%rbp), %edi
- movl %edi, %eax
- subl $1, %eax
- movl %edi, -12(%rbp) ## 4-byte Spill
- movl %eax, -16(%rbp) ## 4-byte Spill
- je LBB0_1
- jmp LBB0_5
- LBB0_5:
- movl -12(%rbp), %eax ## 4-byte Reload
- subl $2, %eax
- movl %eax, -20(%rbp) ## 4-byte Spill
- je LBB0_2
- jmp LBB0_3
- LBB0_1:
- movl $0, -4(%rbp)
- jmp LBB0_4
- LBB0_2:
- movl $1, -4(%rbp)
- jmp LBB0_4
- LBB0_3:
- movl $2, -4(%rbp)
- LBB0_4:
- movl -4(%rbp), %eax
- popq %rbp
- retq
- .cfi_endproc
- .globl _main
- .p2align 4, 0x90
- _main: ## @main
- .cfi_startproc
- ## BB#0:
- pushq %rbp
- Lcfi3:
- .cfi_def_cfa_offset 16
- Lcfi4:
- .cfi_offset %rbp, -16
- movq %rsp, %rbp
- Lcfi5:
- .cfi_def_cfa_register %rbp
- subq $32, %rsp
- movl %edi, -4(%rbp)
- movq %rsi, -16(%rbp)
- movl -4(%rbp), %edi
- movl %edi, -20(%rbp)
- movl -20(%rbp), %edi
- callq _test
- leaq L_.str(%rip), %rdi
- movl %eax, -24(%rbp)
- movl -24(%rbp), %esi
- movb $0, %al
- callq _printf
- xorl %esi, %esi
- movl %eax, -28(%rbp) ## 4-byte Spill
- movl %esi, %eax
- addq $32, %rsp
- popq %rbp
- retq
- .cfi_endproc
- .section __TEXT,__cstring,cstring_literals
- L_.str: ## @.str
- .asciz "%d\n"
- .subsections_via_symbols
- Executions comparing the output of the two programs (to prove that they are the same:
- R5057499:C rpn01$ ./asmIff
- 0
- R5057499:C rpn01$ ./asmIff one
- 1
- R5057499:C rpn01$ ./asmIff one two
- 2
- R5057499:C rpn01$ ./asmIff one two three
- 2
- R5057499:C rpn01$ ./asmSwi
- 0
- R5057499:C rpn01$ ./asmSwi one
- 1
- R5057499:C rpn01$ ./asmSwi one two
- 2
- R5057499:C rpn01$ ./asmSwi one two three
- 2
- R5057499:C rpn01$
- Comparing the number of source lines, both in C and S:
- R5057499:C rpn01$ diff <(wc -l asmIff.c) <(wc -l asmSwi.c)
- 1c1
- < 21 asmIff.c
- ---
- > 22 asmSwi.c
- R5057499:C rpn01$ diff <(wc -l asmIff.s) <(wc -l asmSwi.s)
- 1c1
- < 74 asmIff.s
- ---
- > 82 asmSwi.s
- R5057499:C rpn01$
- Executing each program 10,000 times via the time command to compare the times yields no significant win for either:
- R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmIff one > /dev/null ; done )
- real 0m30.988s
- user 0m8.056s
- sys 0m8.469s
- R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmSwi one > /dev/null ; done )
- real 0m30.584s
- user 0m7.965s
- sys 0m8.310s
- R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmIff one > /dev/null ; done )
- real 0m30.650s
- user 0m7.977s
- sys 0m8.337s
- R5057499:C rpn01$ time ( for (( i=1; i < 10000; i++ )) ; do ./asmSwi one > /dev/null ; done )
- real 0m30.777s
- user 0m7.979s
- sys 0m8.363s
- R5057499:C rpn01$
Add Comment
Please, Sign In to add comment