0

我有一个问题,用户需要输入他们选择的维度来确定矩阵的大小,一次读取一个数字,然后输出最大的数字。

这是我的原始代码,输入1,2,3,4时输出为12343;它声称最大的数字是最后的“3”。当我使用 4 比 1 时,它正确地给出了 4。我的观察在我看来,它可能只能找到 2 x 2 矩阵上第一行的最大数。

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
  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 largest value
    ;xor     edi, edi          ; edi initialized to 0, edi is loop counter


  ;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

    mov eax, 4 ;
    mov ebx, 1
    mov ecx, [edi+2*ebp]
    add ecx, 48
    mov [buff], ecx
    mov ecx, buff
    mov edx, 4
       int 80h

 cmp esp, [edi+2*ebp] ;word[ebx + 2 * eax] ; compares current biggest number with current element iterated.
   jge   skip 
   mov esp, [edi+2*ebp] ;word[ebx + 2 * eax] ; stores new biggest number
   mov esi, ebp         ; Stores pointer to the biggest element    



   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  ; system_write
    mov ebx, 1  ; stdout
    mov ecx, [edi+2*esi]    ; move biggest element to accumulator
    add ecx, 30h            ; convert to ascii representation
    mov [buff], ecx     ; move to memory
    mov ecx, buff       ; put pointer in ecx for printing
    mov edx, 4          ; size, 4 bytes
    int 80h         ; sytem call.


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

最后在绝望的行为中,我尝试了以下方法:

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

    cmp byte[temp], 10
      je end_read2

    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


    cmp esp, [num] ;word[ebx + 2 * eax] ; compares current biggest number with current element iterated.
   jge   skip 
   mov esp, [num] ;word[ebx + 2 * eax] ; stores new biggest number
   mov esi, ebp         ; Stores pointer to the biggest element    
   skip:
    ; outut
    mov eax, 4  ; system_write
    mov ebx, 1  ; stdout
    mov ecx, [esp]  ; move biggest element to accumulator
    add ecx, 30h            ; convert to ascii representation
    mov [buff], ecx     ; move to memory
    mov ecx, buff       ; put pointer in ecx for printing
    mov edx, 4          ; size, 4 bytes
    int 80h         ; sytem call.   

    jmp loop_read2 
  end_read2:
  popa

ret

在这里,我试图同时用户输入一个数字,将其与最大的数字进行比较,然后输出该数字以进行调试。但它对我来说是段错误。

4

1 回答 1

0

好吧,不知何故我设法解决了这个问题,但我真的有点不知道为什么它会起作用。

我所做的是从我以前保存指向矩阵“索引”的指针的方法切换,我认为这不起作用,因为我认为它假设一个一维数组,而是简单地保存新的最大数进入ESP,然后输出它。它有效吗?我尽我所能用 2x2、3x3 和 5x5 进行了测试(因为 5x5 很乏味)和一些边缘情况,并且似乎有效。:欢呼:

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 Largest 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 largest value
    ;xor     edi, edi          ; edi initialized to 0, edi is loop counter


  ;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] ;word[ebx + 2 * eax] ; compares current biggest number with current element iterated.
    jge   skip
    mov esp, [num] ;word[ebx + 2 * eax] ; stores new biggest number
    mov esi, ebp                ; Stores pointer to the biggest element



    skip:
;debug code
    ;mov eax, 4 ;
    ;mov ebx, 1
    ;mov ecx, esp
    ;add ecx, 48
    ;mov [buff], ecx
    ;mov ecx, buff
    ;mov edx, 4
    ;int 80h
   ;mov esp, [num] 

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 biggest 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
于 2013-03-26T06:26:05.580 回答