官方文档
① .ascii "string"...
.ascii
expects zero or more string literals (see section Strings) separated by commas. It assembles each string (with no automatic trailing zero byte) into consecutive addresses.
举例:.ascii “welcome/0”
注意:.ascii伪操作定义的字符串需要自行添加结尾字符’/0’
② .asciz "string"...
.asciz
is just like.ascii
, but each string is followed by a zero byte. The “z” in `.asciz’ stands for “zero”.
举例: .asciz “qwer”, “sun”, “world!”
注意:.ascii伪操作定义的字符串需要自行添加结尾字符’/0’
③ .string "str"
Copy the characters in str to the object file. You may specify more than one string to copy, separated by commas. Unless otherwise specified for a particular machine, the assembler marks the end of each string with a 0 byte. You can use any of the escape sequences described in section Strings.
举例: .string “abcd”, “efgh”, “hello!”
操作实例:
.text
.global _start
_start:
b reset
ldr pc,=und_addr
und_addr:
.word undef
undef:
ldr sp,=0x34000000 //处理异常调用C函数,设置und模式下的栈,以前是svc模式
//保存现场
stmdb sp!,{r0-r12,lr}
//处理异常
mrs r0,cpsr //und模式下的cpsr寄存器,也可以改为输出spsr低5位和svc模式的cpsr相同
ldr r1,=und_string
bl printException
//恢复现场
ldmia sp!,{r0-r12,pc}^
und_string:
.string "undefined"
.align 4
reset:
...............
...............
...............
注意事项~~~:
.align 4
这句话一定要加,因为字符串可能使紧接着的代码段没有按照4字节对齐 。
① //.align 4 将此句话注释掉:
sdram.elf: file format elf32-littlearm
Disassembly of section .text:
30000000 <_start>:
30000000: ea000009 b 3000002c <und_string+0x8>
30000004: e59ff0ac ldr pc, [pc, #172] ; 300000b8 <.text+0xb8>
30000008 <und_addr>:
30000008: 3000000c andcc r0, r0, ip
3000000c <undef>:
3000000c: e3a0d30d mov sp, #872415232 ; 0x34000000
30000010: e92d5fff stmdb sp!, {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, sl, fp, ip, lr}
30000014: e10f0000 mrs r0, CPSR
30000018: e59f109c ldr r1, [pc, #156] ; 300000bc <.text+0xbc>
3000001c: eb0001fe bl 3000081c <printException>
30000020: e8fd9fff ldmia sp!, {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, sl, fp, ip, pc}^
30000024 <und_string>:
30000024: 65646e75 strvsb r6, [r4, #-3701]!
30000028: 656e6966 strvsb r6, [lr, #-2406]!
3000002c: 14530064 ldrneb r0, [r3], #-100
3000002e <reset>:
3000002e: e3a01453 mov r1, #1392508928 ; 0x530
b reset跳到的居然不是对应的地址,奇怪吧,这就是没有按照字节对齐所导致的!
② 加上.align 4:
sdram.elf: file format elf32-littlearm
Disassembly of section .text:
30000000 <_start>:
30000000: ea00000a b 30000030 <reset>
30000004: e59ff0ac ldr pc, [pc, #172] ; 300000b8 <.text+0xb8>
30000008 <und_addr>:
30000008: 3000000c andcc r0, r0, ip
3000000c <undef>:
3000000c: e3a0d30d mov sp, #872415232 ; 0x34000000
30000010: e92d5fff stmdb sp!, {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, sl, fp, ip, lr}
30000014: e10f0000 mrs r0, CPSR
30000018: e59f109c ldr r1, [pc, #156] ; 300000bc <.text+0xbc>
3000001c: eb000201 bl 30000828 <printException>
30000020: e8fd9fff ldmia sp!, {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, sl, fp, ip, pc}^
30000024 <und_string>:
30000024: 65646e75 strvsb r6, [r4, #-3701]!
30000028: 656e6966 strvsb r6, [lr, #-2406]!
3000002c: 00000064 andeq r0, r0, r4, rrx
30000030 <reset>:
30000030: e3a01453 mov r1, #1392508928 ; 0x53000000
30000034: e3a00000 mov r0, #0 ; 0x0