为什么NA​​SM:macOS中的分段错误11?

时间:2017-11-13 13:14:36

标签: macos assembly x86 nasm

我正在MacOS上学习NASM。

我正在尝试使用函数实现单行间距,但我遇到了问题。

    SYS_EXIT    equ 0x1
    SYS_READ    equ 0x3
    SYS_WRITE   equ 0x4
    STDIN       equ 0x1
    STDOUT      equ 0x2

segment .data

    Msg_Line        db " ", 0xA, 0x0
    Len_Line        equ $ - Msg_Line

    Msg_FirstPrompt     db "Enter a Digit : "
    Len_FirstPrompt     equ $ - Msg_FirstPrompt

    Msg_SecondPrompt    db "Please Enter a Second Digit : "
    Len_SecondPrompt    equ $ - Msg_SecondPrompt

    Msg_ThirdPrompt     db "The Sum is : "
    Len_ThirdPrompt     equ $ - Msg_ThirdPrompt

segment .bss

    Bss_FirstNumber     resb    2
    Bss_SecondNumber    resb    2
    Bss_Result      resb    1

section .text

    global  EntryPoint

Endl:

    push    dword   eax

    push    dword   Len_Line
    push    dword   Msg_Line
    push    dword   STDOUT

    sub esp,    0x4
    mov eax,    SYS_WRITE

    int 0x80

    pop eax 

    ret 0x4

EntryPoint:

    ; Print First Prompt
    push    dword   Len_FirstPrompt
    push    dword   Msg_FirstPrompt
    push    dword   STDOUT

    sub esp,    0x4
    mov eax,    SYS_WRITE

    int 0x80

    ; Get Digit
    push    dword   2
    push    dword   Bss_FirstNumber
    push    dword   STDIN

    sub esp,    0x4
    mov eax,    SYS_READ

    int 0x80

    ; Print Second Prompt
    push    dword   Len_SecondPrompt
    push    dword   Msg_SecondPrompt
    push    dword   STDOUT

    sub esp,    0x4
    mov eax,    SYS_WRITE

    int 0x80

    ; Get Second Digit
    push    dword   2
    push    dword   Bss_SecondNumber
    push    dword   STDIN

    sub esp,    0x4
    mov eax,    SYS_READ

    int 0x80

    ; +operator use
    mov eax,    [Bss_FirstNumber]
    sub eax,    '0'

    mov ebx,    [Bss_SecondNumber]
    sub ebx,    '0'

    add eax,    ebx
    add eax,    '0'

    mov [Bss_Result], eax

    ; Print Result Prompt
    push    dword   Len_ThirdPrompt
    push    dword   Msg_ThirdPrompt
    push    dword   STDOUT

    sub esp,    0x4
    mov eax,    SYS_WRITE

    int 0x80

    ; Print Result
    push    dword   1
    push    dword   Bss_Result
    push    dword   STDOUT

    sub esp,    0x4
    mov eax,    SYS_WRITE

    int 0x80

    sub esp,    0x4
    call    Endl
    ;push   dword   Len_Line
    ;push   dword   Msg_Line
    ;push   dword   STDOUT

    ;sub    esp,    0x4
    ;mov    eax,    SYS_WRITE

    ;int    0x80

    ; Exit Program
    sub esp,    0x4
    mov eax,    SYS_EXIT

    int 0x80

结果:

Enter a Digit : 3
Please Enter a Second Digit : 2
The Sum is : 5 
Segmentation fault: 11

如果您只是在不将其作为函数调用的情况下编写它,它就可以正常工作。

如果我将其作为函数实现,为什么会出现错误?

0 个答案:

没有答案