I’m a beginner to assembly and taking an online crash course for it. One of the last challenges of the course is about function writing and calling. The idea is im supposed to recreate this function:
str_lower(src_addr):
i = 0
if src_addr != 0:
while [src_addr] != 0x00:
if [src_addr] <= 0x5a:
[src_addr] = foo([src_addr])
i += 1
src_addr += 1
return i
where src_addr is contained in rdi and i is rax. rdi is going to be randomly given a value and [rdi] contains a byte of information. The function foo is contained at 0x403000. The following is my assembly code to recreate the function:
.intel_syntax noprefix
jmp _start
str_lower:
xor rax, rax
pop rbx
mov rdi, [rsp]
cmp rdi, 0
jnz if
jmp return
if:
cmp byte ptr [rdi], 0x0
jnz while
jmp return
while:
cmp byte ptr [rdi], 0x5a
ja increment
innerif:
mov rcx, [rdi]
push rcx
call 0x403000
pop rcx
mov [rdi], cl
inc rax
increment:
cmp byte ptr [rdi], 0
inc rdi
jnz while
return:
push rax
push rbx
ret
_start:
push rdi
call str_lower
pop rax```
Sorry if the code has a little spaghetti but hopefully its readable. My issue here is the code compiles but when I pass it in the file supposed to check my challenge, pass in the rdi values, and give me my flag it gives me the error "Your program has crashed! Invalid memory fetch (UC_ERR_FETCH_UNMAPPED)". The program is looping until rax and rdi are very large which is why it crashes I just have no reason why it loops forever. I have looked over and changed this code for about 4 hours I am so lost this was my last resort lol.
Im a beginner to functions so my program has had many kinks worked out but so far I've tried changing the pointer header for [rdi] in the cmp lines to "byte ptr"instead of "dword ptr" honestly Im at a complete loss for why it's acting like this but Im sure its an easy fix.
tjwells is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.