【问题标题】:Assembly/Nasm - Segmentation fault (core dumped) error装配/Nasm - 分段错误(核心转储)错误
【发布时间】:2014-12-20 02:43:35
【问题描述】:

我对 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

感谢您的帮助!

【问题讨论】:

  • 你是如何组装它的?你是如何尝试运行它的?您使用的是什么操作系统?
  • nasm -f elf binario.asm //// -m elf_i386 binario.o io.o -o binario //// Ubuntu,应该已经发布了。谢谢!

标签: assembly nasm masm


【解决方案1】:

你得到的汇编代码是用于 DOS 的,但你正在将它组装成一个 ELF 并尝试在 Ubuntu 上运行它。 NASM 会以任何一种方式组装它(它正在完成它的工作,将程序集转换为机器代码),但 Ubuntu 将无法理解结果。

如果你想运行该代码,让 NASM 组装一个带有com 文件扩展名的平面二进制文件,然后在 DOSBox 或虚拟机或其他东西中运行它。

【讨论】:

  • 谢谢。不过,与 Ubuntu 有什么区别?据我所知,在 Ubuntu 上运行的 NASM 示例具有或多或少相同的指令和内容。另外,我可以通过什么方式对其进行编辑以适应 Ubuntu?我真的需要在没有虚拟机的情况下在那里运行它
  • @Kotoriii:它将使用 Linux 系统调用,通过INT 80H 访问,而不是您当前使用的21H 中断。但是你不能只替换这些数字,因为他们希望他们的参数在不同的寄存器中等等。此外,DOS 是 16 位的,而 Linux 应用程序将是 32 位或 64 位的。
  • 相信!非常感谢
  • "io.mac" 看起来像已故的 Sivarama Dandamudi 的东西。如果你搜索他的名字,你可以找到他的书和文件。有适用于 DOS、Windows(?) 和 Linux 的版本。确保你有 Linux 版本!我认为您会这样做,否则“io.o”将被命名为“io.obj”。你的 Masm->Nasm 翻译也有错误。 [ILLEGAL] 将是变量的“[contents]”。您想要地址 - Masm 的 offset ILLEGAL 只是 Nasm 中的 ILLEGAL。对于 Linux,mov ecx, ILLEGAL - 没有括号。
  • @Frank:我不认为INT 21H 可以在 Linux 上运行,不管你是什么%include
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 2014-08-04
  • 2012-11-19
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多