关于汇编数组与字符串示例中LDRB指令使用的技术咨询
LDRB is used in your array and string assembly examples Great question! The use of LDRB here has nothing to do with syntax requirements—it’s entirely driven by the size of the data you’re working with in both examples. Let’s break down each case to see why it’s necessary:
Array Example
Your array is defined with .byte 10, 20, 25—this tells the assembler to store each element as a single 8-bit byte, one after another in memory.
If you used LDR instead of LDRB:
LDRloads a full 32-bit word (4 bytes) from memory at once. So the firstLDRwould grab10, 20, 25,plus whatever random byte is next in memory, combine them into a 32-bit value, and put that intor2. That’s not what you want—you need to sum each individual byte element one at a time.LDRBsolves this by loading exactly one byte from the address inr1, then automatically incrementingr1by 1 (thanks to the, #1post-indexing) to point to the next byte. This lets you process each element in the array correctly, adding them tor3one by one.
String Example
Your string uses .asciz, which stores each character as an ASCII byte (8 bits) followed by a null terminator (0).
Again, LDRB is essential here:
- Each iteration needs to check if the current character is the null terminator to know when to stop the loop.
LDRBloads one character byte at a time intor2, so you can directly comparer2to#nul(0) each loop. - If you used
LDR, you’d load 4 characters at once. To find the null terminator, you’d have to check each of the 4 bytes in the loaded word, which adds extra complexity to your code. UsingLDRBkeeps the loop logic simple and straightforward, processing one character at a time until you hit the end of the string.
Key Takeaway
LDRB is used here because both your array and string are made up of byte-sized data. The instruction matches the data width: use LDRB for 8-bit bytes, LDR for 32-bit words, LDRH for 16-bit halfwords, etc. It’s not a syntax rule—you could use LDR in these cases, but it would break the intended logic of processing each individual element/character.
内容的提问来源于stack exchange,提问作者Arcturus

