[英]Assembly/Nasm - Segmentation fault (core dumped) error
我對 NASM 非常陌生,我正在嘗試在線執行我發現的 MASM 示例,但是轉換為 NASM 一直很痛苦。
它正確編譯並生成輸出文件,但是當我嘗試運行它時,它給出了分段錯誤(核心轉儲錯誤),我不知道它是什么。 操作系統是 Ubuntu,嘗試在以下條件下執行編譯:
nasm -f elf binario.asm
ld -m elf_i386 binario.o io.o -o binario
這是代碼:
%include "io.mac"
.DATA
PROMPT_1 DB 0DH,0AH,'Enter the first binary number ( max 8-digits ) : $'
PROMPT_2 DB 0DH,0AH,'Enter the second binary number ( max 8-digits ) : $'
PROMPT_3 DB 0DH,0AH,'The SUM of given binary numbers in binary form is : $'
ILLEGAL DB 0DH,0AH,'Illegal character. Try again.$'
.CODE
.STARTUP
JMP start2 ; jump to label @START_2
start1: ; jump label
MOV DX, [ILLEGAL] ; load and display the string ILLEGAL
MOV AH, 9
INT 21H
start2: ; jump label
XOR BX, BX ; clear BX
MOV DX, [PROMPT_1] ; load and display the string PROMPT_1
MOV AH, 9
INT 21H
MOV CX, 8 ; initialize loop counter
MOV AH, 1 ; set input function
loop1: ; loop label
INT 21H ; read a character
CMP AL, 0DH ; compare AL with CR
JNE skip1 ; jump to label @SKIP_1 if AL!=0DH
CMP CX, 8 ; compare CX with 8
JE start1 ; jump to label @START_1 if CX=8
JMP exitloop1 ; jump to label @EXIT_LOOP_1
skip1: ; jump label
AND AL, 0FH ; convert ascii into decimal code
SHL BL, 1 ; shift BL towards left by 1 position
OR BL, AL ; set the LSB of BL with LASB of AL
LOOP loop1 ; jump to label @LOOP_1 if CX!=0
exitloop1: ; jump label
MOV DX, [PROMPT_2] ; load and display the string PROMPT_2
MOV AH, 9
INT 21H
MOV CX, 8 ; initialize loop counter
MOV AH, 1 ; set input function
loop2: ; loop label
INT 21H ; read a character
CMP AL, 0DH ; compare AL with CR
JNE skip2 ; jump to label @SKIP_2 if AL!=0DH
CMP CX, 8 ; compare CX with 8
JE start2 ; jump to label @START_2 if CX=8
JMP exitloop2 ; jump to label @EXIT_LOOP_2
skip2: ; jump label
AND AL, 0FH ; convert ascii into decimal code
SHL BH, 1 ; shift BH towards left by 1 position
OR BH, AL ; set the LSB of BH with LASB of AL
LOOP loop2 ; jump to label @LOOP_2 if CX!=0
exitloop2: ; jump label
MOV DX, [PROMPT_3] ; load and display the string PROMPT_3
MOV AH, 9
INT 21H
ADD BL, BH ; add BL and BH
JNC skip ; jump to label @SKIP if CF=1
MOV AH, 2 ; print the digit 1 i.e. carry
MOV DL, 31H
INT 21H
skip: ; jump label
MOV CX, 8 ; initialize loop counter
MOV AH, 2 ; set output function
loop3: ; loop label
SHL BL, 1 ; shift BL towards left by 1 position
JC one ; jump to label @ONE if CF=1
MOV DL, 30H ; set DL=0
JMP display ; jump to label @DISPLAY
one: ; jump label
MOV DL, 31H ; set DL=1
display: ; jump label
INT 21H ; print the character
LOOP loop3 ; jump to label @LOOP_3 if CX!=0
MOV AH, 4CH ; return control to DOS
INT 21H
done:
.EXIT
感謝您的幫助!
您在那里獲得的匯編代碼適用於 DOS,但您正在將其組裝到 ELF 並嘗試在 Ubuntu 上運行它。 NASM 會以任何一種方式組裝它(它正在做它的工作,將組裝轉換為機器代碼),但 Ubuntu 將無法理解結果。
如果你想運行那個代碼,讓 NASM 組裝一個帶有com
文件擴展名的平面二進制文件,然后在 DOSBox 或虛擬機或其他東西中運行它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.