Ad
Fundamentals
Code
Diff
  • 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
Numbers
Data Types
Integers
Algorithms
Logic

Determine the number of digits in a chosen base using an iterator

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