【ARM】ARM汇编程序设计(三) 循环结构

00. 目录

文章目录

    • 00. 目录
    • 01. 循环结构-死循环
    • 02. 循环结构-累加和
    • 03. 循环结构-循环输出
    • 04. 循环结构-循环输出
    • 05. 随机数
    • 06. 综合应用一
    • 07. 综合应用二
    • 08. 综合应用三
    • 09. 附录

01. 循环结构-死循环

第一种方式:死循环

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

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

loop:
    b loop

    mov r0, #0
    pop {pc}

第二种方式:死循环

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

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


    b .

    mov r0, #0
    pop {pc}

第三种方式:死循环

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

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


    sub pc, pc, #8

    mov r0, #0
    pop {pc}

02. 循环结构-累加和

输出1 + 2 + 3 + … + 10的和:

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

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


    mov r1, #0

loop:
    add r1, r1, #1
    cmp r1, #10
    blt loop

    ldr r0, =.LC0
    bl printf


    mov r0, #0
    pop {pc}

03. 循环结构-循环输出

方式一:输出1到10之间的数据

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

    .section .text
    .align 2
    .global main
main:
    push {lr}
    
    mov r4, #1

loop:

    ldr r0, =.LC0
    mov r1, r4
    bl printf

    add r4, r4, #1
    cmp r4, #10
    ble loop

    mov r0, #0
    pop {pc}

方式二:输出1到10之间的数据

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

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


    mov r1, #1

loop:

    ldr r0, =.LC0
    push {r1}
    bl printf
    pop {r1}

    add r1, r1, #1
    cmp r1, #10
    ble loop



    mov r0, #0
    pop {pc}

04. 循环结构-循环输出

方式一:输出1到100之内的所有奇数

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

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


    mov r4, #1

loop:
    mov r1, r4
    ldr r0, =.LC0
    bl printf

    add r4, r4, #2
    cmp r4, #100
    ble loop

    

    mov r0, #0
    pop {pc}

方式二:输出1到100之内的所有奇数

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

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


    mov r4, #1

loop:
    mov r1, r4
    ldr r0, =.LC0
    ands r2, r4, #1
    cmp r2, #0
    blne printf

    add r4, r4, #2
    cmp r4, #100
    ble loop

    

    mov r0, #0
    pop {pc}

方式三:输出1到100之内的所有奇数

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

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


    mov r4, #1

loop:
    mov r1, r4
    ldr r0, =.LC0
    @ands r2, r4, #1
    @cmp r2, #0
    tst r4, #1
    blne printf

    add r4, r4, #2
    @cmp r4, #100
    subs r1, r4, #100
    ble loop

    

    mov r0, #0
    pop {pc}

05. 随机数

产生100个随机数,输出其中的奇数

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

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


    bl getpid
    bl srandom

    mov r4, #1

loop:

    bl random

    mov r1, r0
    tst r1, #1

    ldr r0, =.LC0
    blne printf


    add r4, r4, #1
    cmp r4, #100
    ble loop
    

    mov r0, #0
    pop {pc}

产生100个0-255间的随机数,输出其中的奇数

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

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


    bl getpid
    bl srandom

    mov r4, #1

loop:

    bl random

    mov r1, r0
    and r1, #0xff
    tst r1, #1

    ldr r0, =.LC0
    blne printf


    add r4, r4, #1
    cmp r4, #100
    ble loop
    

    mov r0, #0
    pop {pc}

06. 综合应用一

输出九九乘法表

    .section .rodata
    .align 2
.LC0:
    .string "%2d*%2d=%2d "
.LC1:
    .string "\n"

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


    mov r4, #1
loop:

    mov r5, #1
loop2:

    ldr r0, =.LC0
    mov r1, r5
    mov r2, r4
    mul r3, r4, r5
    bl printf

    add r5, r5, #1
    cmp r5, r4
    ble loop2

    ldr r0, =.LC1
    bl printf


    add r4, r4, #1
    cmp r4, #9
    ble loop
    

    mov r0, #0
    pop {pc}

07. 综合应用二

输出棱形

    .section .rodata
    .align 2
.LC0:
    .string " "

.LC1:
    .string "*"

.LC2:
    .string "\n"

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

    mov r4, #0 
loop:
    cmp r4, #10
    bge out1


        mov r5, #0
    loop1:
        rsb r6, r4, #9
        cmp r5, r6
        bge out2

        ldr r0, =.LC0
        bl printf


        add r5, r5, #1
        cmp r5, r6
        blt loop1
    
    out2:

        mov r5, #0
    loop2:
        mov r7, #2
        mul r6, r4, r7
        add r6, r6, #1
        cmp r5, r6
        bgt out3

        ldr r0, =.LC1
        bl printf

        add r5, r5, #1
        cmp r5, r6
        blt loop2
     

    out3:

        ldr r0, =.LC2
        bl printf


    add r4, r4, #1
    cmp r4, #10
    blt loop

out1:


    @for (i = 0; i < 9; i++)
    mov r4, #0
loop3:

    cmp r4, #9
    bge out4

    mov r5, #0
    loop4:
        cmp r5, r4
        bgt out5

        ldr r0, =.LC0
        bl printf

        add r5, r5, #1
        cmp r5, r5
        ble loop4

    out5:

    mov r5, #0
    loop5:
        rsb r7, r4, #9
        mov r8, #2
        mul r6, r7, r8
        sub r6, r6, #1

        cmp r5, r6
        bge out6

        ldr r0, =.LC1
        bl printf

        add r5, r5, #1
        cmp r5, r6
        blt loop5


    out6:

    ldr r0, =.LC2
    bl printf

    add r4, r4, #1
    cmp r4, #9
    blt loop3

out4:

    mov r0, #0
    pop {pc}

执行结果

[root@itcast 2nd]# ./a.out  
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *
[root@itcast 2nd]# 

08. 综合应用三

输出棱形

    .section .rodata
    .align 2
.LC0:
    .string " "

.LC1:
    .string "*"

.LC2:
    .string "\n"

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

    mov r4, #0 
2:
    cmp r4, #10
    bge 2f


        mov r5, #0
    1:
        rsb r6, r4, #9
        cmp r5, r6
        bge 1f 

        ldr r0, =.LC0
        bl printf


        add r5, r5, #1
        cmp r5, r6
        blt 1b
    
    1:

        mov r5, #0
    1:
        mov r7, #2
        mul r6, r4, r7
        add r6, r6, #1
        cmp r5, r6
        bgt 1f 

        ldr r0, =.LC1
        bl printf

        add r5, r5, #1
        cmp r5, r6
        blt 1b 
     

    1:

        ldr r0, =.LC2
        bl printf


    add r4, r4, #1
    cmp r4, #10
    blt 2b 

2:


    @for (i = 0; i < 9; i++)
    mov r4, #0
2:

    cmp r4, #9
    bge 2f

    mov r5, #0
    1:
        cmp r5, r4
        bgt 1f 

        ldr r0, =.LC0
        bl printf

        add r5, r5, #1
        cmp r5, r5
        ble 1b 

    1:

    mov r5, #0
    1:
        rsb r7, r4, #9
        mov r8, #2
        mul r6, r7, r8
        sub r6, r6, #1

        cmp r5, r6
        bge 1f

        ldr r0, =.LC1
        bl printf

        add r5, r5, #1
        cmp r5, r6
        blt 1b


    1:

    ldr r0, =.LC2
    bl printf

    add r4, r4, #1
    cmp r4, #9
    blt 2b

2:

    mov r0, #0
    pop {pc}

执行结果

[root@itcast 2nd]# ./a.out  
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *
[root@itcast 2nd]# 

09. 附录

你可能感兴趣的:(ARM,ARM,ARM汇编,ARM汇编循环结构,循环结构,ARM汇编实现循环)