简体   繁体   English

在组装中合并两个字符串

[英]Merging two strings in Assembly

I'm trying to merge two simple strings in assembly, in a basic fashion, eg: 我正在尝试以基本方式合并两个简单的字符串,例如:

String 1: AAAAAA 字串1:AAAAAA

String 2: BBB 弦2:BBB

Result: ABABABAAA 结果:ABABABAAA

So just alternating characters. 所以只是交替字符。

I've been trying to do this for a bit, but for some reason, I never reach the null terminators on my strings, and the debugger is making it impossible for me to even figure out how to find out if the strings are being combined in the first place. 我一直在尝试这样做,但是由于某种原因,我从未在字符串上到达空终止符,并且调试器使我什至无法弄清楚如何找出字符串是否被合并首先。

        AREA HW42, CODE, READONLY

        ENTRY

        EXPORT main

main
MAX_LEN EQU 100
    LDR R8, =StrOne
    LDR R9, =StrTwo

Loop
    LDRB R3, [R8], #1
    STRB R3, [r2], #1
    LDRB r4, [r9], #1
    STRB r4, [r2], #1
    ORR R5, R3, R4
    CBZ R5, DONE
    B Loop

    LDR R8, =MixStr
    STR R3, [R8]

DONE
stop
        MOV r0, #0x18
        LDR r1, =0x20026
        SVC #0x11

        ALIGN

        AREA HWData, DATA, READWRITE
        EXPORT adrStrOne
        EXPORT adrStrTwo
        EXPORT adrMixStr

StrOne  DCB "HLO",0
StrTwo  DCB "EL",0
MixStr  SPACE MAX_LEN

adrStrOne DCD StrOne
adrStrTwo DCD StrTwo
adrMixStr DCD MixStr
        ALIGN

        END

What am I doing wrong here? 我在这里做错了什么?

ORR R5, R3, R4
CBZ R5, DONE

This makes it probably keep going until it falls off the end of memory and faults, because your strings are different lengths: 这使得它可能一直继续下去,直到它掉到内存和错误的末端,因为您的字符串长度不同:

'H' | 'E' != 0
'L' | 'L' != 0
'O' | '\0' != 0
'\0' | ??? != 0 (probably)
...

If you need to handle different-length strings, you need to check for and handle the end of each one individually. 如果需要处理不同长度的字符串,则需要分别检查并处理每个字符串的结尾。


In fact, there's no "probably" about it - due to the layout of your data, the first "???" 实际上,没有任何“可能”-由于数据的布局,第一个“ ???” will actually be the beginning of MixStr , which is guaranteed to be nonzero on account of being 'H' by this point, and from then on you run away copying the earlier part of the result into itself, until you fall off the end of the data section, or memory, whichever comes first. 实际上将是MixStr的开始, MixStr为'H',因此它保证为非零,然后从此开始,将结果的较早部分复制到自身中,直到失去结尾为止。数据部分或内存,以先到者为准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM