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