1

I want to make an insertion to a binary search tree in HCS12 Microcontroller Assembly Language. I get the values first and they are in certain adresses. For example; let me assume that the numbers given are 5-3-8-9-1, respectively. And let me assume that we start from the 300th adress. Thus the adress of the numbers are 301,302,303,304,305; respectively. According to this binary search tree; we can understand that 5 is the root,3 is the left child of 5, 8 is the right child of 5, 9 is the right child of 9 and 1 is the left child of 1. Here is the corresponding tree as a result. 5 / \ 3 8 / \ 1 9

Now, I want to write an assembly code to store both the values the adresses of its eft and right child. The result of my program should write the following output to the memory: (First Value)(Adress of its Left Child)(Adress of its Right Child)(Second Value)(Adress of its Left Child)(Adress of its Right Child) .......

Thus, according to this; the program should write the following output to somewhere in a memory:

05 03 02 03 03 03 03 04 00 00 08 00 00 03 05 09 00 00 00 00 01 00 00 00 00 ===> 00 00 means no left or right child.

Here is my first attempt to write the assembly code of it:

 ORG $300
  DC.B 05,03,08,09,01   ; Here we get the values and store them in memory adresses from       301 to 305

 Entry:
  LDS #$500
  LDX #$301
  LDAA 0,X
  STAA $401

 CreateTree:
  LDAA [0,X]
  LDAB [1,X]
  CBA
  BCS WriteLeftChild
  BSR writeRightChild

 writeLeftChild:
  CMPB #$00
  BEQ Exit
  LDAA [1,X]
  STAA 5,X

  Increment ??
  BSR CreateTree

writeRightChild:
  CMPB #$00
  BEQ Exit
  LDAA [1,X]
  STAA 10,X

  Increment ??
  BSR CreateTree

Exit:
  RTS

Can anybody help me to solve this algorithm? Thanks

XTL
  • 851
  • 1
  • 8
  • 23
Aron Feller
  • 65
  • 1
  • 1
  • 9

0 Answers0