2011-01-11 4 views
3

データセクションに2次元配列と2次元配列を定義しました(列和と行合計の2つ).2d配列を1d配列に合計する関数を作成しました。 私はeaxとebxの両方を2d配列のインデックスとして使用していますが、eaxやebxが1でメモリ内の未知のアドレスにアクセスしようとするとプログラムが失敗します。 は、どのように私は、この行のメモリへのアクセスを修正することができます:アセンブリ内の2D配列

mov edx,[ebp+columnsSumArray+type dword*ebx] 

これは私のプログラムである:

.386 
.MODEL flat,stdcall 
.STACK 4096 
extern [email protected]:Near 

.data     ;Data area 
array2D  Dword 1,2,3,4   ; 3 Rows by 4 Columns 
      Dword 5,6,7,8 
      Dword 9,10,11,12 

rowSumArray Dword 1,1,1    ; two sum init array's 
columnSumArray Dword 1,1,1,1 

.code     ;Code area 
_main: 

    mov eax,offset columnSumArray 
    push offset columnSumArray 
    push offset rowSumArray 
    push 4 
    push 3 
    push offset array2D 

    call Sum2DimArray 


    push 0      ;Black box. Always terminate 
    call [email protected]   ;program with this sequence 


;---------------------------------------------------------------- 
; Name: Sum2DimArray 
; Input: 2d array pointer, rows, columns, rowSumArray, columnSumArray, 
; Description: this function sum the rows item in the 2d array and put it in the rowSumArray, 
;    and sum the columns and put it in the columnSumArray 
;---------------------------------------------------------------- 
Sum2DimArray PROC 

    ParamSize = 5*4 
    matrixAddress = 8 
    rowsNumPlace = matrixAddress + 4 
    columnsNumPlace = rowsNumPlace + 4 
    rowsSumArray = columnsNumPlace + 4 
    columnsSumArray = rowsSumArray + 4 

    push ebp       ; using the ebp as function variables pointer 
    mov ebp,esp 

    push ecx 
    push eax 
    push ebx 
    push esi       ; 2d array item pointer 
    push edx      

    mov eax,0       ; rows counter 
    mov ebx,0       ; columns counter 

    mov esi,[ebp+matrixAddress]   ; esi points on the first 2d array value 

    RowsLoop:       ; rows loop 
     mov ebx,0 
     ColumnsLoop:     ; columns loop 

      mov ecx,[esi]    ; ecx is the current value 

      mov edx,[ebp+rowsSumArray+type dword*eax] 
      add [edx],ecx 
      mov edx,[ebp+columnsSumArray+type dword*ebx] 
      add [edx],ecx 

      inc ebx 
      add esi,sizeof Dword 
      cmp ebx,[ebp+columnsNumPlace] 
      jne ColumnsLoop 

     inc eax 
     cmp eax,[ebp+rowsNumPlace] 
     jne RowsLoop 

    pop edx 
    pop esi 
    pop ebx 
    pop eax 
    pop ecx 
    pop ebp 
    ret ParamSize 

Sum2DimArray ENDP 

end _main    ;End of program. Label is the entry point. 

答えて

5

あなたが追加されている間違った場所に和配列のオフセット。

mov edx,[ebp+rowsSumArray+type dword*eax] 
add [edx],ecx 
mov edx,[ebp+columnsSumArray+type dword*ebx] 
add [edx],ecx 

次のようになります。

mov edx,[ebp+rowsSumArray] 
add [edx+type dword*eax],ecx 
mov edx,[ebp+columnsSumArray] 
add [edx+type dword*ebx],ecx 

をあなたが最初に必要な要素のオフセットを追加し、知らebpからのオフセットでスタックからポインタをロードする必要があること。