2015-01-20 90 views
-4

我在x86 Linux上使用汇编,并带有Intel 8086的说明。我的程序有问题,它应该计算Fibbonacci序列的元素。汇编语言和打印结果。 Fibbonacci序列

这个节目,我有ARGS运行,为如:

./fibb 1 2 3

拿什么:序列的第一个元素是1,2ST元素是2,我们要获得第三名。 这例子的伟大工程,但是当我试图像运行:

./fibb 1 2 4

然后我有一些垃圾。 请帮帮我。我是Assembly的新成员,请清楚解释我做错了什么。 这里是我的代码:

.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 
+0

如果你能够编写这么大的代码,你应该可以调试它。我会建议在调试器中运行程序([EDB](http://codef00.com/projects)对于初学者来说是一个很好的调试器),并找到错误。 – johnfound 2015-01-20 18:50:21

+0

我不这么认为 - 我只在大会上写了一个程序。 – 2015-01-20 19:00:04

+0

是的,调试它,看看它做什么你不打算。另外,注释你的代码。我们(甚至你自己)无法知道你想要对每条指令做什么。哦,是的,也使用更好的标签。 'ety#'不是很好。 – Jester 2015-01-20 19:15:35

回答

0

这个代码理应充满EAX与第一个参数(一个数字,而不是一个地址)。

mov eax, [ebp+8] 
call ety1 

但在ety1这个代码的字节在地址EAX与BH比较。

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

这是怎么回事?

编辑

通过我通过跳我完全忘了,其实这是一个应用程序,而不是只是一些子程序的可笑的长级联来到这个节目真正的入口点的时间。因此参数是地址,使我的答案无效!

EDIT2

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