0

I try to sort using x86 assembly language. I use scanf for taking in the values but I am not able to limit the number of elements to take. That is, my output is:

Enter the number of elements less than 10:
4
Enter the elements :
1
N value is 1
2
N value is 1
3
N value is 1
4
N value is 1
5
N value is 1
6
N value is 1

The problem is that it is not stopping after 4.

    extern printf
    extern scanf
    SECTION .data
    temp: dd 0
    n: dd 0
    i: dd 0
    j: dd 0
    k: dd 0
    l: dd 0

    ini: dd 10
    section .bss
       ;X      resw    1
       in1  resd    1
       in2  resd    1       

    SECTION .text
    global main
main:
    push ecx
    push dword fmt0
    call printf
    add esp, 4
    pop ecx
    push edx

    push in1
    push dword fmt1
    call scanf
    add esp, 8
    pop edx
    mov eax,[in1]
    mov [n],eax

    ;push ecx
    ;push dword [n]
    ;push dword fmt6
    ;call printf
    ;add esp, 8
    ;pop ecx

    push ecx
    push dword fmt2
    call printf
    add esp, 4
    pop ecx
    jmp L$2

L$1:
    mov  eax,[j]
    mov  ebx,1
    mov ecx,eax
    add ecx,ebx
    push edx
    push dword ecx
    push dword fmt6
    call printf
    add esp, 8
    pop edx 

L$2:
    mov  eax,ecx
    mov  ebx,[n]
    cmp eax , ebx
    jg L$3      
    push eax
        push edx
    push in2
    push dword fmt3
    call scanf
    add esp, 8
    pop edx
    pop eax
    mov ecx,[in2]
    mov [ini],ecx
    jmp L$1

L$3:
    jmp L$5

L$4:
    mov  eax,[k]
    mov  ebx,1
    mov ecx,eax
    add ecx,ebx

L$5:
    mov  eax,[i]
    mov  ebx,[n]
    cmp eax , ebx
    jl L$6
    jmp L$8

L$7:
    mov  eax,1
    mov ebx,eax
    add ebx,eax
    mov  ecx,[i]
    mov edi,ecx
    add edi,ebx

L$8:
    mov  eax,[j]
    mov  ebx,[n]
    cmp eax , ebx
    jl L$9
    mov  edi,ini
    mov  esi,[k]
    mov ecx ,[edi + 4*esi]

    mov  ebx,[i]
    mov eax ,[edi + 4*ebx]
    cmp ecx , eax
    mov eax ,[edi + 4*ebx]
    mov [temp], eax
    mov ecx ,[edi + 4*esi]
    mov [ini + 4*ebx], ecx
    mov [ini + 4*esi], eax

L$11:
    jmp L$7
L$9:
    jmp L$4

L$6:
    push ecx
    push dword fmt4
    call printf
    add esp, 4
    pop ecx
    jmp L$13

L$12:
    mov  eax,[l]
    mov  ebx,1
    mov ecx,eax
    add ecx,ebx

L$13:
    mov  eax,[l]
    mov  ebx,[n]
    cmp eax , ebx
    jl L$14

    push ecx
    push dword [ini]
    push dword fmt5
    call printf
    add esp, 8
    pop ecx
    jmp L$12

L$14:
    ret

fmt0:   db "Enter the number of elements lessthan 10:",10,0
fmt1:   db "%d",0
fmt2:   db "Enter the elements :",10,0
fmt3:   db "%d",0
fmt4:   db "The sorted Array is :",10,0
fmt5:   db "%d",10,0
fmt6:    db "N value is %d",10,0
4

1 回答 1

1

我假设您的意图是用来[j]保存您输入的值的数量,并在每次循环中增加它。您在 处加载值L$1,将其加一并保存在 CX 寄存器中,但您永远不会将修改后的值存储在内存中的任何位置。所以下一次循环,[j]还是0。

您需要在递增后存储该值:

add ecx,ebx
mov [j], ecx  ; Store the incremented value

提示:代码中的一些注释会很有用。也许每个主要标签上的一行都说明了你在做什么?当你下个月回来查看你的代码时,你会很高兴那些在那里。

于 2013-04-15T20:54:29.277 回答