[英]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.