Assembly/Nasm - 段错误(核心转储)错误

标签 assembly nasm masm

我是 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 或虚拟机或其他东西中运行它。

关于Assembly/Nasm - 段错误(核心转储)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36421476/

相关文章:

assembly - x86 汇编中 "align"关键字/指令的语法是什么?

c - x86 程序集 : INC and DEC instruction and overflow flag

linux - 32 位模式下的 NASM x86_64 程序集 : Why does this instruction produce RIP-Relative Addressing code?

assembly - MASM 中的@data 指令是什么意思?

string - 在 masm 中解码十六进制(空字节问题)

c++ - 简单的 x86-64 C++ 内联汇编 "Hello World"示例

assembly - NASM 中的两个数字相除

assembly - Linux 64 位 shellcode

assembly - 在 MASM 中使用常量初始化大数

assembly - PCIe 卡上的计算机