Removed redundant zeroing.
global double_multiplication section .text double_multiplication: mov rax,rdi mul rsi ret
- global double_multiplication
- section .text
- double_multiplication:
xor rdx,rdx- mov rax,rdi
- mul rsi
- ret
#include <stdio.h> #include <criterion/criterion.h> int double_multiplication(int a, int b); Test(add_test, should_multiply_integers) { cr_assert_eq(double_multiplication(1, 1), 1); cr_assert_eq(double_multiplication(2, 1), 2); cr_assert_eq(double_multiplication(3, 1), 3); cr_assert_eq(double_multiplication(1, 2), 2); cr_assert_eq(double_multiplication(3, 3), 9); }
- #include <stdio.h>
- #include <criterion/criterion.h>
- int double_multiplication(int a, int b);
Test(add_test, should_add_integers) {- Test(add_test, should_multiply_integers) {
- cr_assert_eq(double_multiplication(1, 1), 1);
- cr_assert_eq(double_multiplication(2, 1), 2);
- cr_assert_eq(double_multiplication(3, 1), 3);
- cr_assert_eq(double_multiplication(1, 2), 2);
- cr_assert_eq(double_multiplication(3, 3), 9);
- }
Macro on conditional moves (only one here though).
It works in theory (probably will fail if %2 is rax).
; A macro with two parameters ; Implements actual int max(int a, int b) %macro _max 2 mov rax, %1 cmp %2, %1 ; compare a ?= b cmovg rax, %2 ret %endmacro section .text global max ; declaring int max(int a, int b) max: _max rdi, rsi ; using inner _max macro ret
- ; A macro with two parameters
- ; Implements actual int max(int a, int b)
- %macro _max 2
- mov rax, %1
- cmp %2, %1 ; compare a ?= b
jle _lower ; jump to lower if a <= bjmp _greater ; jump to greater if a > b_lower:mov rax, %1 ; assgine rax to bjmp _end ; end program_greater:mov rax, %2 ; assgine rax to a_end:- cmovg rax, %2
- ret
- %endmacro
- section .text
- global max ; declaring int max(int a, int b)
- max:
- _max rdi, rsi ; using inner _max macro
- ret
#include <criterion/criterion.h> int max(int, int); Test(max, should_return_max) { cr_assert_eq(max(3, 2), 3); cr_assert_eq(max(2, 3), 3); cr_assert_eq(max(3, 3), 3); }
- #include <criterion/criterion.h>
- int max(int, int);
- Test(max, should_return_max) {
- cr_assert_eq(max(3, 2), 3);
- cr_assert_eq(max(2, 3), 3);
- cr_assert_eq(max(3, 3), 3);
- }
Made a branchless variant (but now there is a data dependency).
Extended tests; added transformation outputs.
section .text global changeCase changeCase: mov ax, di ; load default answer (unmodified character) to ax mov cx, di xor cx, 32 ; load modified answer (with changed case) to cx and di, 95 ; normalize value (bring everything to upper case (destroys non alpha characters, but it is irrelevant)) sub di, 65 cmp di, 26 ; check if it is an alphabetical symbol (in range 65-90,97-122) cmovb ax, cx ; if it is - replace the answer by the modified one (with changed case) ret
- section .text
- global changeCase
- changeCase:
mov ax,disub di,0x41 ; di -= 'A'cmp di,0x7a-0x41 ; 'z' - 'A'ja _sub di,0x5a-0x41 ; di -= 'Z' - 'A'cmp di,0x61-0x5a ; 'a' - 'Z'jb _xor ax,0x20_:- mov ax, di ; load default answer (unmodified character) to ax
- mov cx, di
- xor cx, 32 ; load modified answer (with changed case) to cx
- and di, 95 ; normalize value (bring everything to upper case (destroys non alpha characters, but it is irrelevant))
- sub di, 65
- cmp di, 26 ; check if it is an alphabetical symbol (in range 65-90,97-122)
- cmovb ax, cx ; if it is - replace the answer by the modified one (with changed case)
- ret
#include <criterion/criterion.h> char changeCase(char letter); void do_one_test(char input, char expected) { char actual = changeCase(input); printf("'%c' -> '%c' should be '%c'\n", input, actual, expected); cr_assert_eq(actual, expected); } Test(changeCase, should_change_the_char_case) { char *input = " !\"$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^-`abcdefghijklmnopqrstuvwxyz{|}~", *expect = " !\"$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^-`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~"; while (*input) do_one_test(*input++, *expect++); }
- #include <criterion/criterion.h>
- char changeCase(char letter);
- void do_one_test(char input, char expected)
- {
- char actual = changeCase(input);
- printf("'%c' -> '%c' should be '%c'\n", input, actual, expected);
- cr_assert_eq(actual, expected);
- }
- Test(changeCase, should_change_the_char_case) {
cr_expect(changeCase('a') == 'A');cr_expect(changeCase('q') == 'Q');cr_expect(changeCase('T') == 't');cr_expect(changeCase('z') == 'Z');cr_expect(changeCase('@') == '@');cr_expect(changeCase('1') == '1');cr_expect(changeCase('^') == '^');cr_expect(changeCase('{') == '{');- char *input = " !\"$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^-`abcdefghijklmnopqrstuvwxyz{|}~",
- *expect = " !\"$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^-`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~";
- while (*input)
- do_one_test(*input++, *expect++);
- }
Fixed indentations, spacings.
Changed code a bit.
function binarySearch(numbers, element, left = 0, right = numbers.length-1) { // time complexity is log(n) where n is an array length; it excecutes way faster than a linear search. if(left > right) return -1; // get average and cast it to i32 // bit shift can be used depending on preference // let pivot = (left + right) >> 1; let pivot = (left + right) / 2 | 0; if(numbers[pivot] === element) return pivot; if(numbers[pivot] > element) return binarySearch(numbers, element, left, pivot-1); return binarySearch(numbers, element, pivot+1, right); }
function binarySearch(numbers, element, left = 0, right = numbers.length-1) {//time complexity is log(n) it excecutes very fast than linear search n=arraylength;if(left>right) return -1;- function binarySearch(numbers, element, left = 0, right = numbers.length-1)
- {
- // time complexity is log(n) where n is an array length; it excecutes way faster than a linear search.
- if(left > right) return -1;
let pivot =Math.floor((left+right)/2)- // get average and cast it to i32
- // bit shift can be used depending on preference
- // let pivot = (left + right) >> 1;
- let pivot = (left + right) / 2 | 0;
if(numbers[pivot]===element) return pivotif(numbers[pivot]>element) return binarySearch(numbers,element,left,pivot-1)return binarySearch(numbers,element,pivot+1,right)}- if(numbers[pivot] === element) return pivot;
- if(numbers[pivot] > element) return binarySearch(numbers, element, left, pivot-1);
- return binarySearch(numbers, element, pivot+1, right);
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("return index of searching element", function() { assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 4), 3); assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 2), 1); assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 8), -1); assert.strictEqual(binarySearch([1, 2, 3, 4, 9, 10, 16, 52], 52), 7); }); });
- const chai = require("chai");
- const assert = chai.assert;
- describe("Solution", function() {
- it("return index of searching element", function() {
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 4), 3);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 2), 1);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 8), -1);
assert.strictEqual(binarySearch([1, 2, 3, 4, 9, 10, 16, 52], 52), 7);- assert.strictEqual(binarySearch([1, 2, 3, 4, 9, 10, 16, 52], 52), 7);
- });