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)
#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
#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";}}
// TODO: Replace examples and use TDD by writing your own tests Describe(iterating) { It(should_iterate_both) { //iterateTwoVectors<int, char>({1, 'a', 3}, {'a', 'b'}); iterateMultipleVectors(0, 0, std::vector{'a','b', 's'},std::vector{2,3},std::vector{"string", "asb", "sad"}, std::vector{'a', 's', 'x'}, std::vector{15.0,13.2, 1.1}); } };
- // TODO: Replace examples and use TDD by writing your own tests
- Describe(iterating)
- {
- It(should_iterate_both)
- {
iterateTwoVectors<int, char>({1, 'a', 3}, {'a', 'b'});- //iterateTwoVectors<int, char>({1, 'a', 3}, {'a', 'b'});
- iterateMultipleVectors(0, 0, std::vector{'a','b', 's'},std::vector{2,3},std::vector{"string", "asb", "sad"}, std::vector{'a', 's', 'x'}, std::vector{15.0,13.2, 1.1});
- }
- };
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";
}
}
Describe(enumerating)
{
It(should_enumerate)
{
enumerate<int>({1, 5, 'a', 32});
}
};
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";
}
}
// TODO: Replace examples and use TDD by writing your own tests
Describe(iterating)
{
It(should_iterate_both)
{
iterateTwoVectors<int, char>({1, 'a', 3}, {'a', 'b'});
}
};
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
#include <criterion/criterion.h>
int max(int, int);
Test(max, should_return_max) {
cr_assert_eq(max(3, 2), 3);
}
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
#include <criterion/criterion.h>
int max(int, int);
Test(max, should_return_max) {
cr_assert_eq(max(1, 2), 2);
cr_assert_eq(max(3, 2), 3);
}