NASM程序 - 合并两个阵列

时间:2014-01-18 12:49:43

标签: arrays assembly x86 nasm

我是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

1 个答案:

答案 0 :(得分:0)

在汇编中只能出现两种错误:

  • 每条指令旁边的注释描述了一个不正确的算法
  • 说明书不执行旁边的评论所描述的内容

为了找到您的错误,我首先检查每条指令旁边的注释是否描述了正确的算法。它没有。然后我继续说,并检查每条指令是否符合其评论所说的内容。他们都没有。您的代码是100%纯错误。

例如,这段代码是100%错误:

    mov al, byte[d1]
    mov dl, 10
    mul dl
    add al, byte[d0]

...而这段代码可能是100%正确的:

    mov al, byte[d1]    ;al = first digit read from standard input
    mov dl, 10          ;dl = 10
    mul dl              ;al = ax = first digit * 10
    add al, byte[d0]    ;al = al + second digit from standard input
                        ;   = first digit * 10 + second digit

以同样的方式,这段代码是100%错误:

    mov eax, 3
    mov ebx, 0
    mov ecx, d1
    mov edx, 1
    int 80h

...而这段代码可能是100%正确的:

%define SYSAPI_READ   3      ;Linux kernel function number for "READ()"
%define STDIN         0      ;File descriptor number for standard input

    mov eax, SYSAPI_READ     ;eax = function number for Linux API
    mov ebx, STDIN           ;ebx = file descriptor
    mov ecx, d1              ;ecx = address of destination buffer
    mov edx, 1               ;edx = number of bytes to read
    int 80h                  ;Call Linux kernel API

如果你试图解决所有“缺乏评论”的错误,那么你很可能在没有尝试的情况下找到你的其他问题。