在龙芯上用nasm编写x86汇编程序

我在中科梦兰论坛发表于 2007-2-10 22:24:06

在龙芯上用nasm编写x86汇编程序
http://bbs.lemote.com/forum.php?mod=viewthread&tid=5443&fromuid=76
(出处: 龙芯论坛(loongson forum))

硬件:福珑LFLM010029(龙芯2E)
软件:Linux debian 2.6.18.1lemote #1 Thu Dec 28 16:51:25 CST 2006 mips GNU/Linux

步骤:
第一步:在福珑上安装nasm。在福珑终端中运行命令:sudo apt-get install nasm
loongson@debian:~$ sudo apt-get install nasm
正在读取软件包列表… 完成
正在分析软件包的依赖关系树… 完成
下列【新】软件包将被安装:
nasm
共升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 9 个软件未被升级。
需要下载 1599kB 的软件包。
解压缩后会消耗掉 3105kB 的额外空间。
获取:1 http://www.lemote.com etch/main nasm 0.98.38-1.2 [1599kB]
下载 1599kB,耗时 27s (57.3kB/s)
选中了曾被取消选择的软件包 nasm。
(正在读取数据库 … 系统当前总共安装有 115024 个文件和目录。)
正在解压缩 nasm (从 …/nasm_0.98.38-1.2_mipsel.deb) …
正在设置 nasm (0.98.38-1.2) …

第二步:打开gedit或其他文本编辑器,输入以下内容,保存为bintest.asm。

; test source file for assembling to binary files
; build with:
;    nasm -f bin -o bintest.com bintest.asm

; When run (as a DOS .COM file), this program should print
;    hello, world
; on two successive lines, then exit cleanly.

; This file should test the following:
; [1] Define a text-section symbol
; [2] Define a data-section symbol
; [3] Define a BSS-section symbol
; [4] Define a NASM local label
; [5] Reference a NASM local label
; [6] Reference a text-section symbol in the text section
; [7] Reference a data-section symbol in the text section
; [8] Reference a BSS-section symbol in the text section
; [9] Reference a text-section symbol in the data section
; [10] Reference a data-section symbol in the data section
; [11] Reference a BSS-section symbol in the data section

BITS 16
ORG 0x100

SECTION .text

jmp start                ; [6]

endX          mov ax,0x4c00                ; [1]
int 0x21

start          mov byte [bss_sym],’,’ ; [1] [8]
mov bx,[bssptr]        ; [7]
mov al,[bx]
mov bx,[dataptr]        ; [7]
mov [bx],al
mov cx,2
.loop          mov dx,datasym        ; [1] [4] [7]
mov ah,9
push cx
int 0x21
pop cx
loop .loop                ; [5] [6]
mov bx,[textptr]        ; [7]
jmp bx

SECTION .data

datasym          db ‘hello  world’, 13, 10, ‘$’ ; [2]
bssptr          dw bss_sym                ; [2] [11]
dataptr          dw datasym+5                ; [2] [10]
textptr          dw endX                ; [2] [9]

SECTION .bss

bss_sym          resb 1                ; [3]

第三步:编译第二步的文件。
loongson@debian:~$ nasm -f bin -o bintest.com bintest.asm

第四步:把第三步生成的bintest.com复制到一台运行DOS或WIN的X86电脑上,在命令提示符下运行bintest.com。
运行结果是在屏幕上输出两行hello, world。