Move History

Fork Selected
  • Description

    Why use Python when we can use Assembly 😎.

    Code
    int multiply(int a, int b)
    {
      int res;
      __asm__
      (
        "imull      %1, %2     \n\t"
        "movl       %2, %0"
        : "=r" (res)
        : "r" (a), "r" (b)
      );
      return res;
    }
    Test Cases
    // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
    
    #include <criterion/criterion.h>
    
    // replace with the actual method being tested
    int multiply(int a, int b);
    
    Test(the_multiply_function, should_pass_all_the_tests_provided)
    {
        cr_assert_eq(multiply(1, 1), 1);
        cr_assert_eq(multiply(2, 2), 4);
        cr_assert_eq(multiply(3, 7), 21);
    }
  • Code
    • def multiply(a, b):
    • return a*b
    • int multiply(int a, int b)
    • {
    • int res;
    • __asm__
    • (
    • "imull %1, %2 \n\t"
    • "movl %2, %0"
    • : "=r" (res)
    • : "r" (a), "r" (b)
    • );
    • return res;
    • }
    Test Cases
    • import codewars_test as test
    • # TODO Write tests
    • import solution # or from solution import example
    • // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
    • # test.assert_equals(actual, expected, [optional] message)
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • test.assert_equals(multiply(1, 1), 1)
    • test.assert_equals(multiply(1, 1), 1)
    • test.assert_equals(multiply(2, 2), 4)
    • test.assert_equals(multiply(2, -2), -4)
    • test.assert_equals(multiply(-2, 2), -4)
    • test.assert_equals(multiply(-2, -2), 4)
    • test.assert_equals(multiply(3, 7), 21)
    • test.assert_equals(multiply(3, -7), -21)
    • test.assert_equals(multiply(-3, -7), 21)
    • #include <criterion/criterion.h>
    • // replace with the actual method being tested
    • int multiply(int a, int b);
    • Test(the_multiply_function, should_pass_all_the_tests_provided)
    • {
    • cr_assert_eq(multiply(1, 1), 1);
    • cr_assert_eq(multiply(2, 2), 4);
    • cr_assert_eq(multiply(3, 7), 21);
    • }