在NASM 2D阵列中查找最小数量

时间:2013-11-13 04:53:22

标签: loops matrix nasm

所以我有以下代码,用于在任何给定的n乘m矩阵中找到最大数字。

segment .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
  matrix1: resw 200
  m: resw 1
  n: resw 1
  i: resw 1
  j: resw 1
buff resb 4

segment .data
  msg1: db "Enter the number of rows in the matrix : "
  msg_size1: equ $-msg1
  msg2:  db "Enter the elements one by one(row by row) : "
  msg_size2: equ $-msg2
  msg3: db "Enter the number of columns in the matrix : "
  msg_size3: equ $-msg3
 msg4: db "The smallest Number is... : "
  msg_size4: equ $-msg4

  tab: db  9 ;ASCII for vertical tab
  new_line: db 10 ;ASCII for new line

segment .text

global _start

_start:
  mov eax, 4
  mov ebx, 1
  mov ecx, msg1
  mov edx, msg_size1
  int 80h

  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[m], cx

  mov eax, 4
  mov ebx, 1
  mov ecx, msg3
  mov edx, msg_size3
  int 80h

  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[n], cx

  mov eax, 4
  mov ebx, 1
  mov ecx, msg2
  mov edx, msg_size2
  int 80h

  ;Reading each element of the matrix........
  mov eax, 0
  mov ebx, matrix1  

  mov word[i], 0
  mov word[j], 0


  i_loop:
    mov word[j], 0
    j_loop:

 call read_num
mov dx , word[num]
 ;eax will contain the array index and each element is 2 bytes(1 word) long
 mov  word[ebx + 2 * eax], dx

 inc eax    ;Incrementing array index by one....

 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop

    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop
; read out matrix code 
    xor     esp, [matrix1]         
    ; esp initialized to first element in array, & is first smallest value


  ;Loop through the matrix, check each number if its 
  ;larger than the first number in the array. AT the end print said number.

    ;Reading each element of the matrix.(Storing the elements in row major order).......
  mov ebp, 0
  mov edi, matrix1  
  mov esp, 0

  mov word[i], 0
  mov word[j], 0


  i_loop2:
    mov word[j], 0
    j_loop2:

    ;eax will contain the array index and each element is 2 bytes(1 word) long
    mov  dx, word[edi+2*ebp]   ;
    mov word[num] , dx

    cmp esp, [num] ; compares current smallest number with current element iterated.
    jle   skip
    mov esp, [num] ; stores new smallest number
    mov esi, ebp   ; Stores pointer to the smallest element (never used..?)

    skip:
inc ebp
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop2

    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop2

    ;outut
  mov eax, 4
  mov ebx, 1
  mov ecx, msg4
  mov edx, msg_size4
  int 80h
mov ecx, 0

    mov eax, 4  ; system_write
    mov ebx, 1  ; stdout
    mov ecx, esp    ; move smallest element to accumulator
    add ecx, 48         ; convert to ascii representation
    mov [buff], ecx     ; move to memory
    mov ecx, buff
    mov edx, 4          ; size, 4 bytes
    int 80h

exit:
  mov eax, 1
  mov ebx, 0
  int 80h

;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0

  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h

    cmp byte[temp], 10
      je end_read

    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax   

    jmp loop_read 
  end_read:
  popa

ret

我已经发现问题可能是mov esp,0但是没有那条线,结果会更糟,因为答案总是'0'。没有它,就会导致一无所获或无聊。

同样,它的原始作用是找到最大的数字,但现在我想找到最小的,逻辑上这应该意味着只是将jge改为jle但是它保持为零(可能是因为mov esp,0)或乱码我试着把它做成别的吗?

如何解决此问题,以便找到变化最小的最小数字?该代码最初只是在经过重大的试验和错误之后才开始工作,而且几乎随意地摆弄着我。

此外,输出的乱码是:âμμ

如果我删除了mov esp,0但保留为jge,它仍适用于LARGEST数字,但如果我将其切换为jle,则输出âμμ。这没有任何意义。

0 个答案:

没有答案