找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

帖子
查看: 3055|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

msp430單片機(jī)做二分法查找例子加代碼注釋

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:445441 發(fā)表于 2018-12-13 10:17 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
.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

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表