飞道的博客

【ARM】ARM汇编程序设计(五) str和ldr

507人阅读  评论(0)

00. 目录

01. ldr伪指令

ldr示例一:

    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}
	
    ldr r0, =.LC0
    @r0 = *(.L0)
    ldr r1, .L0
    bl printf

    mov r0, #0
    pop {pc}

.L0:
    .word 0x11112222

执行结果

[root@itcast 3rd]# ./a.out  
val = 0x11112222
[root@itcast 3rd]# 

ldr示例二:


    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}

    ldr r0, =.LC0
    ldr r1, .L0
    bl printf

    ldr r0, =.LC0
    ldr r1, .L0 + 4
    bl printf

    ldr r0, =.LC0
    ldr r1, .L0 + 8
    bl printf

    mov r0, #0
    pop {pc}


.L0:
    .word 0x1111
    .word 0x2222
    .word 0x3333
    .word 0x4444
    .word 0x5555
    .word 0x6666
    .word 0x0

执行结果:

[root@itcast 3rd]# ./a.out  
val = 0x1111
val = 0x2222
val = 0x3333
[root@itcast 3rd]# 

02. ldr指令

程序示例一:

    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}


    ldr r4, =.L0
1:
    ldr r0, =.LC0
    @r1 = *(r4)
    ldr r1, [r4]
    
    cmp r1, #0
    beq 1f

    bl printf

    add r4, #4
    b 1b

1:
    mov r0, #0
    pop {pc}

.L0:
    .word 0x1111
    .word 0x2222
    .word 0x3333
    .word 0x4444
    .word 0x5555
    .word 0x6666
    .word 0x0

程序示例二:


    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}


    ldr r4, =.L0
1:
    ldr r0, =.LC0
    @r1 = *(r4); r4 = r4 + 4;
    ldr r1, [r4], #4
    
    cmp r1, #0
    beq 1f

    bl printf

    b 1b

1:

    mov r0, #0
    pop {pc}


.L0:
    .word 0x1111
    .word 0x2222
    .word 0x3333
    .word 0x4444
    .word 0x5555
    .word 0x6666
    .word 0x0

程序示例三:

    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}


    ldr r4, =.L0
    mov r5, #0
1:
    ldr r0, =.LC0
    ldr r1, [r4, r5]
    
    cmp r1, #0
    beq 1f

    bl printf

    add r5, r5, #4
    b 1b

1:
    mov r0, #0
    pop {pc}

.L0:
    .word 0x1111
    .word 0x2222
    .word 0x3333
    .word 0x4444
    .word 0x5555
    .word 0x6666
    .word 0x0

程序示例四:


    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}


    ldr r4, =.L0
    mov r5, #0
1:
    ldr r0, =.LC0
    @ r1 = *(r4 + r5 << 2)
    ldr r1, [r4, r5, LSL #2]
    
    cmp r1, #0
    beq 1f

    bl printf

    add r5, r5, #1
    b 1b

1:

    mov r0, #0
    pop {pc}


.L0:
    .word 0x1111
    .word 0x2222
    .word 0x3333
    .word 0x4444
    .word 0x5555
    .word 0x6666
    .word 0x0

程序示例五:


    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"

.LC1:
    .string "ar[%d] = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}


    ldr r4, =.L0
    mov r5, #0
1:
    ldr r0, =.LC1
    ldr r2, [r4, r5, LSL #2]
    
    cmp r2, #0
    beq 1f

    mov r1, r5
    bl printf

    add r5, r5, #1
    b 1b

1:

    mov r0, #0
    pop {pc}


.L0:
    .word 0x1111
    .word 0x2222
    .word 0x3333
    .word 0x4444
    .word 0x5555
    .word 0x6666
    .word 0x0

程序示例六:


    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"

.LC1:
    .string "ar[%d] = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}


    ldr r4, =.L0
    mov r5, #0
1:
    ldr r0, =.LC1
    ldrh r2, [r4], #2
    
    cmp r2, #0
    beq 1f

    mov r1, r5
    bl printf

    add r5, r5, #1
    b 1b

1:

    mov r0, #0
    pop {pc}


.L0:
    .word 0x11111111
    .word 0x22222222
    .word 0x33333333
    .word 0x44444444
    .word 0x55555555
    .word 0x66666666
    .word 0x0

执行结果

[root@itcast 3rd]# ./a.out  
ar[0] = 0x1111
ar[1] = 0x1111
ar[2] = 0x2222
ar[3] = 0x2222
ar[4] = 0x3333
ar[5] = 0x3333
ar[6] = 0x4444
ar[7] = 0x4444
ar[8] = 0x5555
ar[9] = 0x5555
ar[10] = 0x6666
ar[11] = 0x6666
[root@itcast 3rd]# 

程序示例七:


    .section .rodata
    .align 2
.LC0:
    .string "val = %p\n"

.LC1:
    .string "ar[%d] = %p\n"


    .section .text
    .align 2
    .global main
main:
    push {lr}


    ldr r4, =.L0
    mov r5, #0
1:
    ldr r0, =.LC1
    ldrb r2, [r4], #1
    
    cmp r2, #0
    beq 1f

    mov r1, r5
    bl printf

    add r5, r5, #1
    b 1b

1:
    mov r0, #0
    pop {pc}

.L0:
    .word 0x11111111
    .word 0x22222222
    .word 0x33333333
    .word 0x44444444
    .word 0x55555555
    .word 0x66666666
    .word 0x0

程序示例八:

	.section	.rodata
	.align	2
.LC0:
	.string	"val = %p \n"

	.text
	.align	2
	.global	main
main:
	push {lr}

	ldr r0, =.LC0
	ldr r2, =.L1
	ldr r1, [r2, #-4095]
	bl	printf

	mov r0, #0
	pop {pc}
.L0:
	.word	0x11111111
	.word	0x22222222
.L1:
	.word	0x33333333
	.word	0x44444444
	.word	0x0

程序示例九:


    .section .rodata
    .align 2
.LC0:
    .string "hello world\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}

    @ldr r0, .L0
    @ldr r0, =.LC0
    ldr r0, [pc, #8]
    bl printf

    mov r0, #0
    pop {pc}


.L0:
    .word .LC0

03. adr指令

程序示例


    .section .rodata
    .align 2
.LC0:
    .string "hello world\n"
.LC1:
    .string "val = %p\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}

    ldr r0, =.LC1
    adr r2, .L0
    ldr r1, [r2]
    bl printf

    mov r0, #0
    pop {pc}


.L0:
    .word 0x11223344

04. str指令

程序示例一:


    .section .rodata
    .align 2
.LC0:
    .string "hello world\n"
.LC1:
    .string "val = %p\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}

    ldr r4, =.L0
1:
    ldr r0, =.LC1
    ldr r1, [r4], #4
    cmp r1, #0
    beq 1f
    
    bl printf

    b 1b

1:

    ldr r4, =.L0
    mov r1, #1
1:
    str r1, [r4], #4
    add r1, #1
    cmp r1, #4
    ble 1b

    ldr r4, =.L0
1:
    ldr r0, =.LC1
    ldr r1, [r4], #4
    cmp r1, #0
    beq 1f
    
    bl printf
    b 1b

1:
    mov r0, #0
    pop {pc}

    .section .data
    .align 2
    .global .L0
.L0:
    .word 0x1111
    .word 0x2222
    .word 0x3333
    .word 0x4444
    .word 0x0

程序示例二:


    .section .rodata
    .align 2
.LC0:
    .string "hello world\n"
.LC1:
    .string "val = %p\n"
.LC2:
    .string "ar[%d] = %d\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}

    sub sp, sp, #16

    mov r1, #0
1:
    str r1, [sp], #4
    add r1, #1
    cmp r1, #4
    bne 1b


    mov r4, #0
    mov r5, #-4
1:
    mov r1, r4
    ldr r2, [sp, r5]
    ldr r0, =.LC2
    bl printf

    add r4, #1
    sub r5, #4
    cmp r4, #4
    bne 1b

    mov r0, #0
    pop {pc}


程序示例三:


    .section .rodata
    .align 2
.LC0:
    .string "hello world\n"
.LC1:
    .string "val = %p\n"
.LC2:
    .string "ar[%d] = %d\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}

    sub sp, sp, #16
    mov r4, sp

    mov r1, #0
1:
    str r1, [r4], #4
    add r1, #1
    cmp r1, #4
    bne 1b


    mov r4, #0
1:
    mov r1, r4
    ldr r2, [sp, r4, LSL #2]
    ldr r0, =.LC2
    bl printf

    add r4, #1
    cmp r4, #4
    bne 1b

    add sp, sp, #16
    mov r0, #0
    pop {pc}


程序示例四:


    .section .rodata
    .align 2
.LC0:
    .string "hello world\n"
.LC1:
    .string "val = %p\n"
.LC2:
    .string "ar[%d] = %d\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}

    sub sp, sp, #16
    mov r4, sp

    mov r1, #0
1:
    str r1, [r4], #4
    add r1, #1
    cmp r1, #4
    bne 1b


    mov r5, sp
    mov r4, #0
1:
    mov r1, r4
    ldr r2, [sp, r4, LSL #2]
    ldr r0, =.LC2
    bl printf

    add r4, #1
    cmp r4, #4
    bne 1b

    add sp, sp, #16
    mov r0, #0
    pop {pc}


程序示例五:


    .section .rodata
    .align 2
.LC0:
    .string "hello world\n"
.LC1:
    .string "val = %p\n"
.LC2:
    .string "ar[%d] = %d\n"

    .section .text
    .align 2
    .global main
main:
    push {lr}

    sub sp, sp, #12
    mov r4, sp

    mov r1, #0
1:
    strb r1, [r4], #1
    add r1, #1
    cmp r1, #12
    bne 1b


    mov r5, sp
    mov r4, #0
1:
    mov r1, r4
    ldrb r2, [r5, r4]
    ldr r0, =.LC2
    bl printf

    add r4, #1
    cmp r4, #12
    bne 1b

    add sp, sp, #12
    mov r0, #0
    pop {pc}


05. 附录

8.1 【ARM】Load Store指令


转载:https://blog.csdn.net/dengjin20104042056/article/details/106905340
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场