Task: Displaying the Reversed String
Navigate to tasks/print-rev-string/support/.
In the file print_rev_string.asm, add the reverse_string() function so that you have a listing similar to the one below:
[...]
section .text
extern printf
extern puts
global print_reverse_string
reverse_string:
push ebp
mov ebp, esp
push ebx ; preserve ebx as required by cdecl
mov eax, [ebp + 8]
mov ecx, [ebp + 12]
add eax, ecx
dec eax
mov edx, [ebp + 16]
copy_one_byte:
mov bl, [eax]
mov [edx], bl
dec eax
inc edx
loopnz copy_one_byte
inc edx
mov byte [edx], 0
pop ebx ; restore ebx
leave
ret
print_reverse_string:
push ebp
mov ebp, esp
[...]
IMPORTANT: When copying the
reverse_string()function into your program, remember that the function starts at thereverse_string()label and ends at theprint_reverse_stringlabel. Thecopy_one_bytelabel is part of thereverse_string()function.
The reverse_string() function reverses a string and has the following signature: void reverse_string(const char *src, size_t len, char *dst);. This means that the first len characters of the src string are reversed into the dst string.
Reverse the mystring string into a new string and display that new string.
NOTE: To define a new string, we recommend using the following construction in the data section:
store_string times 64 db 0This creates a string of 64 zero bytes, enough to store the reverse of the string. The equivalent C function call is
reverse_string(mystring, ecx, store_string);. We assume that the length of the string is calculated and stored in theecxregister.You cannot directly use the value of
ecxin its current form. After theprintf()function call for displaying the length, the value ofecxis not preserved. To retain it, you have two options:
- Store the value of the
ecxregister on the stack beforehand (usingpush ecxbefore theprintfcall) and then restore it after theprintfcall (usingpop ecx).- Store the value of the
ecxregister in a global variable, which you define in the.datasection.You cannot use another register because there is a high chance that even that register will be modified by the
printfcall to display the length of the string.
To test the implementation, enter the tests/ directory and run:
make check
In case of a correct solution, you will get an output such as:
./run_all_tests.sh
test_reverse_simple ........................ passed ... 33
test_reverse_special ........................ passed ... 33
test_reverse_long ........................ passed ... 34
Total: 100/100
If you’re having trouble solving this exercise, go through this reading material.