【发布时间】:2014-01-18 12:49:43
【问题描述】:
我是 nasm 新手。 我对nasm有非常基本的了解 我的程序基本上将两个数组作为输入并使用冒泡排序对它们进行排序,然后将它们合并以获得一个新数组(排序) 但是在接受输入时,程序并没有按照我期望的方式工作。 我似乎找不到错误。 我的所有功能都工作正常。“合并”标签可能存在问题。我无法检查
请帮忙
section .data
msg1: db "Enter the size of first array ",10
len1: equ $-msg1
msg2: db "Enter the size of second array ",10
len2: equ $-msg2
msg3: db "Enter the element ",10
len3: equ $-msg3
msg4: db "MERGED ARRAY IS ",10
len4: equ $-msg4
comma: db ","
nline:db 10
section .bss
arr1: resb 100
arr2: resb 100
array: resb 200
d1: resb 1
d0: resb 1
temp: resb 1
num: resb 1
size1: resb 1
size2: resb 1
size: resb 1
i: resb 1
j: resb 1
s: resb 1
r: resb 1
read: resb 1
section .text
global _start:
;-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
read_digit:
mov eax, 3
mov ebx, 0
mov ecx, d1
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, d0
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, temp
mov edx, 1
int 80h
sub byte[d1], 30h
sub byte[d0], 30h
mov al, byte[d1]
mov dl, 10
mul dl
add al, byte[d0]
mov byte[read],al
ret
;---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
read_array:
push rbx
;Printing the message to enter the numbers
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, len3
int 80h
;Reading the number
mov eax, 3
mov ebx, 0
mov ecx, d1
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, d0
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, temp
mov edx, 1
int 80h
sub byte[d1], 30h
sub byte[d0], 30h
mov al, byte[d1]
mov dl, 10
mul dl
add al, byte[d0]
;al now contains the number
pop rbx
mov byte[ebx], al
add ebx, 1
dec byte[r]
cmp byte[r], 0
jg read_array
jmp end
end:
ret
;-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sort:
mov byte[i], 0
i_loop:
mov byte[j], 0
j_loop:
mov ebx, ecx;needs ebx so given it in ecx
movzx eax, byte[j]
add ebx, eax
mov eax, ebx
add eax, 1
mov dl, byte[ebx]
mov dh, byte[eax]
cmp dh, dl
jl swap
jmp no_swap
swap:
mov byte[ebx], dh
mov byte[eax], dl
no_swap:
inc byte[j]
mov al, byte[s]
sub al, byte[i]
sub al, 1
cmp byte[j], al
jl j_loop
inc byte[i]
mov al, byte[s]
cmp byte[i], al
jl i_loop
jmp over
over:
ret
;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, len1
int 80h
call read_digit
mov al,byte[read]
mov byte[size1],al
mov byte[r],al
mov ebx,arr1
call read_array
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, len3
int 80h
call read_digit
mov al,byte[read]
mov byte[size2],al
mov byte[r],al
mov ebx,arr2
call read_array
mov al,byte[size1]
mov byte[s],al
mov ecx,arr1
call sort
mov al,byte[size2]
mov byte[s],al
mov ecx,arr2
call sort
mov al,byte[size1]
mov ah,byte[size2]
add al,ah
sub al,1
mov byte[size],al
mov byte[i],0
mov eax,arr1
mov ebx,arr2
mov ecx,array
merge:
mov dl,byte[eax]
mov dh,byte[ebx]
cmp dl,dh
jna A ;if L[a]<=L[b] ;a[i]=L[a] a=a+1
jmp B
A:
mov byte[ecx],dl
add eax,1
add ecx,1
inc byte[i]
mov dl,byte[size]
cmp byte[i],dl
je done
jmp merge
B:
mov byte[ecx],dh
add ebx,1
add ecx,1
inc byte[i]
mov dl,byte[size]
cmp byte[i],dl
je done
jmp merge
done:
mov eax, 4
mov ebx, 1
mov ecx, msg4
mov edx, len4
int 80h
mov ebx,array
mov al,byte[size]
mov byte[temp],al
print:
push rbx
mov al,byte[array]
mov byte[num],al
movzx ax,byte[num]
mov bl,10
div bl
mov byte[d1],al
mov byte[d0],ah
add byte[d1],48
add byte[d0],48
mov eax, 4
mov ebx, 1
mov ecx, d1
mov edx, 1
int 80h
mov eax, 4
mov ebx, 1
mov ecx, d0
mov edx, 1
int 80h
mov eax, 4
mov ebx, 1
mov ecx, comma
mov edx, 1
int 80h
pop rbx
add ebx,1
dec byte[temp]
cmp byte[temp],0
jg print
mov eax,1
mov ebx,0
int 80h
【问题讨论】: