我的代码中的分段错误在哪里?

问题描述 投票:0回答:1

我继续使用 nasm 运行我的代码,并且不断出现分段错误。我认为这是因为我错误地退出了代码,但我已经没有关于如何更改它的想法了。 (x64 组件)

我尝试过在许多标签上移动“ret”,并移动到我称之为退出系统调用的位置。我也尝试过移动我的宏,但没有看到任何进展。

我的主文件:

%include "utils.inc"

section .data
    ;boring begining text
    intro db "Enter 2 numbers and an operation symbol : ", 10, 0
    outro db "Your answer is : "
    number1 db "Num 1 : ", 0
    number2 db "Num 2 : ", 0
    operation db "Operation : ", 0
    error db "Invalid Operation", 0

section .bss
    num1 resb 6 ;reserve 6 characters for the first number
    num2 resb 6 ;reserve 6 charachters for the second numver
    operand resb 1 ;reserve 1 character for the operation

section .text
    global _start

_start:
    PrintString intro

    ;get the first number
    PrintString number1
    mov rax, 0
    mov rdi, 0
    mov rsi, num1
    mov rdx, 2
    syscall

    ;get the second number
    PrintString number2
    mov rax, 0
    mov rdi, 0
    mov rsi, num2
    mov rdx, 2 
    syscall

    ;get the operation
    PrintString operation
    mov rax, 0
    mov rdi, 0
    mov rsi, operand
    mov rdx, 1
    syscall

    mov al, [operand]

    ;if the operation is '*'
    cmp al, 42
    je _mult
    
    ;if the operation is '/'
    cmp al, 47
    je _div

    ;if the operation is '+'
    cmp al, 43
    je _add

    ;if the operation is '-'
    cmp al, 45
    je _sub

    mov rax, 60
    mov rdi, 0
    syscall
    ret

_mult:    
    ret

_div:
    ret

_add:
    ret

_sub:
    ret
    

我的宏:

%macro Exit 0
    mov rax, 60
    mov rdi, 0
    syscall
%endmacro

%macro PrintString 1
    mov rax, %1
    push rax
    mov rbx, 0
    %%Length:
        inc rax
        inc rbx
        mov cl, [rax]
        cmp cl, 0
        jne %%Length

        mov rax, 1
        mov rdi, 1
        mov rsi, %1
        mov rdx, rbx
        syscall
%endmacro

%macro PrintInt 1
    section .bss
        digitSpace resb 3
        digitSpacePos resb 6

    section .text
        mov rax, %1
        mov rcx, digitSpace
        mov rbx, 10
        mov [rcx], rbx
        inc rcx
        mov [digitSpacePos], rcx

        _pushDigitsToStack:
            mov rdx, 0
            mov rbx, 10
            div rbx
            push rax
            add rdx, 48

            mov rcx, [digitSpacePos]
            mov [rcx], dl
            inc rcx
            mov [digitSpacePos], rcx
            
            pop rax
            cmp rax, 0
            jne _pushDigitsToStack


        _printDigits:
            mov rcx, [digitSpacePos]

            mov rax, 1
            mov rdi, 1
            mov rsi, rcx
            mov rdx, 1
            syscall

            mov rcx, [digitSpacePos]
            dec rcx
            mov [digitSpacePos], rcx
            
            cmp rcx, digitSpace
            jge _printDigits
%endmacro


%macro stringToInteger 1
    PLACE equ $10
    CHAR_0 equ $48 

    mov r13, CHAR_0
    mov r14, PLACE
    mov r15, %1
    push r15
    mov rax, 0
    jmp %%createInt

    %%addNum:
        sub cl, [r13]
        mul r14
        add [rax], cl
        jmp %%createInt

    %%createInt:
        inc r15
        mov cl, [r15]
        cmp cl, 0
        jne %%addNum
%endmacro
assembly x86-64
1个回答
0
投票

问题出在我的标签上,我不需要他们

ret
,因为我没有给他们打电话。

© www.soinside.com 2019 - 2024. All rights reserved.