Archive for » 02月, 2009 «

Signs

也许,爱情往往在不经意间来临。

Category: daily, funny  Comments off

软盘引导扇区

当电脑以磁盘启动时,BIOS会自动载入这个磁盘的第一个扇区(共512字节),即这个磁盘的0面0磁道1扇区。如果这个扇区是以0xAA55这个两字节的16进制数结束,即该扇区最后两位是0×55、0xAA(AA为高位,因此在后),那么BIOS就会认为它是一个引导扇区。0xAA55因此也称为引导标识(Boot Sector Signature),而磁盘的第一个扇区也称为引导扇区(Boot Sector)。

如果BIOS发现该引导扇区可引导,那么BIOS就会将控制权全部交给这个扇区的代码。也就是说从此BIOS撒手不管了,接下来的时就由这512字节的引导代码来做了。你可以认为BIOS唯一做的东西就是载入引导扇区(固定地都是载入到内存中的0000:7C00处),并判断最后的两字节是否0xAA55,如果是那么就将控制权交给这个扇区的代码。

详细内容见这里

好了,下面来实践一下。在网上找到了很多代码,其中这个代码是最简短的一个,使用NASM来进行编译。

[BITS 16] ;Tells the assembler that its a 16 bit code
[ORG 0x7C00] ;Origin, tell the assembler that where the code will
;be in memory after it is been loaded

MOV SI, HelloString ;Store string pointer to SI
CALL PrintString ;Call print string procedure
JMP $ ;Infinite loop, hang it here.

PrintCharacter: ;Procedure to print character on screen
;Assume that ASCII value is in register AL
MOV AH, 0×0E ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0×00 ;Page no.
MOV BL, 0×07 ;Text attribute 0×07 is lightgrey font on black background

INT 0×10 ;Call video interrupt
RET ;Return to calling procedure

PrintString: ;Procedure to print string on screen
;Assume that string starting pointer is in register SI

next_character: ;Lable to fetch next character from string
MOV AL, [SI] ;Get a byte from string and store in AL register
INC SI ;Increment SI pointer
OR AL, AL ;Check if value in AL is zero (end of string)
JZ exit_function ;If end then return
CALL PrintCharacter ;Else print the character which is in AL register
JMP next_character ;Fetch next character from string
exit_function: ;End label
RET ;Return from procedure

;Data
HelloString db ‘Hello World’, 0 ;HelloWorld string ending with 0

TIMES 510 – ($ – $$) db 0 ;Fill the rest of sector with 0
DW 0xAA55 ;Add boot signature at the end of bootloader

使用NASM编译成boot.bin,并写到磁盘(可千万不要写到自己的硬盘上哦,拿软盘做实验吧~)的第一扇区上就可以了。重启动系统并以该磁盘启动就行了。瞧,经典的Hello World!

Category: program, reading  Comments off

Apache上运行Perl CGI

最近迷上了Perl,想用Perl写一个WEB软件来管理一些工作上的内容。以前都是用Java来写的web程序,这次换成Perl尝试一下,估计也挺有趣。可是一开始就遇到一些问题,折腾了一个晚上。幸好最后还是解决了。将过程记录下来,希望对后来者有用。

安装Apache服务器

web服务器是一个软件,也可以把它看作是一个web程序的运行平台,我们通过它来发布我们的web程序。Apache便是众多的web服务器软件中的一个,应该也是最流行的那一个。

首先到Apache的官方网站下载。我这里使用了Apache HTTP Server 2.2的Windows版本,后面也都是按照这个版本进行安装配置。

下载下来一路“下一步”安装完成后,打开浏览器输入“http://localhost/”。如果显示“It works!”的话,恭喜你,Apache服务安装成功了。

安装Perl

Perl在Windows上的Perl语言解释器是Active Perl。我这里使用的版本是5.8。

下载后同样是一路“下一步”。安装完后在命令行中输入“perl -V”,如果显示版本信息的话则表示你安装成功了。

让Apache运行Perl CGI

在Apache的安装目录下找到conf/httpd.conf文件,用记事本或其他的文本编辑器打开。

查找“ScriptAlias /cgi-bin/”,可能你的会是下面的这个结果:

ScriptAlias /cgi-bin/ “C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/”

如果这行前面的有个“#”符号,就将它去掉。

找到下面的这段文本:

<Directory />
Options FollowSymLinks
AllowOverride all
Order deny,allow
Deny from all
</Directory>

加上“+ExecCGI”,即:

<directory />
Options FollowSymLinks +ExecCGI
AllowOverride all
Order deny,allow
Deny from all
</Directory>

一般来说,Perl CGI脚本就是以cgi为拓展名的,不过你也可以设置成pl为拓展名。修改方法是这样的,查找下面的这行:

AddHandler cgi-script .cgi

在末尾加上“.pl”,即:

AddHandler cgi-script .cgi .pl

好了,修改完上面所说的配置,重新启动Apache即可。

哦,对了,忘了写测试页面。在“C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/”下建立一个名叫“index.pl”的文件,里面写:

#!/usr/bin/perl
print “Content-type: text/html\r\n\r\n”;
print “Hello, Perl CGI!”;

打开浏览器输入“http://localhost/cgi-bin/index.pl”。如果显示“Hello, Perl CGI!”的话,恭喜你,你的第一个基于Apache的Perl CGI程序部署成功了!

安装mod_perl模块

最后作为题外话需要提一下mod_perl模块。关于mod_perl是什么,官方网站说得比较清楚,我就不说了。我们还是来说说如何安装及配置mod_perl吧。

首先是安装mod_perl。在命令行中输入以下命令:

ppm install http://theoryx5.uwinnipeg.ca/ppms/mod_perl.ppd

这时ppm就会自动下载并进行编译安装,到最后它会问你Apache的模块目录在哪里,这时你要输入你的Apache的mudules文件夹的路径。如:

C:\Program Files\Apache Software Foundation\Apache2.2\modules

安装好mod_perl.so了。下面来进行配置,让Apache装置mod_perl模块。在你的httpd.conf的末尾加上以下文字:

LoadFile “D:/Perl/bin/perl58.dll”
LoadModule perl_module modules/mod_perl.so
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

其中 “D:/Perl/bin/perl58.dll”根据你的perl的实际安装路径进行修改。

好了,配置到此为此。重新启动Apache,并在浏览器地址栏中输入“http://localhost/cgi-bin/index.pl”。嗯,又看到“Hello, Perl CGI!”了!

现在,你可以自由自在地写Perl CGI程序了~Enjoy~!

Category: program  Comments off

第14个奖杯会到来的!

这场比赛输了,但我们相信你会超过桑神,拿到第14个,第15个大满贯奖杯的!

Category: daily  Comments off