Await and async in C#

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="156.618" Width="310.294">
    <Grid>
        <Button Content="Click Here" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="272" Click="Button_Click"/>
        <Label Name="label" Content="Thinking!!!!!!!" HorizontalAlignment="Left" Margin="12,45,0,0" VerticalAlignment="Top" Visibility="Collapsed"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BigInteger factorial(int n)
        {
            BigInteger b = 1;
            for (int i = n; i > 1; i--)
                b *= i;
            return b;
        }

        BigInteger combination(int n, int r)
        {
            BigInteger top = factorial(n);
            BigInteger bottom = factorial(n - r);
            return top / (factorial(r) * bottom);
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        async void clicked()
        {
            await calculate();
        }

        Task calculate()
        {
            return Task.Run(() =>
                {
                    for (int i = 1; i <= 1000; i++)
                    {
                        BigInteger b = combination(1000, i);
                    }

                    Dispatcher.Invoke(() =>
                        {
                            label.Visibility = System.Windows.Visibility.Collapsed;
                        });
                });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            label.Visibility = System.Windows.Visibility.Visible;
            clicked();
        }
    }
}

Mouseover and press trigger in WPF

MainWindow.xaml

<Window x:Class="ButtonColor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="ButtonStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Border Name="Border"
                                BorderThickness="2"
                                CornerRadius="3" 
                                BorderBrush="Gray" 
                                Background="{TemplateBinding Background}" />

                            <ContentPresenter 
                                x:Name="contentPresenter"
                                Content="{TemplateBinding Content}" 
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="Magenta"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="Yellow" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Content="Click" 
                HorizontalAlignment="Left" 
                Margin="85,80,0,0" 
                VerticalAlignment="Top" 
                Width="215" 
                Height="35"
                Background="Aqua"
                Style="{StaticResource ButtonStyle}" />
    </Grid>
</Window>

Outputing text using interrupts

boot.asm

[ORG 0x7c00]
 
jmp main
 
msg   db 'The itsy bitsy spider climbed up the waterspout. Down came the rain and washed the spider out. Out came the sun and dried up all the rain and the itsy bitsy spider climbed up the spout again.',0x0

; reference
; http://en.wikipedia.org/wiki/INT_10H
main:
    mov     ah, 0xe			; 0xe is used for teletype output
    mov     edx, 0			; edx is our string offset
    ch_loop:
    mov     al, [msg + edx] ; copy character into al from string offset
    int     0x10			; call interrupt 0x10 to output character
    inc     edx				; increment edx (move to next character)
    test    al, al			; test for 0
    jnz     ch_loop			; if not 0, go to ch_loop
     
    hlt
     
    times 510-($-$$) db 0
    db 0x55
    db 0xAA

nasm -f bin -o boot.img boot.asm

Output text using video memory

boot.asm

[ORG 0x7c00]
 
jmp main
 
msg   db 'The itsy bitsy spider climbed up the waterspout. Down came the rain and washed the spider out. Out came the sun and dried up all the rain and the itsy bitsy spider climbed up the spout again.',0x0
 
main:
    mov     ax, 0xb800			; 0xb800 is starting address to Color Video Memory
    mov     es, ax				; copy 0xb800 to es register
    mov     di, 0				; set di to 0
    
	; Now we have [es:di] => 0xb800:0x0
    mov     bx, 0				; bx is going to be our string offset
    loop:
	; must be aligned as the following
	; byte 1: character
	; byte 2: the color of character
    movzx   cx, byte[msg + bx]	; copy character from msg plus offset into cx
								; NOTE: must specify to copy a byte
    mov     ax, cx				; copy into character (in cx) into ax
    stosb						; copies at AL into [ES:DI]
    mov     ax, 5				; 5 is the color Magenta
    stosb						; copies at AL into [ES:DI]
    inc     bx					; increment bx (move to next character)
    test    cx, cx				; test for 0
    jnz     loop				; if not zero, go to loop
	
    hlt
	
    times 510-($-$$) db 0
    db 0x55
    db 0xAA

nasm -f bin -o boot.img boot.asm

Calling assembly code in C

power.asm

global power

section .text
	power:
	; initialize eax to 1
	mov		eax, 1
	; esp+8 is the exponent
	mov		ebx, [esp+8]
	loop:
	; esp+4 is the base
	imul	eax, [esp+4]
	dec		ebx
	cmp		ebx, 0
	jnz		loop
	ret

math.c

#include <stdio.h>

int power(int a, int b);

int main()
{
	int p = power(32,3);
	printf("%i\n", p);
}

https://github.com/randcode-generator/assembly_and_c

To compile and run:
nasm -f elf power.asm && gcc math.c power.o -o math && ./math

Arrays in assembly

global main
 
extern printf
 
section .data
	array:			db 0xF204, 0x64
	array2:			dw 0x1260, 0x62
	array3:			dw 0x54, 0x32
	arraydb:		db 10, 33, 88, 99
	arraydw:		dw 77, 460, 120, 550
	fmtStr: 		db '%i',0xA,0

section .text
	main:

	; Read a byte from array
	; Note: 0xF204 is truncated to
	; 0x04 because array is declared
	; as byte (db)
	movzx	edx, byte[array]
	call 	printout

	; Read a word from array
	; Note: it will be interpreted as 0x6404
	; because it's stored backwards
	movzx	edx, word[array]
	call 	printout

	; Read a word from array2
	movzx	edx, word[array2]
	call 	printout

	; Read a word from array2
	; with 1 byte offset
	; Note: it will be interpreted as 0x6212
	movzx	edx, word[array2+1]
	call 	printout

	; Read a word from array3
	; with 1 byte offset
	; Note: it will be interpreted as 0x3200
	movzx	edx, word[array3+1]
	call 	printout

	; Loop through byte by byte arraydb
	mov 	ebx, 0
	loop:
	movzx	edx, byte[arraydb+ebx]
	call 	printout
	inc 	ebx
	cmp		ebx, 4
	jne		loop

	; Loop through word by word arraydb
	mov 	ebx, 0
	loop2:
	; Size of word is 2 byte, increment
	; by 2
	movzx	edx, word[arraydw+(ebx*2)]
	call 	printout
	inc 	ebx
	cmp		ebx, 4
	jne		loop2

	ret

	printout:
	; print out the argument
    sub     esp, 4
    mov     [esp], edx          ; Copy edx into address of esp
    ; Load the format string into the stack
    sub     esp, 4              ; Allocate space on the stack for one 4 byte parameter
    lea     eax, [fmtStr]       ; Load string into eax
    mov     [esp], eax          ; Copy eax into the address of esp
 
    ; Call printf
    call    printf              ; Call printf(3):
                     
	add     esp, 8				; pop stack by 8 (2 elements * 4 bytes each element)
	ret

https://github.com/randcode-generator/array-in-assembly

To compile and run:
nasm -f elf32 array.asm && gcc -o array array.o && ./array

Proc in Ruby

fullname = Proc.new { |fname, lname| "First Name: #{fname}\nLast Name: #{lname}" }
puts fullname.call("John", "Smith")

Interface and lambda in Java

public class name {
    interface IName {
        void fullName(String a, String b);
    }
    public static void main(String[] args) {
        IName g = (a, b) -> System.out.println(a + " " + b);
        g.fullName("John", "Smith");
    }
}

More Events in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eventhandler
{
    class myClass
    {
        public string message { get; set; }
    }

    class NormalEvent
    {
        public NormalEvent()
        {
            _show += event1;
            _show += event2;
            _show.Invoke(null, new myClass() { message = "Normal Event Invoked!" });

            Console.WriteLine("\nRemoved event 1");
            _show -= event1;
            _show.Invoke(null, new myClass() { message = "Noraml Event Invoked! After removed!" });
        }

        public event EventHandler<myClass> _show;

        void event1(object sender, myClass e)
        {
            Console.WriteLine("event1");
            Console.WriteLine("Message: " + e.message);
        }

        void event2(object sender, myClass e)
        {
            Console.WriteLine("event2");
            Console.WriteLine("Message: " + e.message);
        }
    }

    class WithLogging
    {
        public WithLogging()
        {
            _show += event1;
            _show += event2;
            events.Invoke(null, new myClass() { message = "With Logging Event Invoked!" });

            Console.WriteLine("\nRemoved event 1");
            _show -= event1;
            events.Invoke(null, new myClass() { message = "With Logging Event Invoked! After removed!" });
        }

        EventHandler<myClass> events;
        public event EventHandler<myClass> _show
        {
            add
            {
                Console.WriteLine("-->adding");
                events += value;
            }

            remove
            {
                Console.WriteLine("-->removing");
                events -= value;
            }
        }

        void event1(object sender, myClass e)
        {
            Console.WriteLine("event1");
            Console.WriteLine("Message: " + e.message);
        }

        void event2(object sender, myClass e)
        {
            Console.WriteLine("event2");
            Console.WriteLine("Message: " + e.message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("--Normal Event--");
            new NormalEvent();
            Console.WriteLine("--Events With Logging--");
            new WithLogging();
        }
    }
}
Posted in C#