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.