Assembly 更改字符串中的奇偶位置
所以我想解决一个练习,它改变了字符串中偶数和奇数位置的数据。我的输入字符串是RDI,RSI是我的输出,输入大小是RDX。我不确定我做错了什么,因为我试着跑步时会犯很多错误 我的逻辑是:如果赔率等于偶数,那么我需要加奇数,否则就加偶数。每次添加输出时,我都会验证赔率+偶数是否等于输入大小。如果是,那么我完成了,我需要完成,否则继续 我对汇编的东西还很陌生,所以如果我写了什么愚蠢的东西,就知道我是个大傻瓜 示例:输入字符串“airport”->输出字符串“iaprrot”Assembly 更改字符串中的奇偶位置,assembly,x86,nasm,Assembly,X86,Nasm,所以我想解决一个练习,它改变了字符串中偶数和奇数位置的数据。我的输入字符串是RDI,RSI是我的输出,输入大小是RDX。我不确定我做错了什么,因为我试着跑步时会犯很多错误 我的逻辑是:如果赔率等于偶数,那么我需要加奇数,否则就加偶数。每次添加输出时,我都会验证赔率+偶数是否等于输入大小。如果是,那么我完成了,我需要完成,否则继续 我对汇编的东西还很陌生,所以如果我写了什么愚蠢的东西,就知道我是个大傻瓜 示例:输入字符串“airport”->输出字符串“iaprrot” rsp不是通用寄存器。您可
rsp
不是通用寄存器。您可以在本地将其视为这样,但必须以某种方式保留输入时的内容,否则ret
将被随机放置。除此之外,如果您可以随意使用任何方法,我的建议是,只要有两个可用字符,一次读取两个字符,然后交换它们,如mov AX中所示,[RDI];XCHG AL,啊;mov[RSI],AX
。一次复制两个字节,交换一对字节会简单得多。看看你需要多少逻辑才能准确地计算出如何处理一个位置。(特别是如果你坚持寄存器的inc/dec,而不是在寻址模式中只使用偏移量的话。)例如movzx eax,word[RDI[
/rol-ax,8
(swap-AH和AL)/mov[rsi],ax
@500 InternalServerError-Heh,我们在同一时间发表了相同的评论。但是请注意,rol-ax,8
比xchg
更有效,特别是如果您想在之后读取ax(无需部分寄存器合并)@在奇数长度的字符串中,最后一个字节应该来自哪里?你应该从同一个位置复制未配对的字节吗?因为从一个字节复制后意味着你加载源字符串的终止0字节。如果是这样,最后一个字节将需要特殊处理,无论你是否有效(一次1对)或不。
mov RCX, RDX
mov RCX, 0 ; keeping track of how many odd positions have been added to the output
mov RSP, 0 ; keeping track of how many evenpositions have been added to the output
myloop:
cmp RSP, RCX
je odd
cmp RCX, RSP
jg even
ret
even:
sub RDI, 1 ; remained on odd position so need to go on the previous position
inc RSP ; increment the number of even positions i managed
mov AL, [RDI]
mov [RSI], AL ; put in the output the character from the even position
inc RSI
add RDI, 2
mov RAX, RCX
add RAX, RSP ; add number of odds with number of evens
cmp RDX, RAX ; if number of odds+evens is less then input size then repeat
jg myloop
ret
odd:
inc RDI ; go on odd position
inc RCX ; increment the number of odd position i managed
mov AL, [RDI]
mov [RSI], AL ; put in output the character from the odd position
inc RSI
mov RAX, RCX
add RAX, RSP
cmp RDX, RAX
jg myloop
ret