尝试在NASM中反转字符串时出现分段错误

时间:2016-04-14 14:18:31

标签: string segmentation-fault nasm

我试图在NASM中编写汇编语言,我在字符串上进行各种操作 我正在做的其中一项操作是反向字符串 我想做的是让rsi指向字符串的末尾,然后将其内容复制到另一个由rdi指向的地方。
当我试图将rsi指向的内容移动到al寄存器时,问题就出现了。

以下是代码:

section .data
    menu db "MENU",10
        db "--------------",10
        db "1.Accept String",10
        db "2.Reverse String",10
        db "3.Terminate",10
        db "--------------",10
        db "Enter your choice:",10
    menu_len equ $-menu

    msg1 db "Enter a string",10
    msg1_len equ $-msg1

    msg2 db "The reversed string is..",10
    msg2_len equ $-msg2

    msg3 db "Wrong Choice!",10
    msg3_len equ $-msg3

    newl db 10
    l equ $-newl

section .bss
    string1 resb 20
    string2 resb 20
    strlen resb 1
    strlen2 resb 1
    choice resb 2

    %macro disp 2
    mov rax,4
    mov rbx,1
    mov rcx,%1
    mov rdx,%2
    int 80h
    %endmacro

    %macro accept 2
    mov rax,3
    mov rbx,0
    mov rcx,%1
    mov rdx,%2
    int 80h
    %endmacro

section .text
    global _start

_start:
    disp menu,menu_len
    accept choice,2

    cmp byte[choice],'1'
    je case1
    cmp byte[choice],'2'
    je case2
    cmp byte[choice],'3'
    je exit
    disp msg3,msg3_len
    jmp _start

case1:
    disp msg1,msg1_len
    accept string1,20
    dec rax
    mov [strlen],rax
    jmp _start

case2:
    mov bl,byte[strlen]
    mov byte[strlen],bl
    mov rsi,string1
    add rsi,[strlen]
    dec rsi
    mov rdi,string2
    mov rcx,[strlen]
l1:
    mov al,[rsi]    ;<- Getting a segmentation fault here
    mov [rdi],al
    dec rsi
    inc rdi
    loop l1
    disp msg2,msg2_len
    disp string2,20
    disp newl,l
    jmp _start

exit:
    mov rax,1
    mov rbx,0
    int 80h 

0 个答案:

没有答案