【发布时间】:2011-06-26 05:04:37
【问题描述】:
我开始学习如何使用 NASM 汇编编程语言编写程序。我编写了这个简单的程序,提示用户输入两个数字,然后将两个操作数相加。我让它编译没有错误或警告,但是当它提示用户输入两个数字并开始添加这两个数字时,它会打印出分段错误并且程序结束。我知道分段错误相当于 Win32 世界中的访问读/写冲突异常。但是,因为我不知道如何调试 NASM 代码;我不知道出了什么问题。我怀疑它与无效指针有关;但我不知道。下面是代码:
section .data
msg1: db 'Please Enter A Number: ', 0
length1: equ $ - msg1
msg2: db 'Please Enter A Second Number: ', 0
length2: equ $ - msg2
section .bss
operand1: resb 255
operand2: resb 255
answer: resb 255
section .text
global _start
_start:
; Print first message
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, length1
int 80h
; Now read value
mov eax, 3
mov ebx, 1
mov ecx, operand1
mov edx, 255
int 80h
; Print second message
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, length2
int 80h
; Now read second value
mov eax, 3
mov ebx, 1
mov ecx, operand2
mov edx, 255
int 80h
; Now add operand1 and operand2 and print answer
mov eax, 4
mov ebx, 1
xor ecx, ecx ; Make the ecx register 0
mov ecx, operand1
add ecx, operand2
mov edx, 510
int 80h
【问题讨论】:
-
在 gdb 中运行你的代码,看看它在哪条指令上崩溃
-
如何使用 gdb 调试我的代码?抱歉,Linux 新手。
标签: linux ubuntu segmentation-fault nasm