hi again,
I need to swap the contents of two declared variables using a function call by value, here's what I have so far. I'm actually not quite familiar with using the dword keyword, I'm just following my course slides:
section .data
a db "3"
alen equ $-a
b db "7"
blen equ $-b
msga db "The value of A is "
msgalen equ $-msga
msgb db "The value of B is "
msgblen equ $-msgb
swapmsg db "SWAPPED!"
swapmsglen equ $-swapmsg
section .text
global _start
_start:
; print initial values
call print_a
call print_b
; call swap by value
push dword [a]
push dword [b]
call swap
; print new values
call print_a
call print_b
;exit
mov eax, 1
mov ebx, 0
int 0x80
; ********** functions ************
swap:
; preserve our register data
push ebp
push ebx
push eax
mov ebp, esp
; get data from the stack
mov eax, [ebp + 20]
mov ebx, [ebp + 16]
; swap stack data
mov [ebp + 16], eax
mov [ebp + 20], ebx
; print swap confirmation
mov eax, 4
mov ebx, 1
mov ecx, swapmsg
mov edx, swapmsglen
int 0x80
; restore register data
pop eax
pop ebx
pop ebp
; newline
mov al, 0xA
call print_char
ret 8
print_a:
mov eax, 4
mov ebx, 1
mov ecx, msga
mov edx, msgalen
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, a
mov edx, alen
int 0x80
mov al, 0xA
call print_char
ret
print_b:
mov eax, 4
mov ebx, 1
mov ecx, msgb
mov edx, msgblen
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, b
mov edx, blen
int 0x80
mov al, 0xA
call print_char
ret
; function to print the character in al (like the various newlines, # and colons, etc.)
print_char:
push edx
push ecx
push ebx
push eax ; must be pushed last - it's our "buffer"
mov eax, 4
mov ebx, 1
mov ecx, esp ; our buffer
mov edx, 1 ; just one
int 0x80
pop eax
pop ebx
pop ecx
pop edx
ret
When I run it, the contents are unchanged it and it prints 3 and 7 twice, in the same order (rather than the reverse). If I had to guess, it probably has something to do with the sizes being manipulated, but I'm not seeing it.
EDIT: So the actual memory locations are being swapped (I've tested and printed both locations before and after the swap). However, I can't make them go back to dwords a and b.