Your C code is attempting to swap the values pointed to by pointers but your assembly code seems to be treating values as pointers leading to segmentation faults
. One way to handle this is to use extra register(s) to hold the pointers and the values they point to.
The 32-bit CDECL allows us to use EAX, ECX, and EDX without worrying about saving their values. We'll need a 4th register and we'll have to preserve it ourselves.
I will also assume you want the frame pointer present (the routine could be written without it):
push %ebp
mov %esp,%ebp
push %ebx # Preserve non-volatile EBX register
mov 8(%ebp),%eax # EAX = Pointer to a (arg1)
mov 12(%ebp),%edx # EDX = Pointer to b (arg2)
mov (%eax),%ecx # Temporarily save value at memory address a (*a) to ECX
mov (%edx),%ebx # Temporarily save value at memory address b (*b) to EBX
mov %ebx,(%eax) # Move value of b (EBX) to memory address of a (*a)
mov %ecx,(%edx) # Move value of a (ECX) to memory address of b (*b)
pop %ebx # Restore non-volatile EBX register
pop %ebp
ret
In theory you could remove the stack frame altogether (may make stack traces in the debugger harder to follow). The code could have used ESP to access the arguments rather than EBP:
push %ebx # Preserve non-volatile EBX register
mov 8(%esp),%eax # EAX = Pointer to a
mov 12(%esp),%edx # EDX = Pointer to b
mov (%eax),%ecx # Temporarily save value at memory address a (*a) to ECX
mov (%edx),%ebx # Temporarily save value at memory address b (*b) to EBX
mov %ebx,(%eax) # Move value of b (EBX) to memory address of a (*a)
mov %ecx,(%edx) # Move value of a (ECX) to memory address of b (*b)
pop %ebx # Restore non-volatile EBX register
ret
Note: Failing to preserve (per 32-bit CDECL calling convention) non-volatile registers like EBX, EBP, ESI, and EDI is a bug. In simple test code it may appear to work, but in more complex code with optimizations on you may experience undefined behaviour if the calling convention isn't strictly adhered to.