Fundamentals
extern printf SECTION .text global hello hello: xor rax, rax mov rdi, msg mov rsi, nasm call printf mov rax, 1 ret SECTION .data msg: db "Hello %s", 10, 0 nasm: db "NASM", 0
#include <iostream>#include <string>- extern printf
int helloCplusplus(){std::string str = "Hello, C++!";std::cout << str << '\n';return 0;}- SECTION .text
- global hello
- hello:
- xor rax, rax
- mov rdi, msg
- mov rsi, nasm
- call printf
- mov rax, 1
- ret
- SECTION .data
- msg: db "Hello %s", 10, 0
- nasm: db "NASM", 0
#include <criterion/criterion.h> int hello(); Test(test, test1){ cr_assert(hello() == 1); }
// TODO: TDD development by writing your own tests as you solve the kata- #include <criterion/criterion.h>
- int hello();
Describe(Group_C_plus_plus){It(test_proper_exit){Assert::That(helloCplusplus(), Equals(0));}};- Test(test, test1){
- cr_assert(hello() == 1);
- }
Numbers
Data Types
Integers
Algorithms
Logic
Determine the number of digits in a chosen base using an iterator
fn digits(n: u64, base: u64) -> usize { DigitIterator::new(n, base).count() } struct DigitIterator{ value: u64, base: u64, is_0: bool, first_iter: bool } impl DigitIterator{ pub fn new(value: u64, base: u64) -> Self{ Self{ value, base, is_0: value == 0, first_iter: true, } } } impl Iterator for DigitIterator{ type Item = u64; fn next(&mut self) -> Option<u64>{ if self.is_0 { if self.first_iter{ self.first_iter = false; Some(0) }else{ None } }else{ let old = self.value; self.value /= self.base; if old != 0{ Some(old % self.base) }else{ None } } } }
fn digits(mut n: u64) -> usize {let mut l = 1;while n >= 10 {n /= 10;l += 1;- fn digits(n: u64, base: u64) -> usize {
- DigitIterator::new(n, base).count()
- }
- struct DigitIterator{
- value: u64,
- base: u64,
- is_0: bool,
- first_iter: bool
- }
- impl DigitIterator{
- pub fn new(value: u64, base: u64) -> Self{
- Self{
- value,
- base,
- is_0: value == 0,
- first_iter: true,
- }
- }
- }
- impl Iterator for DigitIterator{
- type Item = u64;
- fn next(&mut self) -> Option<u64>{
- if self.is_0 {
- if self.first_iter{
- self.first_iter = false;
- Some(0)
- }else{
- None
- }
- }else{
- let old = self.value;
- self.value /= self.base;
- if old != 0{
- Some(old % self.base)
- }else{
- None
- }
- }
- }
l- }
#[test] fn pow10() { assert_eq!(digits(0, 10), 1); for i in 0..20 { assert_eq!(digits(POW10[i], 10), i+1); } assert_eq!(digits(std::u64::MAX, 10), 20); } #[test] fn pow10_minus_1() { for i in 1..20 { assert_eq!(digits(POW10[i] - 1, 10), i); } } #[test] fn pow10_half() { for i in 1..20 { assert_eq!(digits(POW10[i] / 2, 10), i); } } #[test] fn base2(){ for i in 1..20{ assert_eq!(digits(2u64.pow(i), 2), (i+1) as usize); } } #[test] fn base16(){ for i in 1..10{ assert_eq!(digits(16u64.pow(i), 16), (i+1) as usize); } }
- #[test]
- fn pow10() {
assert_eq!(digits(0), 1);- assert_eq!(digits(0, 10), 1);
- for i in 0..20 {
assert_eq!(digits(POW10[i]), i+1);- assert_eq!(digits(POW10[i], 10), i+1);
- }
assert_eq!(digits(std::u64::MAX), 20);- assert_eq!(digits(std::u64::MAX, 10), 20);
- }
- #[test]
- fn pow10_minus_1() {
- for i in 1..20 {
assert_eq!(digits(POW10[i] - 1), i);- assert_eq!(digits(POW10[i] - 1, 10), i);
- }
- }
- #[test]
- fn pow10_half() {
- for i in 1..20 {
assert_eq!(digits(POW10[i] / 2), i);- assert_eq!(digits(POW10[i] / 2, 10), i);
- }
- }
- #[test]
- fn base2(){
- for i in 1..20{
- assert_eq!(digits(2u64.pow(i), 2), (i+1) as usize);
- }
- }
- #[test]
- fn base16(){
- for i in 1..10{
- assert_eq!(digits(16u64.pow(i), 16), (i+1) as usize);
- }
- }