//-------------------------------------------------------------------------------------------------- // // @ CopyRight Roberti & Parau Enterprises, Inc. 2021-2023 // // This work is licensed under the Creative Commons Attribution 4.0 International License. // To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/ // or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. // //-------------------------------------------------------------------------------------------------- // // This code provides two entry points, one two insert a node into an AVL tree, // the other to remove a node // // In this example a node is composed of a pointer to a null terminated string // The nodes are inserted so that during a in-order traversal the strings // appear in sorted order. // // The code is divided into two parts: // // 1. The in-line macro that compare two strings belonging to two nodes // // 2. The SECTION that contains the two entry points // //-------------------------------------------------------------------------------------------------- // // Macro InsCompString compare the strings of two nodes // // It carry out the string comparison as the strcmp C function, // and branch to the correct labels passed to the macro base on // the comparison results // #MACRO //MACRO InsCompString Node1, Node2, GtLbl, EqLbl, WReg[4-32] var addr1= WReg[0]; var addr2= WReg[1]; var wreg0= WReg[2]; var wreg1= WReg[3]; \#Label LD #addr1, 24[#Node1] // Load string addr from node 1 \ LD #addr2, 24[#Node2] // Load string addr from node 2 \ WHILE // Start DO block \ LBU #wreg0, 0[#addr1] // Load first string byte \ LBU #wreg1, 0[#addr2] // Load second string byte \ IF (#wreg0 > #wreg1), GOTO, ID= #GtLbl // Done greater than \ IF (#wreg0 < #wreg1), BREAK // Done if less than \ IF ( #wreg1 == 0 ), GOTO, ID= #EqLbl // Equal condition with equal flag \ ADDI #addr1, 1 // Get next char address (string 1) \ ADDI #addr2, 1 // Get next char address (string 2) \ ENDWHILE #END // // Actual code starts here // SETENV "RISCV", "RV64I:a,c,d,m,n,zicsr,zifencei", "LP64D", "linux" // Define the srchitecture // BaseDef // Include standdard definitions // Code TextSect // Start .text section C.SUSPEND // Do not use two byte opCode extension // dvasmavlinsert /> Entry label ENTRY Stack=!"" // Leaf entry, only A* and T* registers // are used. No need to use a stack // or save/restore registers AVL.INSERT 0[a0], a1, InsCompString, duplKey, [t0-6,a2] // Invoke AVL.INSERT macro EXIT // Return to caller // duplKey ADDI a0, -1[0] // Duplicate key handler EXIT // Return -1 to caller // dvasmavlremove /> Entry label ENTRY Stack=!"" // Leaf entry, only A* and T* registers // are used. No need to use a stack // or save/restore registers AVL.REMOVE 0[A0], A1, [A2-6,T0-2] // Invoke AVL.REMOVE macro EXIT // Return to caller // END