在组装二维数组 [英] 2D array in assembly

查看:141
本文介绍了在组装二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据段所限定的二维数组和两个一维数组(一个用于列总和和一个用于行总和)和i写道求和二维数组入一维数组的功能。
我同时使用EAX和EBX为指标的二维数组,但我的计划失败时,EAX或EBX becase的1和试图访问未知地址的内存。
如何解决在此行中访问内存:

  MOV EDX,[EBP + columnsSumArray +类型为DWORD * EBX]

这是我的程序:

  .386
.MODEL平,STDCALL
.STACK 4096
EXTERN ExitProcess的@ 4:近。数据;数据区
array2D双字1,2,3,4; 3行4列
            DWORD 5,6,7,8
            DWORD 9,10,11,12rowSumArray双字1,1,1;两个和初始化数组
columnSumArray双字1,1,1,1。code code区
_主要:    MOV EAX,抵消columnSumArray
    推偏移columnSumArray
    推偏移rowSumArray
    4推
    3推
    推偏移array2D    调用Sum2DimArray
    推0;黑盒子。始终终止
    调用了ExitProcess @ 4;程序与此序列
; ------------------------------------------------- ---------------
;名称:Sum2DimArray
;输入:二维数组的指针,行,列,rowSumArray,columnSumArray,
;说明:此功能总结行项目二维数组中,并把它放在rowSumArray,
; ,总结列,并把它在columnSumArray
; ------------------------------------------------- ---------------
Sum2DimArray PROC    ParamSize = 5 * 4
    matrixAddress = 8
    rowsNumPlace = matrixAddress + 4
    columnsNumPlace = rowsNumPlace + 4
    rowsSumArray = columnsNumPlace + 4
    columnsSumArray = rowsSumArray + 4    推EBP;使用EBP作为函数变量的指针
    MOV EBP,ESP    推ECX
    推EAX
    推EBX
    推ESI;二维数组指针项
    推EDX    MOV EAX,0;行柜台
    MOV EBX,0;列反击    MOV ESI,[EBP + matrixAddress]; ESI点上​​的第一个二维数组值    RowsLoop:;环行
        MOV EBX,0
        ColumnsLoop:;列循环            MOV ECX,[ESI] ECX是当前值            MOV EDX,[EBP + rowsSumArray +类型为DWORD * EAX]
            添加[EDX],ECX
            MOV EDX,[EBP + columnsSumArray +类型为DWORD * EBX]
            添加[EDX],ECX            INC EBX
            加ESI,双字的sizeof
            CMP EBX,[EBP + columnsNumPlace]
            JNE ColumnsLoop        INC EAX
        CMP EAX,[EBP + rowsNumPlace]
        JNE RowsLoop    流行EDX
    流行ESI
    流行EBX
    流行EAX
    流行ECX
    流行EBP
    RET ParamSizeSum2DimArray ENDP结束_main;程序结束。标签是入口点。


解决方案

您要添加的偏移量在错误的地方之和阵列。

  MOV EDX,[EBP + rowsSumArray +类型为DWORD * EAX]
添加[EDX],ECX
MOV EDX,[EBP + columnsSumArray +类型为DWORD * EBX]
添加[EDX],ECX

应该是:

  MOV EDX,[EBP + rowsSumArray]
添加[EDX +类型为DWORD * EAX],ECX
MOV EDX,[EBP + columnsSumArray]
添加[EDX +类型为DWORD * EBX],ECX

这是你应该首先从堆在一个已知的从 EBP 偏移量,然后添加对所需元素的偏移量加载指针。

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.

解决方案

You are adding the offset for the sum arrays in the wrong place.

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

Should read:

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

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.

这篇关于在组装二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆