A simple binary tree program -- in assembler

I believe every programmer must learn assembly programming. It brings the programmer and the machine closer and strengthens the bond between them. I learned the term GIGO (Garbage In Garbage Out) when I got first introduced to computer programming. Nothing has changed since. The languages and their environments have become better beyond doubt. Still, a computer and its resources are as good as its programmer.

Assembly language makes you understand how the machine is working beyond all that glittery higher level languages. If you are a system programmer, learning assembly becomes a must because there are things that C can't handle. I will leave more discussion on "Why learning assembly is important" for later post.

Every new architecture that I learn, I learn its assembler first. When I got first introduced to MIPS, I learned its assembly language. Below is a program that I wrote to achieve that.
 
 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#
# This program takes 10 integer values from user,
# inserts them into a binary tree and then prints
# the tree "inorder"
#
#
# The tree_node is like this:
#   struct tree_node {
#       int value;
#       struct tree_node *left;
#       struct tree_node *right;
#    }


.text

#-----------------------------------------------------------
#
# This function prints a given node.
# The nodes pointer is provided in $a0.
#
print_node:
    subu    $sp, $sp, 32            # space on stack
    sw      $ra, 28($sp)            # store the return address
    sw      $fp, 24($sp)            # store the current frame pointer.
    addiu    $fp, $sp, 32           # $fp to start of stack.
   
    move    $t0, $a0                # save the nodes pointer to $t0
    lw      $a0, ($t0)              # move the integer value to $a0.
    jal     print_int_with_newline  # print the integer.
    nop

    lw      $ra, 28($sp)
    lw      $fp, 24($sp)
    addiu   $sp, $sp, 32            # reclaim the stack.

    jr      $ra

#----------------------------------------------------------


# This function prints the given integer and
# then prints a new line after it.
#   $a0 - Integer to be printed.
print_int_with_newline:
    subu    $sp, $sp, 32    #minium stack frame
    sw    $ra, 28($sp)    #store the return address.
    sw    $fp, 24($sp)    #store the frame pointer.
    sw    $a0, 20($sp)    #store the argument.
    addiu    $fp, $sp, 32    #base of the frame.

    #print the integer first.
    li    $v0, 1
    syscall

    #print the new line.
    la    $a0, newline
    li    $v0, 4
    syscall

    #regenerate the previous frame.
    lw    $ra, 28($sp)
    lw    $fp, 24($sp)
    lw    $a0, 20($sp)
    addiu    $sp, $sp, 32    #reclaim the stack frame.

    jr    $ra        #return to caller.

#------------------------------------------------------------

# This function creates a new tree node, assigns a given
# value to it. Initializes it properly and returns the
# pointer to it.
#   $a0 -> Integer to be assigned in value.
#   $v0 -> on success, returns with node pointer here else zero.
#
create_tree_node:
    subu    $sp, $sp, 32
    sw      $ra, 28($sp)
    sw      $fp, 24($sp)
    addiu   $fp, $sp, 32

    move    $t0, $a0
    li      $a0, 12
    li      $v0, 9
    syscall

    beqz    $v0, out_of_memory
   
    sw      $t0, ($v0)
    sw      $zero, 4($v0)
    sw      $zero, 8($v0)

out_of_memory:
    lw      $ra, 28($sp)
    lw      $fp, 24($sp)
    addiu   $sp, $sp, 32

    jr      $ra
#---------------------------------------------------------------

#
# This function gets number of specified integers in the
# given buffer.
#
# $a0 -> Pointer to the destination buffer.
# $a1 -> number of integers to be read out.
#
get_num_integers:
    addiu   $sp, $sp, -32   # min stack.
    sw      $ra, 28($sp)    # store ra
    sw      $fp, 24($sp)    # store callers frame pointer.
    addiu   $fp, $sp, 32    # point to our frame.

    move    $t0, $a0        # destination buffer
    move    $t1, $a1        # number of integers.

read_loop:
    beqz    $t1, read_loop_exit
    addiu   $t1, $t1, -1    # decrement the count

    li      $v0, 5
    syscall

    sw      $v0, ($t0)
    addiu   $t0, $t0, 4     # point to next number(int are 4 bytes)
    b       read_loop
    nop

read_loop_exit:
    lw      $ra, 28($sp)
    lw      $fp, 24($sp)
    addiu   $sp, $sp, 32

    jr     $ra
   

#---------------------------------------------------------------
# This function adds a node to the given node. If it cannot be
# added to the current node, it calls itself recursively until
# it finds a node where it can add the given number.
#
# $a0 -> The starting node
# $a1 -> The node to add.
    .globl add_node_to_tree
add_node_to_tree:
    addiu   $sp, $sp, -32
    sw      $ra, 28($sp)
    sw      $fp, 24($sp)
    sw      $a0, 20($sp)
    sw      $a1, 16($sp)
    addiu   $fp, $sp, 32
   
    lw      $t0, ($a0)   # value of the starting current node.
    lw      $t1, ($a1)   # value of the node to add.

    bgt     $t1, $t0, is_greater
    lw      $t2, 4($a0)  #load the left pointer.
    beqz    $t2, null_left

    move    $a0, $t2     # take this left pointer start loc now.
    jal     add_node_to_tree
    nop

    b       __return_add_node_to_tree
    nop

is_greater:
    lw      $t2, 8($a0)  #load the right pointer.
    beqz    $t2, null_right

    move    $a0, $t2     # take this right pointer start loc now.
    jal     add_node_to_tree
    nop

    b       __return_add_node_to_tree
    nop


null_left: # null left means that we can add the pointer here.
    sw      $a1, 4($a0)
    b       __return_add_node_to_tree
    nop

null_right:
    sw      $a1, 8($a0)
    b       __return_add_node_to_tree
    nop

__return_add_node_to_tree:
    lw      $ra, 28($sp)
    lw      $fp, 24($sp)
    lw      $a0, 20($sp)
    lw      $a1, 16($sp)
    addiu   $sp, $sp, 32
   
    jr     $ra
   
#----------------------------------------------------------------

#
# This function prints the tree in order.
#
# $a0 -> root node.
    .globl print_inorder
print_inorder:
    addiu   $sp, $sp, -32
    sw      $ra, 28($sp)
    sw      $fp, 24($sp)
    sw      $a0, 20($sp)
    addiu   $fp, $sp, 32

    lw      $t1, 4($a0)
    beqz    $t1, print_curr

    move    $a0, $t1
    jal     print_inorder
    nop

print_curr:
    lw      $a0, 20($sp)
    lw      $t0, ($a0)
    move    $a0, $t0
    jal     print_int_with_newline
    nop

    lw      $a0, 20($sp)
    lw      $t1, 8($a0)
    beqz    $t1, __return_print_inorder

    move    $a0, $t1
    jal     print_inorder
    nop

__return_print_inorder:
    lw      $ra, 28($sp)
    lw      $fp, 24($sp)
    lw      $a0, 20($sp)
    addiu   $sp, $sp, 32

    jr      $ra
#------------------------------------------------------------------

main:
    # readout 10 integers in buffer.
    la      $a0, buffer 
    li      $a1, 10
    jal     get_num_integers
    nop

    li      $t6, 10
    la      $t8, buffer
    li      $t7, 0

tree_create_loop:
    beqz    $t6, tree_create_done
    addiu   $t6, $t6, -1
    lw      $a0, ($t8)
    jal     create_tree_node
    addiu   $t8, $t8, 4         #point to next number

    beqz    $v0, alloc_failed
    nop

    beqz    $t7, init_root
    move    $a1, $v0
    move    $a0, $t7

    jal     add_node_to_tree
    nop

    b       tree_create_loop
    nop

init_root:
    move    $t7, $v0
    b       tree_create_loop
    nop
   
   
tree_create_done:
    move    $a0, $t7
    jal     print_inorder
    nop
   
    b       exit
    nop
   
alloc_failed:
    la      $a0, OOM_Error
    li      $v0, 4
    syscall

exit:
    li      $v0, 10
    syscall

   
.data
buffer: .space 1024
OOM_Error: .asciiz "Out of memory!\n"
newline: .asciiz "\n"

Comments

Popular posts from this blog

MIPS Bootstrapping

Handling out of memory (OOM) in embedded systems without swap