using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Actions
{
class Program
{
static void Main(string[] args)
{
Action<string, string> name = (fname, lname) => { Console.WriteLine("First Name: " + fname); Console.WriteLine("Last Name: " + lname); };
name("John", "Smith");
Console.WriteLine("Using invoke");
name.Invoke("Jane", "Kate");
Console.WriteLine();
Func<string, string, string> fullName = (fname, lname) => { return "First Name: " + fname + " Last Name: " + lname; };
Console.WriteLine(fullName("Will", "Smith"));
}
}
}
Branch in assembly
global main extern printf section .data strEqual: db 'edx is equal to 10',0xA,0 strNotEqual: db 'edx is not equal to 10',0xA,0 fmtStr: db 'Argument: %i',0xA,0 section .text main: push 10 call isValueEqualTo10 add esp, 4 push 11 call isValueEqualTo10 add esp, 4 ret isValueEqualTo10: push ebp ; save ebp into stack mov ebp, esp ; copy esp into ebp mov edx, [ebp+8] ; Copy the first argument into edx ; 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): ; int printf(const char *format, ...); add esp, 8 ; compare mov edx, [ebp+8] ; Copy the first argument into edx cmp edx, 10 ; Is edx equal to 10? jne else ; Go to else label if edx is not equal to 10 ; Load the format string into the stack sub esp, 4 ; Allocate space on the stack for one 4 byte parameter lea eax, [strEqual] ; Load string into eax mov [esp], eax ; Copy eax into the address of esp ; Call printf call printf ; Call printf(3): ; int printf(const char *format, ...); jmp endBranch else: ; Load the format string into the stack sub esp, 4 ; Allocate space on the stack for one 4 byte parameter lea eax, [strNotEqual] ; Load string into eax mov [esp], eax ; Copy eax into the address of esp ; Call printf call printf ; Call printf(3): ; int printf(const char *format, ...); endBranch: add esp, 4 pop ebp ret
https://github.com/randcode-generator/branch-in-assembly
To compile and run:
nasm -f elf32 branch.asm && gcc -o branch branch.o && ./branch
Memory Mapped Files in Java
Program1.cs
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Program1 {
public static void main(String[] args) throws Exception {
File f = new File("/tmp/test.txt");
f.delete();
FileChannel fc = new RandomAccessFile(f, "rw").getChannel();
{
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
String input = "Hello world!!";
for (int i = 0; i < input.length(); i++) {
mem.putChar(input.charAt(i));
}
mem.putChar('\0');
}
System.out.println("Run program 2 then press any key");
System.in.read();
{
System.out.println("\nProgram 2 said: ");
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
char c;
while ((c = mem.getChar()) != '\0') {
System.out.print(c);
}
System.out.println();
}
}
}
Program2.java
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Program2 {
public static void main(String[] args) throws Exception {
File f = new File("/tmp/test.txt");
FileChannel fc = new RandomAccessFile(f, "rw").getChannel();
{
System.out.println("Program 1 said \n");
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
char c;
while ((c = mem.getChar()) != '\0') {
System.out.print(c);
}
System.out.println();
}
{
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
String input = "Randomly generating code";
for (int i = 0; i < input.length(); i++) {
mem.putChar(input.charAt(i));
}
mem.putChar('\0');
}
}
}
Memory Mapped Files in C#
Program1.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Program1
{
class Program
{
static void Main(string[] args)
{
using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MyProgram", 1000))
{
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryWriter bw = new BinaryWriter(stream);
byte [] b = Encoding.ASCII.GetBytes("Hello world!!");
foreach (byte oneByte in b)
{
bw.Write(oneByte);
}
}
Console.WriteLine("Run program 2 then press any key");
Console.ReadKey();
Console.WriteLine("\nProgram 2 said: ");
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryReader sr = new BinaryReader(stream);
int c;
while ((c = sr.Read()) != 0)
{
Console.Write((char)c);
}
}
Console.WriteLine();
}
}
}
}
Program2.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Program2
{
class Program
{
static void Main(string[] args)
{
using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("MyProgram"))
{
Console.WriteLine("Program 1 said \n");
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryReader sr = new BinaryReader(stream);
int c;
while ((c = sr.Read()) != 0)
{
Console.Write((char)c);
}
Console.WriteLine();
}
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryWriter bw = new BinaryWriter(stream);
byte[] b = Encoding.ASCII.GetBytes("Randomly generating code");
foreach (byte oneByte in b)
{
bw.Write(oneByte);
}
}
}
}
}
}
Extension methods in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExtensionMethods
{
public static class StringExtension
{
public static string capAlternative(this string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if (i % 2 == 0)
sb.Append(Char.ToUpper(str[i]));
else
sb.Append(str[i]);
}
return sb.ToString();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("World of Warcraft".capAlternative());
}
}
}
Extension methods in Ruby
class String def capAlternative tempStr = "" index = 0 self.each_char do |c| if index % 2 == 0 tempStr << c.capitalize else tempStr << c end index = index + 1 end return tempStr end end puts "World of Warcraft".capAlternative
Loops in assembly
global main extern printf section .data fmtStr: db '%i',0xA,0 section .text main: mov edx, 10 ; Copy 10 to edx looping: push edx ; Save edx by pushing into stack ; Load the first argument into the stack sub esp, 4 ; Allocate space on the stack for one 4 byte parameter mov [esp], edx ; Copy eax into the 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): ; int printf(const char *format, ...); ; Return the stack (pointer) to it's original position (0 elements) add esp, 8 ; Pop the stack (There were 2 "sub esp,4") pop edx ; Pop the stack and assign value to edx dec edx ; Decrement edx by 1 cmp edx, 0 ; Is edx equal to 0? zf is set if edx is 0 jnz looping ; if flag does not have zf set, go to "looping" ret
https://github.com/randcode-generator/loops-in-assembly
To compile and run:
nasm -f elf loop.asm && gcc -o loop loop.o && ./loop
Function call assembly
global main
extern printf
section .data
fmtStr: db '%i',0xA,0
section .text
main:
push 5 ; argment 4
push 10 ; argment 3
push 20 ; argment 2
push 50 ; argment 1
call calculate ; call the function "calculate"
; eax has the return value of calculate function
add esp, 16 ; pop stack by 16. (4 arguments * 4 bytes each argument)
push eax ; store the return value in stack
lea eax, [fmtStr] ; load the formatted string into eax
push eax ; store formatted string into eax
call printf ; call the printf function
add esp, 8 ; pop stack by 8 (2 elements * 4 bytes each element)
ret ; return function
calculate:
; formula: arg1 * arg2 + arg3 * arg4
push ebp ; save ebp into stack
mov ebp, esp ; copy esp into ebp
sub esp, 4 ; move stack pointer by 4 (1 local variable)
mov eax, [ebp+8] ; copy argument 1 (50) into eax
imul eax, [ebp+12] ; multiply eax with argument 2 (20) to eax
mov [ebp-4], eax ; copy the eax value (20 * 50 = 1000) into local variable (ebp-4)
mov eax, [ebp+16] ; copy argument 3 (10) into eax
imul eax, [ebp+20] ; multiply eax with argument 4 (10 * 5 = 50) to eax
add eax, [ebp-4] ; add eax with local variable (ebp-4)
mov esp, ebp ; copy ebp to esp. All local variables are popped
pop ebp ; copy the top of the stack to ebp, restore the original ebp value
ret ; return function
https://github.com/randcode-generator/function-call-assembly.git
To compile and run:
nasm -f elf function.asm && gcc -o function function.o && ./function
Output in assembly
; http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html global _start section .data message: db 'Printout', 0xA messageLen: equ $-message section .text _start: ; Call write(2) syscall: ; ssize_t write(int fd, const void *buf, size_t count) mov eax, 4 ; Syscall, sys_write: mov ebx, 1 ; fd: 1 is for stdout mov ecx, message ; buf: address of string mov edx, messageLen ; count: string length int 0x80 ; interrupt ; Call exit(2) syscall: ; void exit(int status) mov eax, 1 ; Syscall, sys_exit mov ebx, 0 ; status: exit status 0 int 0x80
https://github.com/randcode-generator/output-in-assembly
To compile and run:
nasm -f elf output.asm && ld output.o && ./a.out
Calling printf in assembly using nasm
global main extern printf section .data fmtStr: db 'printf: a1 - %i, a2 - %s',0xA,0 param1: db 'Parameter String' section .text main: ; Load the second argument into the stack sub esp, 4 ; Allocate space on the stack for one 4 byte parameter lea eax, [param1] ; Load string into eax mov [esp], eax ; Copy eax into the address of esp ; Load the first argument into the stack sub esp, 4 ; Allocate space on the stack for one 4 byte parameter mov eax, 8 ; Copy 8 into eax add eax, 1098 ; eax = eax + 1098 mov [esp], eax ; Copy eax into the 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): ; int printf(const char *format, ...); ; Return the stack (pointer) to it's original position (0 elements) add esp, 12 ; Pop the stack (There were 3 "sub esp,4") ret
https://github.com/randcode-generator/printf-in-assembly
To compile and run:
nasm -f elf32 printf.asm && gcc -o printf printf.o && ./printf