Ad

Removed redundant zeroing.

Code
Diff
  • 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

Macro on conditional moves (only one here though).

It works in theory (probably will fail if %2 is rax).

Code
Diff
  • ; 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 <= b
    • jmp _greater ; jump to greater if a > b
    • _lower:
    • mov rax, %1 ; assgine rax to b
    • jmp _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

Made a branchless variant (but now there is a data dependency).

Extended tests; added transformation outputs.

Code
Diff
  • 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,di
    • sub 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

Fixed indentations, spacings.

Changed code a bit.

Code
Diff
  • 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 pivot
    • if(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);
    • }