Monday 25 November 2013

Guess, Spock. Your best guess.


...
Spock: Guessing is not in my nature, Doctor.
McCoy: Well... nobody's perfect.
Q: Do you think everything can be explained and will be explained?
A: Yes, though it depends what you mean by explain. I believe the Universe is everything and the only comprehensive explanation of the Universe is the Universe itself. However, explanation (the way I've learned that word) does not imply omniscience. Explanation is a model, an approximation of the whole, a valid generalization within certain (error) limits, something which attemps to converge towards the correct answer. Infinitely approach it, but never quite reach it. I believe we can make "the best guess" based on finite amount of information we've fed into the model, a guess we can be proud of, a guess which doesn't warrant regret even if we find points which won't fit. If that occurs, we'll just make a better guess, a more sophisticated guess, an educated guess.
We will never be right about anything, it's like trying to model a fractal by fitting polynomials into it. It is infinitely complex from the context of the polynomial, but is still something we can understand an approximate. Kind of like some peoples worldviews, I find it odd people get so upset when their worldviews are proven wrong, they should expect it, embrace it. The world does not flow along a smooth line you can draw on a map, you can never be sure your worldview is converging towards the global minimum (but that's why we need noise), and thank goodness for that, we'd be bored to death in no time at all if that was the case.
http://en.wikipedia.org/wiki/Overfitting
http://en.wikipedia.org/wiki/Global_optimization
http://www.esrf.eu/computing/scientific/FIT2D/MF/node1.html
--

No matter how good you become, you'll never beat Moore's law, but even if you did, you'd still be stuck on this planet, in this solar system, this galaxy and this universe. How far is far enough?

--
Looking into building small executables (for anything) and possibly a small os and compiler for the raspberry pi...



; nasm -f bin elf_x86.asm
BITS 32

                org     0x08048000

  ehdr:                                                 ; Elf32_Ehdr
                db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
        times 8 db      0
                dw      2                               ;   e_type
                dw      3                               ;   e_machine
                dd      1                               ;   e_version
                dd      _start                          ;   e_entry
                dd      phdr - $$                       ;   e_phoff
                dd      0                               ;   e_shoff
                dd      0                               ;   e_flags
                dw      ehdrsize                        ;   e_ehsize
                dw      phdrsize                        ;   e_phentsize
                dw      1                               ;   e_phnum
                dw      0                               ;   e_shentsize
                dw      0                               ;   e_shnum
                dw      0                               ;   e_shstrndx

  ehdrsize      equ     $ - ehdr

  phdr:                                                 ; Elf32_Phdr
                dd      1                               ;   p_type
                dd      0                               ;   p_offset
                dd      $$                              ;   p_vaddr
                dd      $$                              ;   p_paddr
                dd      filesize                        ;   p_filesz
                dd      filesize                        ;   p_memsz
                dd      5                               ;   p_flags
                dd      0x1000                          ;   p_align

  phdrsize      equ     $ - phdr

  _start:
                mov     eax, 4                          ; syscall 4: write
                mov     ebx, 1                          ; stdout
                mov     ecx, msg                        ; message address
                mov     edx, 13                         ; number of bytes
                int     0x80                            ; invoke syscall

                mov     eax, 1                          ; syscall 1: exit
                xor     ebx, ebx                        ; return code 0
                int     0x80                            ; invoke syscall

  msg:          db      'hello, world',10,13            ; also \n

  filesize      equ     $ - $$
decod@korpimaa:~/tiny$ nasm -f bin elf_x86.asm
decod@korpimaa:~/tiny$ ls -l elf_x86
-rwxrwxr-x 1 decod decod 129 Nov 11 19:45 elf_x86
decod@korpimaa:~/tiny$ ./elf_x86
hello, world


/*  x64 asm */
#define write(message, length) \
asm ( \
"mov $1, %%rax;" \
"mov $1, %%rdi;" \
"mov %0, %%rsi;" \
"mov %1, %%rdx;" \
"syscall" \
:: "r" (&message), "g" (length) \
: "rax", "rdi", "rsi", "rdx")

#define exit() \
asm ( \
"mov $60, %rax;" \
"mov $0, %rdi;" \
"syscall")

_start() {
        char str[] = "Hello, World\n";

        write(str, 13);
        exit();
}
decod@korpimaa:~/tiny$ gcc -nostdlib hello_x86.c

decod@korpimaa:~/tiny$ strip a.out
decod@korpimaa:~/tiny$ ./a.out
Hello, World
decod@korpimaa:~/tiny$ ls -lah a.out
-rwxrwxr-x 1 decod decod 1.1K Nov 11 19:48 a.out

decod@korpimaa:~/tiny$ objcopy a.out -O binary
decod@korpimaa:~/tiny$ ls -lah a.out
-rwxrwxr-x 1 decod decod 192 Nov 11 19:49 a.out

------------------------------------------------------------------
#define write(msg) asm volatile ( \
"mov r0, $1;" \
"mov r1, %0;" \
"mov r2, $13;" \
"mov r7, $4;" \
"swi $0" \
:: "r" (msg) \
: "r0", "r1", "r2", "r7")

#define exit() asm ( \
"mov r0, $0;" \
"mov r7, $1;" \
"swi $0")

_start() {
        char str[] = "Hello, World\n";

        write(str);
        exit();
}
pi@raspberrypi ~ $ gcc helloarm.c -nostdlib

pi@raspberrypi ~ $ strip a.out
pi@raspberrypi ~ $ ls -lah a.out
-rwxr-xr-x 1 pi pi 720 Nov 11 17:50 a.out

pi@raspberrypi ~ $ objcopy a.out -O binary
pi@raspberrypi ~ $ ls -lah a.out
-rwxr-xr-x 1 pi pi 140 Nov 11 17:50 a.out

------------------------------------------------------------------
ubuntu@ubuntu:~$ wget bellard.org/otcc/otccelf.c
ubuntu@ubuntu:~$ gcc -m32 otccelf.c
ubuntu@ubuntu:~$ ./a.out otccelf.c otccelf1
ubuntu@ubuntu:~$ chmod +x otccelf1
ubuntu@ubuntu:~$ ./otccelf1
usage: otccelf file.c outfile

ubuntu@ubuntu:~$ wget bellard.org/otcc/otccelfn.c
ubuntu@ubuntu:~$ wget bellard.org/otcc/otccex.c
ubuntu@ubuntu:~$ gcc -m32 otccelfn.c
ubuntu@ubuntu:~$ ./a.out otccex.c otccex
ubuntu@ubuntu:~$ chmod a+x otccex
ubuntu@ubuntu:~$ ./otccex
usage: ./otccex n [base]
Compute fib(n) and fact(n) and output the result in base 'base'


http://bellard.org/otcc/

Something to aim for...


------------------------------------------------------------------


http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/

------------------------------------------------------------------

Some old effects...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>

int WinMain() {
SDL_Event event;
SDL_Surface *screen, *textureImg;
int quit = 0, shiftX, shiftY, shiftLookX, shiftLookY;
float timeDisplacement = 0.0;
int *p, *q;
int x, y;
int *distanceTable=(int *)malloc(4*1024*1024);
int *angleTable=(int *)malloc(4*1024*1024);
int w=512, h=512;
int angle;
int depth;
int texture_x=0, texture_y=0;

textureImg = SDL_LoadBMP("red_smoke.bmp");
for(x=0; x<w*2; x++)
for(y=0; y<h*2; y++) {
depth = 32.0 * 512 / sqrt(1.0*((x - w) * (x - w) + (y - h) * (y - h)));
angle = 0.5 * 512 * atan2(1.0*(y - h), 1.0*(x - w)) / 3.141592653589793;
distanceTable[x+y*1024] = depth;
angleTable[x+y*1024] = angle;
  }
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(512, 512, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
while(!quit) {
timeDisplacement += 0.015;
shiftX = 512 * .2 * timeDisplacement+300;
  shiftY = 512 * .15 * timeDisplacement+300;
shiftLookX = w / 2 + 0*w / 4 * sin(timeDisplacement);
shiftLookY = h / 2 + 0*h / 4 * sin(timeDisplacement * 1.5);
for(y=0; y<512; y++)
for(x=0; x<512; x++) {
texture_x = abs((distanceTable[ (x+shiftLookX) + (y+shiftLookY)*1024] + shiftX)) % 512;
texture_y = abs((angleTable[ (x+shiftLookX) + (y+shiftLookY)*1024] + shiftY)) % 512;
        q = (int *)textureImg->pixels+(texture_x+texture_y*511);
        p = (int *)screen->pixels+(x+y*512);
        *p = *q;
      }
SDL_PollEvent(&event);
if(event.type==SDL_QUIT) quit = 1;
SDL_Flip(screen);
}
SDL_Quit();
free(distanceTable);
free(angleTable);
return 0;
}
--

You'd never let me win anyway...
...well, that wouldn't be winning.