0

I'm trying to make a really simple NASM program that will output the first value in my array.

When I run it, I get a Segmentation Fault. I can't figure out why. The value in the array is a byte, and the length I am putting into the edx register is 1. Why is there a fault?

segment .data
    array: db 2, 9, 6, 7, 1, 4

segment .bss

segment .text
    global main

main:
    mov eax, 4
    mov ebx, 1
    mov ecx, [array]
    mov edx, 1
    int 0x80 
4

1 回答 1

2

你得到一个段错误,因为你没有正确结束程序。它正在进入无人区!

mov   eax, 1
xor    ebx, ebx
int     80H

是退出程序的正确方法。此外,您没有打印您期望的内容。数组中的那些数字不是 ASCII,您需要在代码中转换为 ASCII,或者只用引号括起来。

array    db  "2", "9", "6", "7", "1", "4"

此外, sys_write 期望和地址不是一个值,删除数组周围的括号

* 编辑 *

%define sys_exit    1
%define sys_write   4
%define stdout      1

section .data
array       db  "2", "9", "6", "7", "1", "4"

section .text
global main
main:
    mov     eax, sys_write
    mov     ebx, stdout
    mov     ecx, array
    mov     edx, 1
    int     80H

    mov     eax, sys_exit                
    xor     ebx, ebx                      
    int     80h   
于 2013-04-22T01:58:59.787 回答