源程序
.section .rodata
.align 2
.LC0:
.string "hello arm"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
bl puts
mov r0, #0
pop {lr}
bx lr
编译执行
# PC端
[root@itcast 1st]# arm-linux-gcc test.s -o test
[root@itcast 1st]#
# ARM板执行
[root@itcast 1st]# ./test
hello arm
[root@itcast 1st]#
程序改进版本
.section .rodata
.align 2
.LC0:
.string "hello arm"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
bl puts
mov r0, #0
pop {pc}
编译执行
[root@itcast 1st]# arm-linux-gcc test.s -o test
[root@itcast 1st]#
[root@itcast 1st]# ./test
hello arm
[root@itcast 1st]#
程序如下:
.section .rodata
.align 2
.LC0:
.string "val = %d\n"
.LC1:
.string "val = %d %d %d\n"
.LC2:
.string "val = %d %d %d %d\n"
.section .text
.align 2
.global main
main:
push {lr}
@printf("val = %d\n", 123);
ldr r0, =.LC0
mov r1, #123
bl printf
@printf("val = %d %d %d\n", 11, 22, 33);
ldr r0, =.LC1
mov r1, #11
mov r2, #22
mov r3, #33
bl printf
@printf("val = %d %d %d %d\n", 11, 22, 33, 44);
ldr r0, =.LC2
mov r1, #11
mov r2, #22
mov r3, #33
mov r4, #44
push {r4}
bl printf
pop {r4}
mov r0, #0
pop {pc}
执行结果
[root@itcast 1st]# ./a.out
val = 123
val = 11 22 33
val = 11 22 33 44
[root@itcast 1st]#
友情提示
当传递参数超过4个时候,需要使用栈传递参数。
程序示例:
.section .rodata
.align 2
.LC0:
.string "hello arm\n"
.LC1:
.ascii "hello arm\n\0"
.LC2:
.asciz "hello arm\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
bl printf
ldr r0, =.LC1
bl printf
ldr r0, =.LC2
bl printf
mov r0, #0
pop {pc}
友情提示
.string 会在字符串后面自动添加\0
.asciz 会在字符串后面自动添加\0
.ascii 不会在字符串后面自动添加\0
程序示例
.section .rodata
.align 2
.LC0:
.string "val = %d\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r1, #10
bl printf
ldr r0, =.LC0
mov r1, #010
bl printf
ldr r0, =.LC0
mov r1, #0x10
bl printf
ldr r0, =.LC0
mov r1, #0b10
bl printf
mov r0, #0
pop {pc}
执行结果
[root@itcast 1st]# ./a.out
val = 10
val = 8
val = 16
val = 2
[root@itcast 1st]#
友情提示
二进制 0b10
八进制 010
十进制 10
十六进制 0x10
程序示例一: 立即数
.section .rodata
.align 2
.LC0:
.string "val = %d\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
@
@mov r1, #0x123
@mov r1, #0x7f8
@mov r1, #0xc80000003
@mov r1, #0xc80000007
@mov r1, #0x10000008
mov r1, #0xff
bl printf
mov r0, #0
pop {pc}
执行结果:
[root@itcast 1st]# ./a.out
val = 255
[root@itcast 1st]#
程序示例二:移位操作
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #0x100
mov r1, r2, LSL #4
bl printf
mov r0, #0
pop {pc}
执行结果:
[root@itcast 1st]# ./a.out
val = 0x1000
[root@itcast 1st]#
程序示例三: 移位操作
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #0xff000000
@mov r1, r2, LSR #31
@mov r1, r2, ASR #31
mov r1, r2, ROR #31
bl printf
mov r0, #0
pop {pc}
执行结果:
[root@itcast 1st]# ./a.out
val = 0xfe000001
[root@itcast 1st]#
程序示例四:表达式
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r1, #(100/4 + 3 * 2 + 1)
bl printf
mov r0, #0
pop {pc}
程序示例五:优化处理
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r1, #0xffffffff
bl printf
mov r0, #0
pop {pc}
执行结果:
[root@itcast 1st]# ./a.out
val = 0xffffffff
[root@itcast 1st]#
程序示例
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mvn r1, #0xffffff00
bl printf
mov r0, #0
pop {pc}
执行结果:
[root@itcast 1st]# ./a.out
val = 0xff
[root@itcast 1st]#
程序示例:
.section .rodata
.align 2
.LC0:
.string "val = %d\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #1
mov r3, #2
mov r1, r2
@r1 = r2 + r3
@add r1, r2, r3
@r1 = r2 + 8
@add r1, r2, #8
@r1 = r1 + 1
@add r1, r1, #1
@r1 = r1 + 1
@add r1, #1
@r1 = r2 + r3 << 2
add r1, r2, r3, LSL #2
bl printf
mov r0, #0
pop {pc}
执行结果:
[root@itcast 1st]# ./a.out
val = 9
[root@itcast 1st]#
程序示例:
.section .rodata
.align 2
.LC0:
.string "val = %d\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #1
mov r3, #2
mov r1, r2
@r1 = r3 - r2
@sub r1, r3, r2
@r1 =r2 - r3
@sub r1, r2, r3
@r1 = r3 - 1
sub r1, r3, #1
bl printf
mov r0, #0
pop {pc}
程序示例
.section .rodata
.align 2
.LC0:
.string "val = %d\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #1
mov r3, #2
mov r1, r2
@r1 = r3 - r2
@rsb r1, r2, r3
@r1 = 10 - r2
rsb r1, r2, #10
bl printf
mov r0, #0
pop {pc}
程序示例:
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #0xff
mov r3, #0x3
@r1 = r2 & r3
and r1, r2, r3
bl printf
mov r0, #0
pop {pc}
程序示例
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #0xff
mov r3, #0x3
@r1 = r2 & ~r3
bic r1, r2, r3
bl printf
mov r0, #0
pop {pc}
程序示例:
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #0xf0
mov r3, #0x3
@r1 = r2 | r3
orr r1, r2, r3
bl printf
mov r0, #0
pop {pc}
程序示例
.section .rodata
.align 2
.LC0:
.string "val = %p\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #0xff
mov r3, #0x3
@r1 = r2 ^ r3
eor r1, r2, r3
bl printf
mov r0, #0
pop {pc}
程序示例
.section .rodata
.align 2
.LC0:
.string "val = %d\n"
.section .text
.align 2
.global main
main:
push {lr}
ldr r0, =.LC0
mov r2, #3
mov r3, #8
mov r1, #1
@r1 = r2 * r3
@mul r1, r2, r3
@r1 = r1 * r2
mul r1, r1, r2
bl printf
mov r0, #0
pop {pc}