在汇编语言程序中找不到错误

时间:2016-04-22 11:48:28

标签: linux assembly x86

我在此汇编代码中找不到错误:

extern accept1,str1,concate,str2,substring

section .data

msg db "1.Concate 2 strings",10,"2.Find substring",10,"3.Exit",10,"Enter choice: "
msglen equ $-msg

section .bss
cnt resd 1
choice resb 1

%macro read 2
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro print 2
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro

section .text
global _start

_start:

begin:
    print msg,msglen
    read choice,1

    cmp byte[choice],31h
    jne next
    call accept1
    call concate

next:
    cmp byte[choice],32h
    jne next
    call accept1
    call substring

exit:
    cmp byte[choice],33h
    jne begin

mov eax,1
mov ebx,0
int 80h 

这是代码的第二部分:

global accept1,concate,str1,str2,substring

section .data
msg1 db "Enter the 1st string: "
msg1len equ $-msg1

msg2 db "Enter the 2nd string: "
msg2len equ $-msg2

nl db " ",10
nllen equ $-nl

section .bss
str1 resb 15
str2 resb 15
strlen1 resb 15
strlen2 resb 15
count resb 1
temp resb 100

%macro read 2
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro print 2
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro

section .text

accept1:
    print msg1,msg1len
    read str1,15
    dec al
    mov [strlen1],al

    print msg2,msg2len
    read str2,15
    dec al
    mov [strlen2],al
    ret

concate:
    cld
    mov esi,str2
    mov edi,str1
    add edi,[strlen1]
    mov ecx,[strlen2]

    rep movsb

    mov eax,[strlen1]
    add eax,[strlen2]
    mov [temp],eax
    print str1,15
    print nl,nllen
    ret

substring:
    CLD
    mov byte[count],00
    mov esi,str2
    mov edi,str1
    mov ebp,edi
    mov eax,[strlen1]
    sub eax,[strlen2]
    inc eax

up:
    mov ecx,[strlen2]
    repe cmpsb

    jnz next
    inc byte[count]

next:
    inc ebp
    mov edi,ebp
    mov esi,str2
    dec eax
    jnz up
    add byte[count],30h
    print count,1
    print nl,nllen
    ret

这是一个实施远程程序的程序。执行时,程序进入无限循环。这是输出的快照:out.png

1 个答案:

答案 0 :(得分:0)

next:
 cmp byte[choice],32h
 jne next

如果输入不是“2”,那么这将变成无限循环。这需要成为:

next:
 cmp byte[choice],32h
 jne exit
  

它直接跳转到“输入第二个字符串:”

您的代码结构不正确。在连接或子串搜索后,您需要jmp标签开始或者到程序的真正结束。

begin:
    print msg,msglen
    read choice,1

    cmp byte[choice],31h
    jne next
    call accept1
    call concate
    jmp OverAndOut

next:
    cmp byte[choice],32h
    jne exit
    call accept1
    call substring
    jmp OverAndOut

exit:
    cmp byte[choice],33h
    jne begin

OverAndOut:
    mov eax,1
    mov ebx,0
    int 80h