0

ユーザーが行列のサイズを決定するために選択した次元を入力し、一度に 1 つずつ数値を読み取り、最大の数値を出力することが期待されるという問題があります。

これが私の元のコードです。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

さて、どうにかして問題を解決できましたが、なぜそれが機能したのか本当にわかりません。

私がしたことは、ポインターをマトリックスの「インデックス」に保存する以前の方法から切り替えることでした。これは、1次元配列を想定しているため機能していないと思い、代わりに新しい最大数を保存することに単純に落ち着きました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 に答える