交换2个指针x86 intel汇编 [英] Swapping 2 pointers x86 intel Assembly

查看:102
本文介绍了交换2个指针x86 intel汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试交换2个指针,这些指针是通过引用传递给子例程的.这是我所拥有的:

I am trying to swap 2 pointers in that are passed by reference to a sub-routine. Here is what I have:

.data
    firstInputPrompt                BYTE 'Enter First String: ',0
    secondInputPrompt               BYTE 'Enter Second String: ',0

    firstString                     BYTE 16 DUP(0)              ;string buffers
    secondString                    BYTE 16 DUP(0)

    firstPointer                    DWORD OFFSET firstString    ;pointers
    secondPointer                   DWORD OFFSET secondString

.code

compare PROC

    push        ebp                 ;readying stack for use
    mov         ebp, esp

    push        eax                 ;used for comparing chars
    push        ebx                 
    push        ecx                 

    mov         ebx, [ebp+12]       ;ebx now a pointer to firstString
    mov         ecx, [ebp+8]        ;ecx now a pointer to secondString
    mov         ebx, [ebx]
    mov         ecx, [ecx]


;iterate over strings
iterate:    
    mov         al, [ebx]           ;compare characters
    cmp         al, [ecx]
    ja          swap_pointers
    jb          end_method

    mov         al, [ebx]
    cmp         al, 0
    je          end_method
    mov         al, [ecx]
    cmp         al, 0
    je          end_method  

    inc         ebx
    inc         ecx 

    jmp         iterate

swap_pointers:
    ;mov            ecx, [ebp+12]       ;get pointers again
    ;mov            ebx, [ebp+8]


    lea         ebx, dword ptr [ebp+12]
    lea         ecx, dword ptr [ebp+8]



end_method:
    ;pop used registers
    pop         ecx
    pop         ebx
    pop         eax
    pop         ebp
    ret
compare ENDP

我感到困惑的地方是在swap_pointers:标签之后.我不知道如何交换2个指针.关于我在做什么错的任何想法吗?

The point where I get confused is right after the swap_pointers: label. I can't figure out how to swap the 2 pointers. Any ideas on what I'm doing wrong?

推荐答案

...
;load registers with pointers 
mov ebx, firstPointer
mov ecx, secondPointer
;save pointers from register, swapping content
mov firstPointer, ecx
mov secondPointer, ebx
;finis

这篇关于交换2个指针x86 intel汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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