What Is 0 ^ 0?

Quick questions:

What is 2 ^ 3? 8

What is 10 ^ 2? 100

What is 912873683428920348791239817231928370 ^ (200 – (40 * 5)) ? 1

Very good. It is an easy test, right? Yes indeed. OK let’s try another question: what is 0 ^ 0?

I repeat: what is 0 ^ 0?

..

..

Give up? OK. This time I’ll answer this question programmatically, not mathematically (there are several approach used to tackle this problems: limit, set theory, etc). Here’s the X86 assembly to outline the idea:


format PE console 4.0

include 'win32a.inc'

entry start

section '.data' readable executable
num1 dd 0
num2 dd 0
fmt db "%d",0

section '.code' code readable executable
start:
mov ecx, [num1]
mov eax, [num2]
mov ebx, ecx

process:
cmp eax, 1
jg multiply
je done

multiply:
imul ecx, ebx
dec eax
jmp process

done:
invoke printf, fmt, ecx
invoke ExitProcess, 0

section '.idata' import data readable writable
library kernel32,'kernel32.dll', msvcrt, 'msvcrt.dll'
import kernel32, ExitProcess, 'ExitProcess'
import msvcrt, printf, 'printf'

The output is, surprisingly (or not surprisingly) 0.

I expect careful readers to complain that I’m cheating, since the output of the code will always be 0.

No, I’m not cheating. May I suggest you to play with the code yourself? Probably you’ll be even more surprised :mrgreen:

Inside MP3

If you are curious about the structure of MP3 file, please take a look at this:

MP3 File Structure Description

MP3 file is divided into a small blocks – frames. Each frame has constant time length 0.026 sec.
But size of one frame (in Bytes) varies according to bitrate. Eg. for 128kbps it is (normally) 417 Bytes and for 192kbps 626 Bytes.

The first 4 Bytes of each frame is frame header and the rest is audio data…

I have a strong felling that I’ll mess with this soon.

Ouch.

Always Use error_reporting(E_ALL)

PHP

I’m mostly a Java guy, who very rarely use PHP. Recently, I just wrote some web services (which is obviously ini PHP) for the project I’m working on. One of them didn’t work as expected, and I could not figure it out (must have been sleepy!!)

function XOR_encrypt($message, $key){
$ml = strlen($message);
$kl = strlen($key);
$newmsg = "";

for ($i = 0; $i < $ml; $i++){
$newmsg = $newmsg . ($msg[$i] ^ $key[$i % $kl]);
}

return base64_encode($newmsg);
}

I spent 2 hours looking at the code scratching my head, wondering why on earth it gave the wrong result. Of course, aware readers will notice the error instantly: the msg variable hasn’t been declared before. And PHP happily ran the code without giving warnings.

Finally, a friend suggested to use error_reporting. I did, and reloaded the code. Then I saw a bunch of errors.

Well, what a waste of 2 hours :mrgreen:

 

No more command line tools in Windows 8 SDK?

Well, this is quite annoying, IMO.

http://msdn.microsoft.com/en-us/windows/hardware/hh852363.aspx

Sometimes VS (Visual Studio) Express is not adequate for Windows programming, and the Pro/Premium/Ultimate/etc version is pricey. Until now, the alternative is to use Windows SDK , or the illegal copies from the torrents. It has command-line build enviroment, C++ libraries,  .NET framework, etc (no MFC, though). :mrgreen:

And MS just removed the command-line build environment from the Windows SDK, which means no nmake.exe, cl.exe, ml.exe, link.exe, and so on. I wonder why?

Probably because they don’t like people cheating, and then can force people to use VS instead?

Honestly I don’t understand the rationale behind this decision. Windows SDK is obviously not a drop dead replacement for VS, so why cripple it? That’s just silly. Not every person is a professional programmer who can afford the commercial version of VS. Sometimes Windows SDK is good enough. This is a bad news for hobbyists.

If my understanding is correct, then… bad decision MS, bad decision… 😕 🙄

A Simple CMS written in assembly

Someone is working on a simple CMS, written in assembly.

..

Wait a sec. Did you just say assembly? Yup. FASM, to be precise. The CMS it’s called MiniMagAsm.

That’s CGI programming. And honestly, I’m not familliar with it, so I just built and ran it using lighttpd.

It works. Maybe somewhen in the future we’ll have a web framework written in assembly.

:mrgreen:

Building GCC on Windows…

is a PITA (pain in the a**). Yeah, really.

Not convinced? Please give it a try. See how many hours spent on building the compiler. Every time I build GCC, my notebook (4 years old) fan will speed up like a F1 car. :mrgreen:

Fortunately, Windows users don’t need the build GCC themselves. Some peoples already done them. Just google “MinGW GCC” or “GCC for Windows” or “GCC 4.7 Windows”, for example, and you’ll found some GCC builds, for example:

  1. MinGW (obviously)
  2. Cygwin (go get this if you want to emulate UNIX environment on Windows)
  3. TDM GCC
  4. mingw-builds
  5. MinGW distro (I like this one. You have Boost, PCRE, libjpeg, and some other libs included).
  6. Fortran, C and C++ for Windows
  7. Cross-Mingw x86/x64 (20120306), with GCC 4.7.0 (Pre-Release RC1)

See? :mrgreen:

For the brave souls who want to build GCC themselves, please take a look at this guide.

..

..

..

BTW, building GCC on Linux is a less PITA, compared to on Windows. But is still a PITA, though. Even when I’m working on Linux, I never have interest to build GCC myself. Better to trust the package manager on this issue :mrgreen:

Surprise Surprise!

Could you figure out what is the output of this snippet?


format PE console 4.0

include 'win32a.inc'

entry start

section '.data' data readable
fmt db "%d ", 0

section '.code' readable executable
start:
 mov ecx, 15
 cinvoke printf, fmt, ecx
 inc ecx
 cinvoke printf, fmt, ecx
 inc ecx
 cinvoke printf, fmt, ecx
invoke ExitProcess,

section '.idata' data import readable writeable
library kernel32, 'kernel32.dll', msvcrt, 'msvcrt.dll'
import kernel32, ExitProcess, 'ExitProcess'
import msvcrt, printf, 'printf'

At a glance, this snippet looks so simple and self-explaining.

mov ecx, 15 -> ecx = 15
cinvoke printf, fmt, ecx -> print 15
inc ecx -> ecx = 15 + 1 = 16
cinvoke printf, fmt, ecx -> print 16
inc ecx -> ecx = 16 + 1 = 17
cinvoke printf, fmt, ecx -> print 17
 invoke ExitProcess, 0 -> exit

So the result must be 15 16 17, right? That’s easy.

..

..

..

Huh??

Built and run it  yourself (use FASM). On my laptop, the result is 15 1986971169 1986971169.

Surprised? :mrgreen:

The pitfall is the ECX register is trashed, due to the invocation of printfs (hint).

So, what to do, then? Simple. Either

  1. Use EBX, ESI, or EDI because they are guaranteed to be preserved
  2. Or use a pair of push & pop to preserve the value, if you must use EAX, ECX, etc.

Example #1


format PE console 4.0

include 'win32a.inc'

entry start

section '.data' data readable
fmt db "%d ", 0

section '.code' readable executable
start:
 mov ebx, 15
 cinvoke printf, fmt, ebx
 inc ebx
 cinvoke printf, fmt, ebx
 inc ebx
 cinvoke printf, fmt, ebx
 invoke ExitProcess, 0

section '.idata' data import readable writeable
library kernel32, 'kernel32.dll', msvcrt, 'msvcrt.dll'
import kernel32, ExitProcess, 'ExitProcess'
import msvcrt, printf, 'printf'

Example #2


format PE console 4.0

include 'win32a.inc'

entry start

section '.data' data readable
fmt db "%d ", 0

section '.code' readable executable
start:
 mov ecx, 15
 push ecx
 cinvoke printf, fmt, ecx
 pop ecx
 inc ecx
 push ecx
 cinvoke printf, fmt, ecx
 pop ecx
 inc ecx
 push ecx
 cinvoke printf, fmt, ecx
 pop ecx
 invoke ExitProcess, 0

section '.idata' data import readable writeable
library kernel32, 'kernel32.dll', msvcrt, 'msvcrt.dll'
import kernel32, ExitProcess, 'ExitProcess'
import msvcrt, printf, 'printf'

Either way works :mrgreen:

BTW, the reason I write this post is sometimes I still stumble unto this error. So, yeah, let this post be the reminder.

Check If Yahoo! ID Is Online Or Not Using Python

Now is almost 1 PM, and I still cannot sleep. Suddenly I get the idea to write something in Python: a script to check whether a Yahoo! ID is online or not.

The basic principle is to use this Yahoo! API:

http://presence.msg.yahoo.com/online?u=<Yahoo! ID>

Now let’s say your Yahoo! ID is john.doe. Open this URL in your browser:

http://presence.msg.yahoo.com/online?u=john.doe

If that ID is online, you’ll see this picture:

Otherwise, you’ll see this:

To sum it up, this is the algo:

  1. Call the URL, and save the result as a file.
  2. Calculate the MD5 sum of the file (of course you are not limited to MD5, you can use any checksum algo)
  3. See the ‘online’ image above? It’s MD5 sum is 9e31b63daa88647ead982d2c7598e634. Now calculate the MD5 sum of the saved file. If both MD5 sums are match, it means that the Yahoo! ID is online. Otherwise, it’s offline.
And this is the Python script.

#!/usr/bin/python2

# A simple script to check whether a Yahoo! ID is online or not
# Usage: python2 ycheck.py
# Example: python2 ycheck.py john.doe

import pycurl
import StringIO
import md5
import sys

ONLINE_VALUE = '9e31b63daa88647ead982d2c7598e634'
buffer = StringIO.StringIO()

# Call the API via curl
# and save the result into file called 'img'
user = sys.argv[1]
curl = pycurl.Curl()
curl.setopt(curl.URL, 'http://presence.msg.yahoo.com/online?u='+user)
curl.setopt(curl.WRITEFUNCTION, buffer.write)
curl.perform()
tmpfile = open('img', 'wb')
tmpfile.write(buffer.getvalue())
tmpfile.close()

# Calculate the MD5 of 'img'
tmpfile = open('img','rb')
md5_result = md5.new(tmpfile.read()).hexdigest()
tmpfile.close()

# Compare it with the predefined value above
if (md5_result == ONLINE_VALUE):
 print'%s is online' % (user)
else:
 print '%s is offline' % (user)

P.S: I make NO claims that the above code is robust/efficient/etc. It’s just a quick n’ dirty script, anyway :mrgreen:

BTW, the above code is only tested with Python 2, because the pycurl available in the GNU/Linux distro I’m using is not compatible with Python 3.

Installing the development snapshot of FPC on Linux

I’m using ArchLinux. The latest version of FPC (Free Pascal) which is available in the official repo is 2.6.0

$ pacman -Ss fpc
community/fpc 2.6.0-1 [installed]
The Free Pascal Compiler is a Turbo Pascal 7.0 and Delphi compatible 32bit Pascal Compiler. It comes with fully TP 7.0 compatible run-time library.

So what if you want to use the development version, which at the time of this post, is 2.7.0? Easy.

First, grab the development snapshot:

svn checkout http://svn.freepascal.org/svn/fpc/trunk fpc

Next, build it:

 sudo make install INSTALL_PREFIX=/usr

Hmm it seems to be done. So let’s give it a try. Type fpc on your favourite shell.

$ fpc
Free Pascal Compiler version 2.6.0 [2012/01/02] for x86_64
Copyright (c) 1993-2011 by Florian Klaempfl and others
/usr/lib/fpc/2.6.0/ppcx64 [options]  [options]

Wait a sec. This is FPC 2.6.0, and not 2.7.0. There must be an error.
..
..
..
Actually there is no error at all.

$ ls /usr/lib/fpc/
2.4.4  2.6.0  2.7.1  lexyacc  ppcx64

See, the 2.7.1 directory?

And BTW

$ file /usr/bin/ppcx64 
/usr/bin/ppcx64: symbolic link to `/usr/lib/fpc/2.6.0/ppcx64'

Apparently, /usr/bin/ppcx64 is still pointing to /usr/lib/fpc/2.6.0/ppcx64. So, just change the symlink.

$ sudo ln -sf /usr/lib/fpc/2.7.1/ppcx64 /usr/bin/ppcx64

Now try fpc once again:

Free Pascal Compiler version 2.7.1 [2012/03/02] for x86_64
Copyright (c) 1993-2011 by Florian Klaempfl and others
/usr/lib/fpc/2.7.1/ppcx64 [options]  [options]

That’s it. Now you can use the development snapshot :mrgreen: