【ARM】ARM汇编程序设计(四) 选择结构

00. 目录

文章目录

    • 00. 目录
    • 01. if 结构一
    • 02. if 结构二
    • 03. if 结构三
    • 04. if 结构四
    • 05. if 结构五
    • 06. switch结构
    • 07. 附录

01. if 结构一

    .section .rodata
    .align 2
.LC0:
    .string "i > 10\n"

.LC1:
    .string "i <= 10\n"

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

    mov r2, #1
    cmp r2, #10
    ldrgt r0, =.LC0
    ldrle r0, =.LC1
    
    bl printf

    mov r0, #0
    pop {pc}

02. if 结构二

    .section .rodata
    .align 2
.LC0:
    .string "i > 10\n"

.LC1:
    .string "i <= 10\n"

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

    mov r2, #1
    cmp r2, #10
    bgt do1
    b do2

do1:
    ldr r0, =.LC0
    bl printf
    b out

do2:
    ldr r0, =.LC1
    bl printf

out:

    mov r0, #0
    pop {pc}

03. if 结构三

    .section .rodata
    .align 2
.LC0:
    .string "i > 10\n"

.LC1:
    .string "i < 10\n"

.LC2:
    .string "i == 10\n"

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

    mov r2, #1
    cmp r2, #10
    bgt do1
    blt do2
    b do3

do1:
    ldr r0, =.LC0
    bl printf
    b out

do2:
    ldr r0, =.LC1
    bl printf
    b out

do3:
    ldr r0, =.LC2
    bl printf

out:

    mov r0, #0
    pop {pc}

04. if 结构四

	if(i > 10)
		...
	else if(i + j > 10)
		...
	else if(j < 10)
		...
    .section .rodata
    .align 2
.LC0:
    .string "i > 10\n"

.LC1:
    .string "i + j > 10\n"

.LC2:
    .string "j < 10\n"


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

    mov r2, #100
    mov r3, #2

    cmp r2, #10
    bgt do1
    
    add r1, r2, r3
    cmp r1, #10
    bgt  do2

    cmp r3, #10
    blt do3

    b out

do1:
    ldr r0, =.LC0
    bl printf
    b out

do2:
    ldr r0, =.LC1
    bl printf
    b out

do3:
    ldr r0, =.LC2
    bl printf

out:

    mov r0, #0
    pop {pc}

05. if 结构五

    .section .rodata
    .align 2
.LC0:
    .string "i > 10 && i < 20\n"


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

    mov r2, #15

    cmp r2, #10
    ble out

    cmp r2, #20
    blt do1
    b out

do1:
    ldr r0, =.LC0
    bl printf

out:

    mov r0, #0
    pop {pc}

06. switch结构

C语言代码

#include 

int main(void)
{
	int i = 10;
	char ch = 'a';

	switch(i)
	{
		case 1:
			printf("val = %d \n", i);
			break;
		case 345:
			printf("val = %d \n", i);
			break;
		case 23:	
			printf("val = %d \n", i);
			break;
		case 7:
			printf("val = %d \n", i);
			break;
		case 1234:
			printf("val = %d \n", i);
			break;
		case 17:
			printf("val = %d \n", i);
			break;
		case 10:
			printf("val = %d \n", i);
			break;
	}
	
	return 0;
}

汇编语言代码

	.section	.rodata
	.align	2
.LC0:
	.ascii	"val = %d \012\000"

	.text
	.align	2
	.global	main
main:
	stmfd	sp!, {fp, lr}
	add	fp, sp, #4
	sub	sp, sp, #8
	mov	r3, #10
	str	r3, [fp, #-8]
	mov	r3, #97
	strb	r3, [fp, #-9]
	ldr	r3, [fp, #-8]
	cmp	r3, #17
	beq	.L6
	cmp	r3, #17
	bgt	.L10
	cmp	r3, #7
	beq	.L4
	cmp	r3, #10
	beq	.L5
	cmp	r3, #1
	beq	.L3
	b	.L2
.L10:
	ldr	r2, .L11
	cmp	r3, r2
	beq	.L8
	ldr	r2, .L11+4
	cmp	r3, r2
	beq	.L9
	cmp	r3, #23
	beq	.L7
	b	.L2
.L3:
	ldr	r3, .L11+8
	mov	r0, r3
	ldr	r1, [fp, #-8]
	bl	printf
	b	.L2
.L8:
	ldr	r3, .L11+8
	mov	r0, r3
	ldr	r1, [fp, #-8]
	bl	printf
	b	.L2
.L7:
	ldr	r3, .L11+8
	mov	r0, r3
	ldr	r1, [fp, #-8]
	bl	printf
	b	.L2
.L4:
	ldr	r3, .L11+8
	mov	r0, r3
	ldr	r1, [fp, #-8]
	bl	printf
	b	.L2
.L9:
	ldr	r3, .L11+8
	mov	r0, r3
	ldr	r1, [fp, #-8]
	bl	printf
	b	.L2
.L6:
	ldr	r3, .L11+8
	mov	r0, r3
	ldr	r1, [fp, #-8]
	bl	printf
	b	.L2
.L5:
	ldr	r3, .L11+8
	mov	r0, r3
	ldr	r1, [fp, #-8]
	bl	printf
	mov	r0, r0	@ nop
.L2:
	mov	r3, #0
	mov	r0, r3
	sub	sp, fp, #4
	ldmfd	sp!, {fp, pc}
.L12:
	.align	2
.L11:
	.word	345
	.word	1234
	.word	.LC0
	.size	main, .-main
	.ident	"GCC: (ctng-1.8.1-FA) 4.5.1"
	.section	.note.GNU-stack,"",%progbits

07. 附录

你可能感兴趣的:(ARM,ARM,ARM汇编,if结构,switch结构,汇编选择结构)