x86汇编,误导性错误

1 x86 assembly gnu-assembler

我正在尝试学习汇编,并有一个AT&T语法的程序,用于GNU AS,我认为应该可行.我收到GDB的错误:

Program received signal SIGSEGV, Segmentation fault.
.PROGRAM () at concatenator.s:60
60              call    strlen
Current language:  auto; currently asm
Run Code Online (Sandbox Code Playgroud)

代码是:

.file "concatenator.s"
.globl _start
.section .text
strlen:
 mov %esp, (str1)
 push %ebx
 push %ecx
 push %edx

        mov $1, %edi
        sub     %ecx, %ecx
        sub     %al, %al
        not     %ecx
        cld
        repne   scasb
        not     %ecx
        dec     %ecx
 mov %ecx, %eax



 pop %edx
 pop %ecx
 pop %ebx
 leave
 ret
write:
 push %eax
 push %ebx
 push %ecx
 push %edx
 mov %eax, %ecx
 mov $4, %eax
 mov $4, %edx
 mov $2, %ebx
 int $0x80
 pop %edx
 pop %ecx
 pop %ebx
 pop %eax
 ret

.globl concatenate
concatenate:
 pop %eax
 mov %eax, (str2)
 pop %eax
 mov %eax, (str1)
 push  %ebx
 push %ecx
 push %edx
        pushl   %ebp#Pushes Previous programs local vars to the stack.
        movl    %esp, %ebp
        subl    $24, %esp
.PROGRAM:
        movl    (str1), %esp#Moves str1 to ESP
 call    strlen#//Strlen counts len of ESP
        movl    %eax, -16(%ebp)#//Moves eax[Return] into ebp[-16](len)
        movl    $str2, (%esp)#//Moves str2 to ESP
        call    strlen#//Counts len of ESP
        subl    $1, %eax#//Removes one from the return value
        movl    %eax, -12(%ebp)#//Stores return in INT len2
        //movl  -12(%ebp), %eax
        movl    %eax, -8(%ebp)#//Stores return in INT J
        movl    $0, -4(%ebp)##//INT X = 0
        jmp     .L7
.L8:
        addl    $1, -8(%ebp)#//ADDS 1 to J
        movl    -8(%ebp), %eax#//Moves J to EAX
        movl    -4(%ebp), %edx#//MOVES X TO EDX
        movzbl  str1(%edx), %edx#//Moves str1[EDX] (EDX is X) to EDX and fills wit null
        movb    %dl, str2(%eax)#//Moves one byte, (Tbhe character we just copied) into str2 [EAX]
        addl    $1, -4(%ebp)#//INT X++
.L7:
        movl    -4(%ebp), %eax#//Moves INT X to EAX
        cmpl    -16(%ebp), %eax#//Compares len with EAX
        jl      .L8#//While below length of string one, go to L8 and copy str1 to str2
        addl    $1, -8(%ebp)#//Adds one to J(J++)
        movl    -8(%ebp), %eax#//Moves J to EAX
        movb    $0, str2(%eax)#//Adds null character to string at position J.
.RETURN:
 leave
        pop %edx
 pop %ecx
 pop %ebx
 mov (str2), %eax
 ret

_start:
push str1
push str2
call concatenate
mov %eax, str2 
mov $1, %eax
mov $0, %ebx
int $0x80


.globl str1
.section .data
str1:
        .string "DEF"
        .zero   252
str2:
        .string "ABC"
        .zero   252
Run Code Online (Sandbox Code Playgroud)

有什么我真的明显做错了吗?你会推荐哪些资源来学习装配?(我已经阅读了WikiBooks X86汇编文章和大部分GAS手册).

Ste*_*non 7

这个:

movl    (str1), %esp
Run Code Online (Sandbox Code Playgroud)

可能是您崩溃的直接原因.我从未使用过在%esp中传递参数的x86系统; 一般来说,%esp必须始终保持有效的堆栈指针.在许多系统上,它必须在呼叫指令时另外满足一些对齐保证.

我假设您打算将str1的内存内容移动到%esp指向的内存位置,但这不是它的作用.您应该能够通过逐个指令逐步执行调试程序中的程序执行来查看.这是学习编写汇编的重要技能,可以帮助您自己找到这样的问题.

  • 只是为了解释你为什么看到SIGSEGV错误,这是因为调用将下一条指令的地址压入堆栈; 因为你刚刚将一些任意数据加载到%esp中,所以它没有指向有效的堆栈地址,因此将返回地址(存储到str1,解释为内存位置)的尝试导致SIGSEGV. (2认同)