Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631754
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:14:08

;

; ARM汇编编程-逻辑控制

; 实现if for while do...while switch

;

                    

                     AREA     TEST,CODE,READONLY     ; 声明代码段TEST

                     ENTRY                        ; 标识程序入口

                     CODE32                      ; 声明32ARM指令

                    

START           ; if(x>y) z=100;

                     ;  else  z=50;

                     ; xR0yR1zR2 (xyz均为无符号整数)

                     MOV             R0,#76           ; 初始化x的值

                     MOV             R1,#243         ; 初始化y的值

                     CMP              R0,R1            ; 判断x>y?

                     MOVHI   R2,#100         ; x>y条件正确,z=100

                     MOVLS  R2,#50           ; 条件失败,z=50

                    

                     ; for(i=0; i<10; i++)

                     ; {  x++;

                     ; }

                     ; xR0iR2 (ix均为无符号整数)

                     MOV             R0,#0             ; 初始化x的值

                     MOV             R2,#0             ; 设置i=0

FOR_L1         CMP              R2,#10           ; 判断i<10?

                     BHS        FOR_END             ; R2>=10,退出循环

                    

                     ADD              R0,R0,#1 ; 循环体,x++

                    

                     ADD              R2,R2,#1 ; i++

                     B            FOR_L1        

FOR_END             NOP

 

 

                     ; while(x<=y) 

                     ; {  x *= 2;

                     ; }

                     ; xR0yR1 (xy均为无符号整数)

                     MOV             R0,#1             ; 初始化x的值

                     MOV             R1,#20           ; 初始化y的值

                     B            WHILE_L2     ; 首先要判断条件

                    

WHILE_L1     MOV             R0,R0,LSL #1 ; 循环体,x *= 2

 

WHILE_L2     CMP              R0,R1            ; 判断xy?               

                     BLS        WHILE_L1     ; 若条件正确,继续循环

WHILE_END  NOP

 

 

                     ; do

                     ; {  x--;

                     ; }   while(x>0);

                     ; xR0 (x为无符号整数)

                     MOV             R0,#5             ; 初始化x的值

                    

DOWHILE_L1       ADD              R0,R0,#-1      ; 循环体,x--

                    

DOWHILE_L2       MOVS    R0,R0            ; R0 <= R0,并影响条件码标志        

                     BNE        DOWHILE_L1       ; R0不为0(x不为0),则继续循环

DOWHILE_END    NOP                    

                    

                    

                     ; switch(key&0x0F)

                     ; {  case  0:

                     ;    case  2:

                     ;    case  3:  x = key + y;

                     ;              break;

                     ;    case  5:  x = key - y;

                     ;              break;

                     ;    case  7:  x = key * y;

                     ;              break;

                     ;    default:  x = 168;

                     ;              break;      

                     ; }

                     ; xR0yR1keyR2 (xykey均为无符号整数)

                     MOV             R1,#3             ; 初始化y的值

                     MOV             R2,#2             ; 初始化key的值

 

SWITCH        AND              R2,R2,#0x0F  ; switch(key&0x0F)

             

CASE_0         CMP              R2,#0             ; case      0:

CASE_2         CMPNE  R2,#2             ; case      2:

CASE_3         CMPNE  R2,#3             ; case      3:

                     BNE        CASE_5 

                     ADD              R0,R2,R1       ;            x = key + y

                     B            SWITCH_END      ;            break

                    

CASE_5         CMP              R2,#5             ; case  5:

                     BNE        CASE_7

                     SUB        R0,R2,R1       ;            x = key - y

                     B            SWITCH_END      ;            break

                    

CASE_7         CMP              R2,#7             ; case      7:

                     BNE        DEFAULT            

                     MUL              R0,R2,R1       ;             x = key * y

                     B            SWITCH_END  ;               break

                    

DEFAULT             MOV             R0,#168         ; default: x = 168

                    

SWITCH_END      NOP

                    

 

HALT             B            HALT            

      

                     END      

阅读(121) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~