i need help writing this naked function in assemply code __declspec(naked) int i
ID: 3833360 • Letter: I
Question
i need help writing this naked function in assemply code
__declspec(naked)
int isPalindrome(char *input_string, int left_index, int right_index)
// C code to be converted to x86 assembly /* Input Validation
if (NULL == input_string || left_index < 0 || right_index < 0){
return -1;
}
// Recursion termination condition
if (left_index >= right_index)
return 1;
if (input_string[left_index] == input_string[right_index]){
return isPalindrome(input_string, left_index + 1, right_index - 1);
}
return -1;
*/
__asm {
mov eax, 0
// BEGIN YOUR CODE HERE
// END YOUR CODE HERE
ret
Explanation / Answer
isPalindrome(char*, int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov QWORD PTR [rbp-8], rdi
mov DWORD PTR [rbp-12], esi
mov DWORD PTR [rbp-16], edx
cmp QWORD PTR [rbp-8], 0
je .L2
cmp DWORD PTR [rbp-12], 0
js .L2
cmp DWORD PTR [rbp-16], 0
jns .L3
.L2:
mov eax, -1
jmp .L4
.L3:
mov eax, DWORD PTR [rbp-12]
cmp eax, DWORD PTR [rbp-16]
jl .L5
mov eax, 1
jmp .L4
.L5:
mov eax, DWORD PTR [rbp-12]
movsx rdx, eax
mov rax, QWORD PTR [rbp-8]
add rax, rdx
movzx edx, BYTE PTR [rax]
mov eax, DWORD PTR [rbp-16]
movsx rcx, eax
mov rax, QWORD PTR [rbp-8]
add rax, rcx
movzx eax, BYTE PTR [rax]
cmp dl, al
jne .L6
mov eax, DWORD PTR [rbp-16]
lea edx, [rax-1]
mov eax, DWORD PTR [rbp-12]
lea ecx, [rax+1]
mov rax, QWORD PTR [rbp-8]
mov esi, ecx
mov rdi, rax
call isPalindrome(char*, int, int)
jmp .L4
.L6:
mov eax, -1
.L4:
leave
ret
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.