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 |
|---|
| /** | |
| * 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: | |
| * |
| /** | |
| * Contiguous and non-contiguous C-arrays allocation in memory. | |
| * by Dmitry Soshnikov <[email protected]> | |
| */ | |
| #include <iostream> | |
| int main() | |
| { | |
| // ----------------------------- |
Sometimes more code in a source language, can mean less code in a generated language.
NOTE: below we used
gcc 5.3with-std=c++14 -O3flags.
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`.| (* | |
| * "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]> |
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| /** | |
| * 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 |