【ARM】ARM汇编程序设计(五) str和ldr
发布日期:2021-06-29 20:47:54 浏览次数:2 分类:技术文章

本文共 7449 字,大约阅读时间需要 24 分钟。

00. 目录

文章目录

01. ldr伪指令

ldr示例一:

.section .rodata    .align 2.LC0:    .string "val = %p\n"    .section .text    .align 2    .global mainmain:    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 mainmain:    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 = 0x1111val = 0x2222val = 0x3333[root@itcast 3rd]#

02. ldr指令

程序示例一:

.section .rodata    .align 2.LC0:    .string "val = %p\n"    .section .text    .align 2    .global mainmain:    push {lr}    ldr r4, =.L01:    ldr r0, =.LC0    @r1 = *(r4)    ldr r1, [r4]        cmp r1, #0    beq 1f    bl printf    add r4, #4    b 1b1:    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 mainmain:    push {lr}    ldr r4, =.L01:    ldr r0, =.LC0    @r1 = *(r4); r4 = r4 + 4;    ldr r1, [r4], #4        cmp r1, #0    beq 1f    bl printf    b 1b1:    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 mainmain:    push {lr}    ldr r4, =.L0    mov r5, #01:    ldr r0, =.LC0    ldr r1, [r4, r5]        cmp r1, #0    beq 1f    bl printf    add r5, r5, #4    b 1b1:    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 mainmain:    push {lr}    ldr r4, =.L0    mov r5, #01:    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 1b1:    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 mainmain:    push {lr}    ldr r4, =.L0    mov r5, #01:    ldr r0, =.LC1    ldr r2, [r4, r5, LSL #2]        cmp r2, #0    beq 1f    mov r1, r5    bl printf    add r5, r5, #1    b 1b1:    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 mainmain:    push {lr}    ldr r4, =.L0    mov r5, #01:    ldr r0, =.LC1    ldrh r2, [r4], #2        cmp r2, #0    beq 1f    mov r1, r5    bl printf    add r5, r5, #1    b 1b1:    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] = 0x1111ar[1] = 0x1111ar[2] = 0x2222ar[3] = 0x2222ar[4] = 0x3333ar[5] = 0x3333ar[6] = 0x4444ar[7] = 0x4444ar[8] = 0x5555ar[9] = 0x5555ar[10] = 0x6666ar[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 mainmain:    push {lr}    ldr r4, =.L0    mov r5, #01:    ldr r0, =.LC1    ldrb r2, [r4], #1        cmp r2, #0    beq 1f    mov r1, r5    bl printf    add r5, r5, #1    b 1b1:    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	mainmain:	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 mainmain:    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 mainmain:    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 mainmain:    push {lr}    ldr r4, =.L01:    ldr r0, =.LC1    ldr r1, [r4], #4    cmp r1, #0    beq 1f        bl printf    b 1b1:    ldr r4, =.L0    mov r1, #11:    str r1, [r4], #4    add r1, #1    cmp r1, #4    ble 1b    ldr r4, =.L01:    ldr r0, =.LC1    ldr r1, [r4], #4    cmp r1, #0    beq 1f        bl printf    b 1b1:    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 mainmain:    push {lr}    sub sp, sp, #16    mov r1, #01:    str r1, [sp], #4    add r1, #1    cmp r1, #4    bne 1b    mov r4, #0    mov r5, #-41:    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 mainmain:    push {lr}    sub sp, sp, #16    mov r4, sp    mov r1, #01:    str r1, [r4], #4    add r1, #1    cmp r1, #4    bne 1b    mov r4, #01:    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 mainmain:    push {lr}    sub sp, sp, #16    mov r4, sp    mov r1, #01:    str r1, [r4], #4    add r1, #1    cmp r1, #4    bne 1b    mov r5, sp    mov r4, #01:    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 mainmain:    push {lr}    sub sp, sp, #12    mov r4, sp    mov r1, #01:    strb r1, [r4], #1    add r1, #1    cmp r1, #12    bne 1b    mov r5, sp    mov r4, #01:    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

转载地址:https://dengjin.blog.csdn.net/article/details/106905340 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【ARM】ARM汇编程序设计(六) stm和ldm
下一篇:【ARM】ARM汇编程序设计(四) 选择结构

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月28日 05时54分30秒