I'm having trouble swapping the strings in an array in Linux x86_64 assembly language

111 views Asked by At

I want to implement bubblesort and quicksort in x86_64 assembly language on Linux, but I'm having a lot of trouble. I can't get a basic swap routine (swapij) to work.

Any help would be greatly appreciated. Thank you.

The program is built with:

yasm -f elf64 -g dwarf2 -o testStr.obj testStr.asm
gcc -g -z noexecstack testStr.obj -o testStr
1

There are 1 answers

4
Sep Roland On

You don't have the necessary buffers

section     .data
pTempi      db  64
pTempj      db  64

These dbs just side aside a single byte each (preloaded with the value 64). What you need is something like:

section     .bss
pTempi      resb  64
pTempj      resb  64

Doesn't your strcpy need to be told how many bytes to copy?

mov     rcx, r8
lea     rdi, [pTempi]
call    getElement          ; rdi contains str from array[i]
strcpy

You provide destination and source addresses, but I don't see you pass an actual length to strcpy.
Also, pay extra attention to the comments that you write. Upon returning from getElement, it is RSI that points to the string in the i-th 64-byte record of the array. And a mention like "contains str" is definitely confusing/wrong since the register does not contain any characters from the string. Instead the register contains a pointer to a certain string.