ユーザーが行列のサイズを決定するために選択した次元を入力し、一度に 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
ここでは、ユーザーが数値を入力すると同時に、それを最大の数値と比較し、デバッグ目的でその数値を出力しようとしています。しかし、私にはセグメンテーション違反があります。