【发布时间】:2022-01-14 14:37:50
【问题描述】:
我是汇编语言的新手,我正在尝试使用以下代码进行二叉搜索树,但是粗体部分的逻辑似乎不正确...有什么解决办法吗?
- 如何比较数组元素?
- 如何在使用循环时比较数组中的元素?
- 我的逻辑正确吗?
AREA bsttest, CODE, READONLY
ENTRY
Reset_Handler
ADR r0,Arr ;r0 pointer to arr (array)
ADR r1,smallest ;r1 pointer to smallest (array)
ADR r2,greatest ;r2 pointer to greatest (array)
MOV r3,#5 ;5 is the size of array
loop
LDRB r5,[r0],#1; load the first element into r5 and set this as root
**LDRB r6,[r0,#1]** ; load the second element into r6 using pre-increment
CMP r6,r5 ; compare r6 and r5
BGE storeatright ; if r6 is greater than r5 jump to the label
;if r6<r5
**CMP r1,#0**; check if there's a value on the left hand side, if LHS=NULL,
BGT jump; if it's not NULL, jump
STRB r6,[r1],#1 ;save and store value at the LHS
jump
STRB r5,[r1],#1 ;save and store value at the LHS
;if r6>r5
storeatright
**CMP r2,#0**; check if there's a value on the right hand side, if RHS=NULL,
BGT jumps; if it's not NULL, jump
STRB r6,[r2],#1 ;save and store value at the RHS
jumps
STRB r5,[r2],#1 ;save and store value at the RHS
SUBS r3,r3,#1 ; i-- for array
BNE loop ; loop until array size=0
Arr DCB 2,1,4,3,5;array
smallest DCB 0,0,0,0,0
greatest DCB 0,0,0,0,0
stop B stop
END```
【问题讨论】: