Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define INT_TYPE 8
- #define FLOAT_TYPE -8
- struct ValueAndType
- {
- unsigned long long value;
- unsigned long long another_value;
- long long type;
- };
- void
- my_test(const char* s, int arg1, int arg2, char arg3, short arg4, int arg5, int arg6)
- {
- puts(s);
- printf("%d\n", arg1);
- printf("%d\n", arg2);
- printf("%c\n", arg3);
- printf("%d\n", arg4);
- printf("%d\n", arg5);
- printf("%d\n", arg6);
- }
- int
- test_double(const char* s, double arg1, double arg2, double arg3, double arg4, double arg5, double arg6, double arg7, double arg8)
- {
- //puts(s);
- printf("%lf\n", arg1);
- /*printf("%lf\n", arg2);
- printf("%lf\n", arg3);
- printf("%lf\n", arg4);
- printf("%lf\n", arg5);
- printf("%lf\n", arg6);
- printf("%lf\n", arg7);
- printf("%lf\n", arg8);
- */
- return arg1 + arg8;
- }
- int
- old_main()
- {
- unsigned long long rax_value = 0;
- //unsigned long long args[] = { (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 48 };
- //unsigned long long args[] = { (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %lf\n", 1, 2, 3, 4, 5, (unsigned long long)((double)5.5) };
- //unsigned long long args[] = { (unsigned long long)my_test, (unsigned long long)"Hi! %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 48 };
- //unsigned long long args[] = { (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d 1: %lf 2: %lf 3: %lf 4: %lf 5: %lf 6: %lf 7: %lf 8: %lf %lf %lf %lf %lf\n", 1, 2, 3, 4, 5, (unsigned long long)((double)5.5) };
- //unsigned long long args[] = { (unsigned long long)test_double, (unsigned long long)"Hi! %d %d %d %d %d 1: %lf 2: %lf 3: %lf 4: %lf 5: %lf 6: %lf 7: %lf 8: %lf %lf %lf %lf %lf\n", 1, 2, 3, 4, 5, (unsigned long long)((double)5.5) };
- //unsigned long long args[] = { 0, (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 48 };
- //unsigned long long args[] = { 0, (unsigned long long)printf, (unsigned long long)"Hi! %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 100, 77, 75, 48, 56, 64, 12, 75, 77 };
- //unsigned long long args[] = { 0, (unsigned long long)printf, (unsigned long long)"Hi! %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", 1, 2, 3, 4, 5, 100, 77, 75, 48, 56, 64, 12, 75, 77 };
- unsigned long long args_row[] = { 4684295950315513862, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
- 0, (unsigned long long)printf, (unsigned long long)"Hi! double = %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 111, 100, 77, 75, 48, 56, 64, 12, 75, 77 };
- unsigned long long* args = args_row + 2 * 8 + 1;
- long long additional_args_quantity = sizeof(args_row) / sizeof(*args_row) - 2 * 8 - 1 - 1 - 1 - 6;
- if (additional_args_quantity < 0) {
- additional_args_quantity = 0;
- }
- args[0] = additional_args_quantity; // Количество дополнительных аргументов, которые пойдут на стек
- printf("args[0] = %llu\n", args[0]);
- asm volatile(
- "movsd -136(%%rax), %%xmm0;"
- "movsd -120(%%rax), %%xmm1;"
- "movsd -104(%%rax), %%xmm2;"
- "movsd -88(%%rax), %%xmm3;"
- "movsd -72(%%rax), %%xmm4;"
- "movsd -56(%%rax), %%xmm5;"
- "movsd -40(%%rax), %%xmm6;"
- "movsd -24(%%rax), %%xmm7;"
- "mov $0, %%rdi;"
- //"pushq %%rax;"
- "for_begin_label:"
- "cmpq %%rdi, (%%rax);"
- "jna for_end_label;"
- "pushq 64(%%rax, %%rdi, 8);"
- //"pushq $42;"
- "add $1, %%rdi;"
- "jmp for_begin_label;"
- "for_end_label:"
- //"mov %%rsp, %%rsi;"
- "mov 16(%%rax), %%rdi;"
- "movq 24(%%rax), %%rsi;"
- "movq 32(%%rax), %%rdx;"
- "movq 40(%%rax), %%rcx;"
- "movq 48(%%rax), %%r8;"
- "movq 56(%%rax), %%r9;"
- "movq %%rax, %%rbx;"
- "addq $8, %%rbx;"
- "mov -8(%%rax), %%rax;"
- /*"notq %%mm0;"*/
- /*"pcmpeqd %%xmm0, %%xmm0;"*/
- "call *(%%rbx);"
- "subq $8, %%rbx;"
- "movq (%%rbx), %%rbx;"
- "shlq $3, %%rbx;"
- "add %%rbx, %%rsp;"
- //"popq %%rbx;"
- : "=a"(rax_value)
- : "a"(args)
- : "rbx"
- );
- printf("Out: %lld\n", rax_value);
- return 0;
- }
- unsigned long long*
- argswt2args(unsigned long long func_address, struct ValueAndType* args_with_types, size_t len)
- {
- int int_type_counter = 0;
- int float_type_counter = 0;
- for (int i = 0; i < len; i++) {
- if (args_with_types[i].type > 0) {
- int_type_counter += 1;
- } else {
- float_type_counter += 1;
- printf("%d ", i);
- }
- }
- puts("");
- int on_stack_int_type_counter = (int_type_counter - 6 >= 0) ? int_type_counter - 6 : 0;
- int on_stack_float_type_counter = (float_type_counter - 8 >= 0) ? float_type_counter - 8 : 0;
- int on_stack_alignment = (on_stack_int_type_counter + on_stack_float_type_counter) % 2;
- /*
- printf("int_type_counter = %d \n", int_type_counter);
- printf("float_type_counter = %d \n", float_type_counter);
- printf("on_stack_int_type_counter = %d \n", on_stack_int_type_counter);
- printf("on_stack_float_type_counter = %d \n", on_stack_float_type_counter);
- */
- size_t total_size = 2 * 8 + 1 + 1 + 1 + 6 + on_stack_alignment + on_stack_int_type_counter + on_stack_float_type_counter;
- unsigned long long* result_row = malloc(total_size * (sizeof(*result_row)));
- unsigned long long* result = result_row + 2 * 8 + 1;
- result[-1] = float_type_counter;
- result[0] = on_stack_int_type_counter + on_stack_float_type_counter + on_stack_alignment;
- result[1] = func_address;
- int current_int_type_counter = 0;
- int current_float_type_counter = 0;
- int current_stack_position = total_size - 2 * 8 - 1 - 1;
- for (int i = 0; i < len; i++) {
- if (args_with_types[i].type > 0) {
- if (current_int_type_counter < 6) {
- result[2 + current_int_type_counter] = args_with_types[i].value;
- } else {
- result[current_stack_position] = args_with_types[i].value;
- current_stack_position -= 1;
- }
- current_int_type_counter += 1;
- } else {
- if (current_float_type_counter < 8) {
- result_row[2 * current_float_type_counter] = args_with_types[i].value;
- } else {
- result[current_stack_position] = args_with_types[i].value;
- current_stack_position -= 1;
- }
- current_float_type_counter += 1;
- }
- }
- return result;
- }
- int
- main()
- {
- struct ValueAndType args_with_types[] = {
- { (unsigned long long)"Shalom! %d %d %d %d %d %d %d %d %d %d\n", 0, INT_TYPE },
- { 1, 0, INT_TYPE },
- { 2, 0, INT_TYPE },
- { 3, 0, INT_TYPE },
- { 4, 0, INT_TYPE },
- { 5, 0, INT_TYPE },
- { 6, 0, INT_TYPE },
- { 7, 0, INT_TYPE },
- { 8, 0, INT_TYPE },
- { 9, 0, INT_TYPE },
- { 10, 0, INT_TYPE },
- { 111, 0, FLOAT_TYPE },
- { 112, 0, FLOAT_TYPE },
- { 113, 0, FLOAT_TYPE },
- { 114, 0, FLOAT_TYPE },
- { 115, 0, FLOAT_TYPE },
- { 116, 0, FLOAT_TYPE },
- { 117, 0, FLOAT_TYPE },
- { 118, 0, FLOAT_TYPE },
- { 119, 0, FLOAT_TYPE }
- };
- size_t len = sizeof(args_with_types) / sizeof(*args_with_types);
- unsigned long long* result = argswt2args((unsigned long long)printf, args_with_types, len);
- int result_len = 2 * 8 + 1 + 1 + 1 + 6 + result[0] + result[0] % 2;
- result -= 2 * 8 + 1;
- for (int i = 0; i < result_len; i++) {
- printf("%lld ", result[i]);
- }
- puts("");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement