交换2个指针x86 intel汇编

时间:2013-06-11 01:46:15

标签: pointers assembly x86

我试图交换2个指针,通过引用传递给子程序。这就是我所拥有的:

.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指针。关于我做错了什么想法?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:-2)

xor在需要时交换寄存器值

 load registers with pointers 
 mov ebx, firstPointer
 mov ecx, secondPointer

 xor ebx,ecx
 xor ecx ebx
 xor ebx,ecx

GL