25.12.2012 Views

MIPS Assembly Language Programming

MIPS Assembly Language Programming

MIPS Assembly Language Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.9. TREESORT.ASM 87<br />

39 input_loop:<br />

40 li $v0, 5 # syscall 5 == read_int.<br />

41 syscall<br />

42 move $s1, $v0 # $s1 = read_int<br />

43<br />

44 beq $s1, $s2, end_input # if we read the sentinel, break.<br />

45<br />

46 # tree_insert (number, root);<br />

47 move $a0, $s1 # number= $s1<br />

48 move $a1, $s0 # root = $s0<br />

49 jal tree_insert # call tree_insert.<br />

50<br />

51 b input_loop # repeat input loop.<br />

52 end_input:<br />

53<br />

54 ## Step 3: print out the left and right subtrees.<br />

55 lw $a0, 4($s0) # print the root’s left child.<br />

56 jal tree_print<br />

57<br />

58 lw $a0, 8($s0) # print the root’s right child.<br />

59 jal tree_print<br />

60<br />

61 b exit # exit.<br />

62 ## end of main.<br />

63<br />

64 ## tree_node_create (val, left, right): make a new node with the given<br />

65 ## val and left and right descendants.<br />

66 ## Register usage:<br />

67 ## $s0 - val<br />

68 ## $s1 - left<br />

69 ## $s2 - right<br />

70 tree_node_create:<br />

71 # set up the stack frame:<br />

72 subu $sp, $sp, 32<br />

73 sw $ra, 28($sp)<br />

74 sw $fp, 24($sp)<br />

75 sw $s0, 20($sp)<br />

76 sw $s1, 16($sp)<br />

77 sw $s2, 12($sp)<br />

78 sw $s3, 8($sp)<br />

79 addu $fp, $sp, 32<br />

80 # grab the parameters:<br />

81 move $s0, $a0 # $s0 = val<br />

82 move $s1, $a1 # $s1 = left

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!