1

私はアセンブリを学び、GNU ASで使用するためのAT&T構文のプログラムを持っています。これは動作するはずです。GDBでこのエラーが発生します:

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

コードは次のとおりです。

.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

私が本当に明らかに間違ったことはありますか?そして、アセンブリを学ぶためにどのリソースをお勧めしますか?(私はWikiBooks X86アセンブリの記事とほとんどのGASマニュアルを読みました)。

4

3 に答える 3

7

これ:

movl    (str1), %esp

クラッシュの直接の原因である可能性があります。%esp で引数が渡された x86 システムで作業したことはありません。一般的に言えば、%esp は常に有効なスタック ポインターである必要があります。多くのシステムでは、呼び出し命令の時点で、いくつかのアラインメント保証を追加で満たす必要があります。

str1 のメモリの内容を %esp が指すメモリの場所に移動するつもりだと思いますが、これはそうではありません。デバッガーでプログラムの実行を命令ごとにステップ実行することで、それを確認できるはずです。これは、アセンブリを記述するために習得する重要なスキルであり、このような問題を自分で見つけるのに役立ちます。

于 2009-09-13T16:19:53.303 に答える
0

stephentyrone の回答に加えて、strlen を gcc -S で呼び出す C プログラムをコンパイルし、生成された .s ファイルを観察して、システムで適切な呼び出し規則を確認することをお勧めします (彼が言ったように、%esp で値を渡すことできません) 。 %eax で単一の引数が渡されることを期待していましたが、しばらく経ち、その間 amd64 ABI を使用していたため、混乱する可能性があります)。

于 2009-09-13T17:15:39.397 に答える
0

A fairly normal, safe way to pass a parameter to a subroutine is to push the parameter value onto the stack.

A slightly quicker way is to move the the parameter value into a register: but not, as stephen says, the esp register (the push, pop, call, and ret opcodes assume that esp contains a pointer to the stack).

于 2009-09-13T17:25:03.513 に答える