NASM初學

為何不使用MASM來學習?
因為MASM僅能在Intel的機器上執行,寫出來的軟體除非有買微軟的軟體和微軟簽約才能發佈,網路所提供眾多版本的新功能對新手卻不是那麼方便,最後你也不能拿到他的原始碼來研究,因為他不提供對手任何改進空間,對於以後要深入研究編譯器和系統結構又是一層問題。

Hello World for Linux

SECTION .DATA
hello: db 'Hello world!',10
helloLen: equ $-hello
SECTION .TEXT
GLOBAL _start
_start:
; Write 'Hello world!' to the screen
mov eax,4 ; 'write' system call
mov ebx,1 ; file descriptor 1 = screen
mov ecx,hello ; string to write
mov edx,helloLen ; length of string to write
int 80h ; call the kernel
; Terminate program
mov eax,1 ; 'exit' system call
mov ebx,0 ; exit with error code 0
int 80h ; call the kernel


編譯方式參考如下(因為自己的系統是64位元的)
$ nasm -f elf64 -g -F stabs <filename>.asm
$
ld -o <filename> <filename>.o
$
./<filename>
或是
$ nasm -f elf -g -F stabs <filename>.asm
$ ld -o <filename> <filename>.o -melf_i386
$ ./<filename>


如何 Debug ?
Linux 下使用 kdbg

留言

這個網誌中的熱門文章

為 Line-in 設定音量

Firefox: 設定滑鼠滾輪捲動行數