汇编语言和打印结果。斐波那契序列

时间:2015-01-20 18:38:31

标签: linux assembly x86 intel-syntax

我在x86 Linux上使用Assembly,带有Intel 8086的说明。我的程序有问题,应该计算Fibbonacci序列的元素。

这个程序我用args运行,例如:

./ fibb 1 2 3

意味着什么:序列的第一个元素是1,第二个元素是2,我们想获得第三个元素。 这个例子效果很好,但是当我尝试运行时:

./ fibb 1 2 4

然后我有一些垃圾。 请帮我。我是大会的新人,所以请清楚地向我解释我做错了什么。 这是我的代码:

.intel_syntax noprefix
    .global _start

    .data
var1:
    .ascii "To few args\n"
    .equ    len1, $-var1
var2:
    .ascii "Wrong data\n"
    .equ    len2, $-var2
var3:
    .ascii "wrong element of the sequence\n"
    .equ    len3, $-var3
var4:
    .ascii "element of the sequence is higher than 255\n"
    .equ    len4, $-var4
var5:
    .ascii "result: "
    .equ    len5, $-var5
var6: 
    .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
var7: 
    .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
var8:
    .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

    .text
_start:


    jmp     ety5

ety1:
    mov bh, 48
ety2:
    cmp [eax], bh
    jz  ety3
    inc bh
    cmp bh, 58
    jnz ety2
ety3:
    cmp bh, 58
    jnz ety4
    inc     bl
ety4:
    inc eax
    cmp [eax], byte ptr 0
    jnz     ety1
    ret
ety5:

    jmp     ety10

invert:
    xor     ecx, ecx
ety6:
    inc     ebx
    inc     ecx
    cmp     [ebx], byte ptr 0
    jnz     ety6
ety7:
    dec ecx
    cmp ecx, 0
    jz  ety8
    mov [ebx], byte ptr 0
    dec     ebx
    cmp [ebx], byte ptr 48
    jz  ety7
    inc ebx
ety8:
    inc     ecx
    push    ecx
    xor     ecx, ecx
ety9:
    add     ecx, 2
    push    ecx
    dec     ebx
    mov     cl, [eax]
    mov     ch, [ebx]
    mov     [eax], ch
    mov     [ebx], cl
    inc     eax
    pop     ecx
    cmp     ecx, [ebp-8]
    jb  ety9
    pop     ecx
    ret
ety10:

    jmp ety18

ety11:
    mov cl, [eax]
    cmp cl, 0
    jz  ety12
    sub cl, 48  
ety12:
    mov ch, [ebx]
    sub ch, 48
    add     cl, dh
    xor dh, dh
    add cl, ch
    cmp cl, 10
    jb  ety13
    inc     dh
    sub     cl, 10
ety13:
    add cl, 48  
    mov     [eax], cl
    inc eax
    inc ebx
    cmp [ebx], byte ptr 0
    jnz ety11
ety14:
    cmp [eax], byte ptr 0
    jz  ety16
    mov cl, [eax]
    sub cl, 48
    add cl, dh
    xor dh, dh
    cmp cl, 10
    jb  ety15
    sub cl, 10
    inc dh
ety15:
    add cl, 48
    mov [eax], cl
    inc eax
    jmp ety14
ety16:
    cmp dh, 0
    jz  ety17
    add dh, 48
    mov [eax], dh
ety17:
    ret
ety18:

    jmp ety22
ety19:
    mov esi, 3
    mov     dl, [ecx]
    sub     dl, 48
    loop_mul:
    add dl, dl
    dec esi
    cmp esi,0
    jnz loop_mul
    add dl, dh
    xor dh, dh
    cmp dl, 10
    jb  ety20
    sub dl,10
    inc dh
ety20:  
    add     dl, 48
    mov     [ebx], dl
    inc ebx
    inc ecx
    cmp [ecx], byte ptr 0
    jnz     ety19
    cmp dh, 0
    jz  ety21
    add dh, 48
    mov [ebx], dh
ety21:
    mov ebx, offset var8
    ret
ety22:

    mov     ebp, esp

    mov eax, [ebp+8]
    cmp eax, 0
    jnz ety23
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var1
    mov     edx, offset len1
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80
ety23:
    mov eax, [ebp+12]
    cmp eax, 0
    jnz ety24
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var1
    mov     edx, offset len1
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80
ety24:
    mov eax, [ebp+16]
    cmp eax, 0
    jnz ety25
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var1
    mov     edx, offset len1
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80
ety25:

    xor bl, bl
    mov eax, [ebp+8]
    call    ety1
    mov eax, [ebp+12]
    call    ety1
    cmp     bl, 0
    jz  ety26
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var2
    mov     edx, offset len2
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80
ety26:

    mov eax, [ebp+16]
    call    ety1
    cmp     bl, 0
    jz  ety27
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var3
    mov     edx, offset len3
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80
ety27:


    xor     ebx, ebx;
    mov eax, [ebp+16]
    mov ecx, [ebp+16]

ety28:
    cmp [eax], byte ptr 0
    jnz ety29
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var3
    mov     edx, offset len3
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80
ety29:
    cmp [eax], byte ptr 48
    jnz ety30
    inc eax
    inc ecx
    jmp     ety28

ety30:
    inc ecx
    cmp [ecx], byte ptr 0
    jz  ety35
    inc ecx
    cmp [ecx], byte ptr 0
    jz  ety33
    inc ecx
    cmp [ecx], byte ptr 0
    jz  ety31
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var4
    mov     edx, offset len4
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80

ety31:
    mov cl, [eax]
    inc     eax
    sub cl, 48
ety32:
    add ebx, 100
    dec     cl
    cmp     cl, 0
    jnz     ety32
ety33:
    mov cl, [eax]
    inc     eax
    sub cl, 48
    cmp     cl, 0
    jz  ety35
ety34:
    add ebx, 10
    dec     cl
    cmp     cl, 0
    jnz     ety34
ety35:
    mov cl, [eax]
    sub cl, 48
    cmp     cl, 0
    jz  ety37
ety36:
    inc ebx
    dec     cl
    cmp     cl, 0
    jnz     ety36
ety37:

    cmp     ebx, 256
    jb  ety38
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var4
    mov     edx, offset len4
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80
ety38:

    xor dl, dl
ety39:
    dec ebx
    inc     dl
    cmp ebx,0
    jnz ety39

    mov eax, [ebp+12]
    mov ebx, offset var7
    xor dh, dh
ety40:
    inc     eax
    inc dh
    cmp [eax], byte ptr 0
    jnz ety40
    xor ch, ch
ety41:
    dec eax
    dec dh
    mov cl, [eax]
    mov [ebx], cl
    inc ebx
    cmp dh, 0
    jnz ety41

    mov eax, [ebp+8]
    mov ebx, offset var6
ety42:
    inc     eax
    inc dh
    cmp [eax], byte ptr 0
    jnz ety42
ety43:
    dec eax
    dec dh
    mov cl, [eax]
    mov [ebx], cl
    inc ebx
    cmp dh, 0
    jnz ety43


    cmp     dl, 1
    jnz ety44
    jmp ety46
ety44:

    dec dl
    cmp     dl, 1
    jnz ety45
    jmp ety47


ety45:
    dec     dl

    cmp dl, 0
    jz  ety47
    push    edx
    xor dh, dh
    mov     eax, offset var6
    mov     ebx, offset var8
    mov     ecx, offset var7
    call    ety19
    xor dh, dh  
    call    ety11
    pop edx
    dec     dl
    cmp dl, 0
    jz  ety46
    push    edx
    xor dh, dh
    mov     eax, offset var7
    mov     ebx, offset var8
    mov     ecx, offset var6
    call    ety19
    xor dh, dh  
    call    ety11
    pop edx
    jmp     ety45

ety46:
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var5
    mov     edx, offset len5
    int     0x80
    mov     eax, offset var6
    mov     ebx, offset var6
    call    invert
    mov     eax, 4
    mov ebx, 1
    mov     edx, ecx
    mov ecx, offset var6
    int     0x80
    mov     eax, 4
    mov ebx, 1
    mov [ecx], byte ptr 10
    mov     edx, 1
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80

ety47:
    mov     eax, 4
    mov ebx, 1
    mov ecx, offset var5
    mov     edx, offset len5
    int     0x80
    mov     eax, offset var7
    mov     ebx, offset var7
    call    invert
    mov     eax, 4
    mov ebx, 1
    mov     edx, ecx
    mov ecx, offset var7
    int     0x80
    mov     eax, 4
    mov ebx, 1
    mov [ecx], byte ptr 10
    mov     edx, 1
    int     0x80
    mov     eax, 1
    mov ebx, 0
    int     0x80

    int     0x80

1 个答案:

答案 0 :(得分:0)

这段代码应该用第一个参数(一个数字,而不是地址)填充EAX。

mov eax, [ebp+8]
call    ety1

但是在 ety1 ,此代码将地址EAX 处的字节与BH进行比较。

ety1:
 mov bh, 48
ety2:
 cmp [eax], bh
 jz  ety3

这是怎么回事?

修改

当我通过一个可笑的长级跳跃到达该程序的真正入口点时,我完全忘记了这实际上是一个应用程序,而不仅仅是一些子程序。因此,参数是地址,使我的答案无效!

EDIT2

您确定DS寄存器已正确初始化吗?