Manipulating arrays in assembly

时光总嘲笑我的痴心妄想 提交于 2019-12-13 05:23:43

问题


I have a problem, that i cant figure out:

In assembly language, write a function that receives a pointer to an array of integers and the size of this array, and changes the array by reversing its elements without copying the array to stack. Use the dedicated instructions and registers to work with arrays (esi, edi; lodsb, stosb, cld, std).

Example: 1 2 3 4 5 -> 5 4 3 2 1

Anyone have any suggestions?


回答1:


Reversing an array with lodsb and stosb requires cld and std for every element (because one of the pointers needs to increment and the other needs to decrement), or alternatively, you can just forget cld and std and just cancel the incorrect increment (or decrement) of the other pointer by subtracting 2 (or adding 2) to it after each element.

Anyway, using lodsb and stosb in this case makes things unnecessarily complicated in my opinion. I'd use something like this:

    mov esi,start_address
    mov edi,end_address

    mov ecx,edi
    sub ecx,esi

x1: test ecx,ecx
    jz @ready

    mov al,[esi]
    xchg al,[edi]
    mov [esi],al
    inc esi
    dec edi
    dec ecx
    jmp x1

@ready:


来源:https://stackoverflow.com/questions/13541679/manipulating-arrays-in-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!