arm汇编编程范例

更新时间:2023-11-14 10:48:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

一。if语句实例1 /*

*if(a > 50)

* puts(\r0 r1 r2 r3 *else if(b < a)

* puts(\ *else if(b < 20)

* puts(\ * */ .section .rodata .align 2 .LC0: .string \.LC1: .string \.LC2: .string \ .text .align 2 .global main main: push {lr} @上一个函数的返回地址压到栈中;

@ mov r0, #60 mov r0, #10 mov r1, #30 cmp r0, #50 bgt do1 cmp r1, r0 blt do2 cmp r1, #20 blt do3 b out do1: ldr r0, .L0 bl puts b out do2:

ldr r0, .L0+4 bl puts b out do3: ldr r0, .L0+8 bl puts out: pop {pc} @从栈中取出lr存到pc中; .L0: .word .LC0 .word .LC1 .word .LC2

if语句实例2, &运算 /*

*if (a > 10 && <20)

* puts(\ */ .section .rodata .align 2 .LC0: .string \ .text .align 2 .global main main: push {lr} @上一个函数的返回地址压到栈中; mov r0, #15 @if( (a>10) && (a<20) ) @ puts(\ cmp r0, #10 ble out cmp r0, #20 ldrlt r0, .L0 bllt printf out: pop {pc} @从栈中取出lr存到pc中;

.L0: .word .LC0

if语句实例3, ||运算 .section .rodata .align 2 .LC0: .string \ .text .align 2 .global main main: push {lr} @上一个函数的返回地址压到栈中; mov r0, #15 @if( (a<10) || (a>20) ) @ puts(\ cmp r0, #10 blt do cmp r0, #20 ble out do: ldr r0, .L0 bl puts out: pop {pc} @从栈中取出lr存到pc中; .L0: .word .LC0

二,switch语句的实现1 /*

*char ch = 'A'; *

*switch (ch) *{

* case x: * break; * ... *}

* */ .section .rodata .align 2 .LC0: .ascii \ .text .align 2 .global main main: stmfd sp!, {fp, lr} add fp, sp, #4 sub sp, sp, #16 mov r3, #65 @ch = 'A'; strb r3, [fp, #-5] ldrb r3, [fp, #-5] @ zero_extendqisi2 str r3, [fp, #-16] @int --> switch ldr r3, [fp, #-16] cmp r3, #45 @二分查找; beq .L5 ldr r3, [fp, #-16] cmp r3, #45 bgt .L9 ldr r3, [fp, #-16] cmp r3, #2 beq .L3 ldr r3, [fp, #-16] cmp r3, #9 beq .L4 b .L11 .L9: ldr r3, [fp, #-16] cmp r3, #97 beq .L7 ldr r3, [fp, #-16] cmp r3, #99 beq .L8 ldr r3, [fp, #-16] cmp r3, #65 beq .L6 b .L11

.L7: ldrb r3, [fp, #-5] @ zero_extendqisi2 ldr r0, .L12 mov r1, r3 bl printf b .L11 .L8: ldrb r3, [fp, #-5] @ zero_extendqisi2 ldr r0, .L12 mov r1, r3 bl printf b .L11 .L3: ldrb r3, [fp, #-5] @ zero_extendqisi2 ldr r0, .L12 mov r1, r3 bl printf b .L11 .L4: ldrb r3, [fp, #-5] @ zero_extendqisi2 ldr r0, .L12 mov r1, r3 bl printf b .L11 .L5: ldrb r3, [fp, #-5] @ zero_extendqisi2 ldr r0, .L12 mov r1, r3 bl printf b .L11 .L6: ldrb r3, [fp, #-5] @ zero_extendqisi2 ldr r0, .L12 mov r1, r3 bl printf .L11: sub sp, fp, #4 ldmfd sp!, {fp, pc} .L13: .align 2 .L12: .word .LC0

本文来源:https://www.bwwdw.com/article/vh1v.html

Top