找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2444|回复: 0
收起左侧

msp430单片机做二分法查找例子加代码注释

[复制链接]
ID:445441 发表于 2018-12-13 10:17 | 显示全部楼层 |阅读模式
.data
List: .byte 1,3,4,7,9,10,12,17,19,21,26
Last: .byte 1
counter: .byte 1


Goal: .byte 1
Index: .space 1



       .text
       mov.w   #List,R5; move the first address of List to R5
       mov.w   #List+10,R6; same as R5+10 to R6
       mov.w   #Index,R12; move the address of Index to R12
       mov.b   Goal,R8;
       call    #binary; call binary subroutine
       sub.w   #0x2400,R7; R7-2400 give to R7 .The position of the value
       mov.b   R7,0(R12); store the final value
       jmp     $;


binary:
       cmp.b   #10,counter; compare with the mid number
       jge     action; jump to action if greater or equal
       mov.w   R6,R7;
       add.w   R5,R7; the add of the first and end address
       rra.w   R7; R7/2 get the mid address
       mov.b   R7,R10;
       cmp.b   R8,R10; compare to know if we get the Goal
       jge     Left; jump to Left if greater or equal
       jl      Right; jump to Right if less


Left:
       cmp.b   R8,R10; judge if R8=R10
       jeq     Done;
       mov.w   R7,R6; give the high Index to R6
       add.b   #1,counter; counter+1;
       jmp     binary;

Right:
       mov.w   R7,R5; give the low Index to R5
       add.b   #1,counter; counter+1
       jmp     binary;

action:
       add.w   #1,R7;
       ret ;return the result

Done:  ret; do return

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表