[英]Implement StrCat in x86_64 assembly
所以這是問題所在:我實際上是在嘗試使用匯編語言重新編碼某些clib函數(這是一個幫助從匯編開始的學校項目)。 我當前正在使用的功能是strcat。 目前,我的目標是使其保持簡單並遵循以下幾條規則:
這是我的代碼:
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...
我嘗試了多種不同的方法(movsb,通過寄存器直接傳遞參數,更改寄存器...)始終達到相同的結果:
當前版本保持目標部分完整無缺,但在末尾添加了這些非字符字符: Ph
(這只是一個示例,但字符 Ph
更改)...
我以為您也許可以幫我(至少給我提示更改的地方,或者代碼中可能存在的錯誤),因為我在互聯網上四處尋找,卻從未找到能真正幫助我的東西。
哦,順便說一下,我在Ubuntu上與Nasm一起工作(是的,我知道;))。
非常感謝任何會回答的人。 :)
最有可能是
mov rdi, [rsp + 32] ; destination string
mov rsi, [rsp + 24] ; source string
要么
mov rdi, [rbp + 24] ; destination string
mov rsi, [rbp + 16] ; source string
無論您喜歡哪個
在當前版本中,RSI包含函數的返回地址。 如果當前您要在“目的地”后面添加一些內容,請嘗試
mov rdi, [rsp + 24]
mov rsi, [rsp + 32]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.