Ad
Code
Diff
  • section .text
    global max ; declare max function
    max:            ; long max(long a, long b)
      cmp rdi, rsi ; compare a ?= b
      jle _lower  ; jump to lower if a <= b
      jmp _greater ; jump to greater if a > b
      _lower:
        mov rax, rsi ; assgine rax to b
        jmp _end ; end program
      _greater:
        mov rax, rdi ; assgine rax to a
      _end:
      ret
    • section .text
    • global max ; declare max function
    • max: ; int max(int a, int b)
    • max: ; long max(long a, long b)
    • cmp rdi, rsi ; compare a ?= b
    • jle _lower ; jump to lower if a <= b
    • jmp _greater ; jump to greater if a > b
    • _lower:
    • mov rax, rsi ; assgine rax to b
    • jmp _end ; end program
    • _greater:
    • mov rax, rdi ; assgine rax to a
    • _end:
    • ret

C++ : Iterating over two vectors at the same time till end of one
(with pair instead of tuple)

Code
Diff
  • #include <vector>
    #include <utility>
    #include <iostream>
    template <class T, class U>
    void iterateTwoVectors(std::vector<T> A, std::vector<U> B)
    {
    
       for (auto [a,b] = std::pair{A.begin(), B.begin()}; a != A.end() && b != B.end(); a++, b++) 
       { 
        std::cout << *a << ":" << *b << "\n";
       }
    }
    • #include <vector>
    • #include <tuple>
    • #include <utility>
    • #include <iostream>
    • template <class T, class U>
    • void iterateTwoVectors(std::vector<T> A, std::vector<U> B)
    • {
    • for (auto [a,b] = std::tuple{A.begin(), B.begin()}; a != A.end() && b != B.end(); a++, b++)
    • for (auto [a,b] = std::pair{A.begin(), B.begin()}; a != A.end() && b != B.end(); a++, b++)
    • {
    • std::cout << *a << ":" << *b << "\n";
    • }
    • }

C++ : Iterating over Undefinitive number of vectors of any type at the same time till end of first

Code
Diff
  • #include <vector>
    #include <iostream>
    
    template<class T>
    void iterateMultipleVectors(unsigned int vecNumber, typename std::vector<T>::size_type index, std::vector<T> vec)
    {
     std::cout <<", Vec"<< vecNumber << ":" << vec[index] << "\n";
    }
    
    
    template<class T, class ... Args>
    void iterateMultipleVectors(unsigned int vecNumber, typename std::vector<T>::size_type index, std::vector<T> vec, Args ... args)
    {
      if(vecNumber == 0)
      {
        for (;index < vec.size();++index)
        {
          std::cout << "Index " << vecNumber << ":";
          std::cout <<" Vec"<< vecNumber << ":" << vec[index];
          iterateMultipleVectors(++vecNumber, index, args...);
          --vecNumber;
        }
        return;
      }
      if(index < vec.size())
        std::cout <<", Vec"<< vecNumber << ":" << vec[index];
      else
        std::cout <<", Vec"<< vecNumber << ":has no more content";
      iterateMultipleVectors(++vecNumber, index, args...);
    }
    
    
    
    • #include <vector>
    • #include <tuple>
    • #include <iostream>
    • template <class T, class U>
    • void iterateTwoVectors(std::vector<T> A, std::vector<U> B)
    • template<class T>
    • void iterateMultipleVectors(unsigned int vecNumber, typename std::vector<T>::size_type index, std::vector<T> vec)
    • {
    • std::cout <<", Vec"<< vecNumber << ":" << vec[index] << "\n";
    • }
    • template<class T, class ... Args>
    • void iterateMultipleVectors(unsigned int vecNumber, typename std::vector<T>::size_type index, std::vector<T> vec, Args ... args)
    • {
    • if(vecNumber == 0)
    • {
    • for (;index < vec.size();++index)
    • {
    • std::cout << "Index " << vecNumber << ":";
    • std::cout <<" Vec"<< vecNumber << ":" << vec[index];
    • iterateMultipleVectors(++vecNumber, index, args...);
    • --vecNumber;
    • }
    • return;
    • }
    • if(index < vec.size())
    • std::cout <<", Vec"<< vecNumber << ":" << vec[index];
    • else
    • std::cout <<", Vec"<< vecNumber << ":has no more content";
    • iterateMultipleVectors(++vecNumber, index, args...);
    • }
    • for (auto [a,b] = std::tuple{A.begin(), B.begin()}; a != A.end() && b != B.end(); a++, b++)
    • {
    • std::cout << *a << ":" << *b << "\n";
    • }
    • }

enumerating over vector, equivelent to python's enumerate(list)

#include <vector>
#include <tuple>
#include <iostream>

template <class T>
void enumerate(std::vector<T> A)
{
    for(auto [a,i]=std::tuple{A.begin(), 0}; a < A.end(); a++, i++)
   { 
    std::cout << i << ":" << *a << "\n";
   }
}

Iterating over two vectors at the same time till end of one of them

#include <vector>
#include <tuple>
#include <iostream>
template <class T, class U>
void iterateTwoVectors(std::vector<T> A, std::vector<U> B)
{

   for (auto [a,b] = std::tuple{A.begin(), B.begin()}; a != A.end() && b != B.end(); a++, b++) 
   { 
    std::cout << *a << ":" << *b << "\n";
   }
}

Macro to get max of two integers
can be used as a new pseudo-command

; A macro with two parameters
; Implements actual int max(int a, int b)
%macro _max 2 
  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:
  ret
  
%endmacro

section .text
global max ; declaring int max(int a, int b)

max:            
  _max rdi, rsi ; using inner _max macro 
  ret

Get Max of two integers

section .text
global max ; declare max function
max:            ; int max(int a, int b)
  cmp rdi, rsi ; compare a ?= b
  jle _lower  ; jump to lower if a <= b
  jmp _greater ; jump to greater if a > b
  _lower:
    mov rax, rsi ; assgine rax to b
    jmp _end ; end program
  _greater:
    mov rax, rdi ; assgine rax to a
  _end:
  ret