assembly 我的代码中的分段错误在哪里?[重复]

goucqfw6  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(86)

此问题在此处已有答案

call subroutines conditionally in assembly(4个答案)
Nasm segmentation fault on RET in _start(1个答案)
Does it matter where the ret instruction is called in a procedure in x86 assembly(3个答案)
10天前关门了。
我一直在使用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

kr98yfug

kr98yfug1#

问题是我的标签,我不需要从他们ret,因为我没有叫他们。

相关问题