2011-03-25 75 views
6

我有一個簡單的程序NASM只調用sys_exitNASM編程 - `int0x80`與`INT 0x80`

segment .text 
    global _start 
    _start: 
     mov eax, 1 ; 1 is the system identifier for sys_exit 
     mov ebx, 0 ; exit code 
     int 0x80 ; interrupt to invoke the system call 

當我第一次寫的,我犯了一個錯誤,忘記int0x80之間的空間:

 int0x80 

...但程序仍編譯沒有問題!

[prompt]> nasm -f elf MyProgram.asm 
[prompt]> ld -o MyProgram MyProgram.o 

它只是給我一個分割錯誤,當我跑它!

[prompt]> ./MyProgram 
Segmentation fault 

那麼這個程序 - 我寫的原始程序是什麼,缺少空間 - 呢?在NASM中int0x80(沒有空格)是什麼意思?

segment .text 
    global _start 
    _start: 
     mov eax, 1 
     mov ebx, 0 
     int0x80 ; no space... 
+0

發生,如果'INT 0x80的什麼'之後被調用? – 2011-03-25 02:08:37

+0

@pst - 好主意! *嘗試它*似乎正常執行!即使我添加了一個「sys_write」系統調用以使其成爲「Hello World」程序......就像'int0x80'甚至不存在:O – 2011-03-25 02:13:42

+0

您的意思是分割錯誤消失了嗎? – BoltClock 2011-03-25 02:17:33

回答

6

NASM是給我這樣的警告:

警告:單獨的標籤上沒有一個冒號行可能是錯誤的

顯然,錯字被視爲一個標籤,你可以像往常一樣參考程序中新的int0x80標籤:

segment .text 
    global _start 
    _start: 
     mov eax, 1 ; 1 is the system identifier for sys_exit 
     mov ebx, 0 ; exit code 
     int0x80 ; interrupt to invoke the system call 

     jmp int0x80 ; jump to typo indefinitely 

NASM支持無結腸標籤,我經常使用的數據聲明:

error_msg db "Ooops", 0 
flag  db 0x80 
nullpointer dd 0 
0

你需要把一個冒號這行的末尾:

Segment .text: 

global _start 
_start: 
    mov eax, 1 
    mov ebx, 0 
    int0x80 ; no space...