装配中的二维数组
我在数据部分定义了一个二维数组和两个一维数组(一个用于列总和,一个用于行总和),并且我编写了一个函数,将二维数组求和到一维数组中。 我使用 eax 和 ebx 作为二维数组的索引,但是当 eax 或 ebx 为 1 并尝试访问内存中的未知地址时,我的程序失败。 我如何修复这一行中的内存访问:
mov edx,[ebp+columnsSumArray+type dword*ebx]
这是我的程序:
.386
.MODEL flat,stdcall
.STACK 4096
extern ExitProcess@4: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 ExitProcess@4 ;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.
I defined in the data section an 2d array and two 1d arrays (one for column sum and one for row sum) and i wrote a function that sum the 2d array into the 1d array.
I'm using both eax and ebx as indexes to the 2d array but my program fail when eax or ebx becase 1 and trying to access to unknown address in the memory.
how can i fix the access to the memory in this line:
mov edx,[ebp+columnsSumArray+type dword*ebx]
this is my program:
.386
.MODEL flat,stdcall
.STACK 4096
extern ExitProcess@4: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 ExitProcess@4 ;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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在错误的位置添加了总和数组的偏移量。
应该读为:
也就是说,您应该首先从堆栈中距
ebp
已知偏移量处加载指针,然后添加所需元素的偏移量。You are adding the offset for the sum arrays in the wrong place.
Should read:
That is you should first load the pointer from the stack at a known offset from
ebp
, then add the offset for the required element.