Don't like ads? PRO users don't see any ads ;-)

# Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.15 KB  |  hits: 11  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. print:
2.     sw \$1, -4(\$30)
3.     sw \$2, -8(\$30)
4.     sw \$3, -12(\$30)
5.     sw \$4, -16(\$30)
6.     sw \$5, -20(\$30)
7.     sw \$6, -24(\$30)
8.     sw \$7, -28(\$30)
9.     sw \$8, -32(\$30)
10.     sw \$9, -36(\$30)
11.     sw \$10, -40(\$30)
12.     lis \$3
13.     .word -40
14.     add \$30, \$30, \$3            ; allocate space for the saved registers
15.
16.     lis \$3
17.     .word 0xffff000c        ; \$3 = 0xffff000c
18.     lis \$4
19.     .word 10                ; \$4 = 10
20.     lis \$5
21.     .word 4                 ; \$5 = 4
22.     add \$6, \$1, \$0          ; \$6 = \$1
23.     slt \$7, \$1, \$0          ; \$7 = 1 iff \$1 < 0, else \$7 = 0
24.     beq \$7, \$0, IfDone
25.     lis \$8
26.     .word 0x0000002d        ; load - (ascii 2D) into \$8
27.     sw \$8, 0(\$3)            ; print - from \$8
28.     sub \$6, \$0, \$6          ; \$6 = 0 - \$6
29. IfDone:
30.     add \$9, \$30, \$0         ; \$9 = \$30
31.
32. ; calculate all the digits and save them onto the stack
33. ; they are calculated in reverse order.  Popping them off the stack
34. ; to print will put them in the forward order.
35. Loop:
36.     divu \$6, \$4             ; \$6/ \$4 (unsigned)
37.     mfhi \$10                ; \$10 = \$6 % \$4
38.     sw \$10, -4(\$9)          ; mem[\$9] = \$10
39.     mflo \$6                 ; \$6 = \$6 / \$4
40.     sub \$9, \$9, \$5          ; \$9 = \$9 - 4
41.     slt \$10, \$0, \$6         ; \$10 = 1 iff (\$6 > 0) otherwise \$10 = 0
42.     bne \$10, \$0, Loop       ; continue the loop until done
43.
44. ; use second Loop to print the digits in the right order
45.     lis \$7
46.     .word 48                ; load character 0 (ascii 48) into \$7
47. Loop2:
48.     lw \$8, 0(\$9)            ; \$8 = mem[\$9]
49.     add \$8, \$8, \$7         ; calculate the ascii value of the digit
50.     sw \$8, 0(\$3)            ; print the character in \$8
51.     add \$9, \$9, \$5          ; \$9 = \$9 + 4
52.     bne \$9, \$30, Loop2      ; jump the loop
53.     sw \$4, 0(\$3)            ; print character '\\n' (ascii 10)
54.
55.     ; restore saved registers
56.     lis \$3
57.     .word 40
58.     add \$30, \$30, \$3        ; restore the stack pointer
59.     lw \$1, -4(\$30)
60.     lw \$2, -8(\$30)
61.     lw \$3, -12(\$30)
62.     lw \$4, -16(\$30)
63.     lw \$5, -20(\$30)
64.     lw \$6, -24(\$30)
65.     lw \$7, -28(\$30)
66.     lw \$8, -32(\$30)
67.     lw \$9, -36(\$30)
68.     lw \$10, -40(\$30)
69.
70.     jr \$31                  ; return control to the caller