Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / asm-addr-mode.md
Last active January 19, 2017 03:07
Addressing modes in `mov`, and their C version
Addressing modes in mov, and their C version

Below are addressing modes (immediate, register, memory) on the examples of the mov assembly instruction. Here we use movl ("move long") for a 32-bit word.

Note: cannot move memory-to-memory, need two instructions.

Src
movl
@DmitrySoshnikov
DmitrySoshnikov / s-expression-parser.js
Last active October 9, 2023 05:59
S-expression parser
/**
* S-expression parser
*
* Recursive descent parser of a simplified sub-set of s-expressions.
*
* NOTE: the format of the programs is used in the "Essentials of interpretation"
* course: https://github.com/DmitrySoshnikov/Essentials-of-interpretation
*
* Grammar:
*
@DmitrySoshnikov
DmitrySoshnikov / c-arrays-allocation.c
Last active February 19, 2016 00:08
Contiguous and non-contiguous C-arrays allocation
/**
* Contiguous and non-contiguous C-arrays allocation in memory.
* by Dmitry Soshnikov <[email protected]>
*/
#include <iostream>
int main()
{
// -----------------------------
@DmitrySoshnikov
DmitrySoshnikov / more-code-less-code.md
Last active February 27, 2016 19:49
x86: More code - less code

x86: More code - less code

Sometimes more code in a source language, can mean less code in a generated language.

NOTE: below we used gcc 5.3 with -std=c++14 -O3 flags.

On the example of C programming language, and its translation to x64 assembly, let's take a look at the following example:

// 7 times `x`.
@DmitrySoshnikov
DmitrySoshnikov / eoi-ml-lesson1-ae.ml
Last active March 1, 2016 20:50
Essentials of interpretation, ML version: Lesson 1: Arithmetic expressions evaluator
(*
* "Essentials of interpretation. Semantics of programming languages"
*
* Lesson 1: Arithmetic expressions evaluator.
*
* BNF grammar:
*
* NUMBER -> \d+
*
* Exp -> NUMBER
/**
* Stack buffer overflow.
*
* TL;DR: In older OSes, a buffer overflow of the data on the stack
* allowed overriding a return address, and execute an exploit code,
* instead of returning to a caller.
*
* Docs: https://en.wikipedia.org/wiki/Stack_buffer_overflow
*
* by Dmitry Soshnikov <[email protected]>
@DmitrySoshnikov
DmitrySoshnikov / expression-only.md
Last active April 16, 2016 12:00
ES: Expression only

"Everything is an expression"... since ES1?

Many languages support "expression-only" semantics, allowing to use any construct in an expression position.

NOTE: the difference between expressions and statements is that the former produce a value, while the later don't.

For example, Ruby's if-expression:

x = 10
@DmitrySoshnikov
DmitrySoshnikov / es6-list-negative-indices.js
Last active August 17, 2016 05:19
ES6 List negative indices
/**
* Reverse indices of Lists/Arrays in ES6.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*/
'use strict';
class List extends Array {
/**
* JS scoping, and this property resolutions rules in ES7/ES8.
*
* Quiz: x is gone, and x is everywhere!
*
* Help find Xs! What's the output?
*/
let x = 1;
class Foo {
get x() {}
set x(v) {}
}
class Bar extends Foo {
x = 42;
}
console.log(new Bar().x); // undefined, should be 42