So here is the problem : I'm actually trying to recode some of the clib functions in assembly language (this is a school project to help start with assembly). The function I am currently working on is strcat. For the moment my goal is to keep it simple and follow the few following rules:
Here is my code:
ft_strcat:
push rbp
mov rbp, rsp ; saving the stack state
push rdi ; seems to work better this way but I don't know why
mov rdi, [rsp + 24] ; destination string
mov rsi, [rsp + 16] ; source string
push rdi ; keeping the adress to return
test rsi, rsi ; in case one of the strings is NULL
je getdest
test rdi, rdi
je getsrc
toend: ; to go to the end of the destination string
cmp byte [rdi], 0x0 ; is it the end?
je cpy ; if so, go to the next part
inc rdi ; else keep going
jmp toend ; loop
cpy: ; to copy the source string to the end of the destination string
mov al, byte[rsi] ; getting the byte to copy
mov byte [rdi], al ; copying it
cmp byte [rsi], 0x0 ; it is the end of the source string?
je getdest ; if so, jump to the end
inc rdi ; else increase counter
inc rsi
jmp cpy ; loop
getdest: ; if source is NULL or copy is done
pop rax
jmp end
getsrc: ; if destination is NULL
mov rax, rsi
end:
pop rdi ; get rdi back
leave
ret ; finally return...
I have tried a tremendous number of different ways (movsb, passing argument with the registers [directly], changing registers...) always reaching the same result :
This current version keeps the destination part intact but adds those none-character characters at the end : Ph
(this is just an example but the characters tend to change once in a while)...
I thought maybe you could help me (at least give me a hint of where to change things, or what may be wrong in my code), because I have looked all over the internet and never found things that would really help me.
Oh, by the way, I work with Nasm on Ubuntu (yes, I know ;) ).
Thx a lot to anyone who will answer. :)
most likely its
mov rdi, [rsp + 32] ; destination string
mov rsi, [rsp + 24] ; source string
or
mov rdi, [rbp + 24] ; destination string
mov rsi, [rbp + 16] ; source string
whichever you like better
In your current version, RSI contains return addr of the function. If currently you're appending some stuff to 'destination' then try
mov rdi, [rsp + 24]
mov rsi, [rsp + 32]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.