【ARM】ARM汇编程序设计(一)

00. 目录

文章目录

    • 00. 目录
    • 01. 第一个汇编程序
    • 02. printf函数
    • 03. 字符串
    • 04. 进制
    • 05. mov指令
    • 06. mvn指令
    • 07. add指令
    • 08. sub指令
    • 09. rsb指令
    • 10. and指令
    • 11. bic指令
    • 12. orr指令
    • 13. eor指令
    • 14. mul指令
    • 15. 附录

01. 第一个汇编程序

源程序

    .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]# 

02. printf函数

程序如下:

    .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个时候,需要使用栈传递参数。

03. 字符串

程序示例:

    .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

04. 进制

程序示例

    .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

05. mov指令

程序示例一: 立即数

    .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]# 

06. mvn指令

程序示例

    .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]# 

07. add指令

程序示例:

    .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]# 

08. sub指令

程序示例:

    .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}

09. rsb指令

程序示例

    .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}

10. and指令

程序示例:

    .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}

11. bic指令

程序示例

    .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}

12. orr指令

程序示例:

    .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}

13. eor指令

程序示例

    .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}

14. mul指令

程序示例

    .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}

15. 附录

你可能感兴趣的:(ARM,ARM,ARM汇编,ARM汇编指令,ARM汇编语言,arm-linux-gcc)