.data
############################################
.eqv ISO_LF 10 # Line feed (newline)
.eqv SYS_PRINT_CHAR 11
.align 2
userinput: .space 100
head: .word 0
tail: .word 0
ask: .asciiz "Enter a number: "
message: .asciiz"\nyour enter number is: "
#########################################################################
# Receiver control. 1 in bit 0 means new char has arrived. This bit
# is read-only, and resets to 0 when CONS_RECEIVER_DATA is read.
# 1 in bit 1 enables hardware interrupt at interrupt level 1.
# Interrupts must also be enabled in the coprocessor 0 status register.
.eqv CONS_RECEIVER_CONTROL 0xffff0000
.eqv CONS_RECEIVER_READY_MASK 0x00000001
.eqv CONS_RECEIVER_DATA 0xffff0004
.eqv DISPLAY_CONTROL 0xffff0008
.eqv CONS_DISPLAY_MASK 0x00000001
.eqv DISPLAY_DATA 0Xffff000c
# Main body
.text
main:
lui $t0,0xffff
li $v0,4
la $a0,ask
syscall
li $v0,5
syscall
li $v0,4
la $a0,message
syscall
li $v0,1
syscall
jal MakeList
li $v0,10
syscall
# Spin-wait for key to be pressed\
waitinloop:
addi $t0,$zero,20
addi $t1,$zero,40
slt $s0,$t0,$t1
bne $s0,$zero,ask
li $v0,10
syscall
waitoutloop:
addi $t0,$zero,10
addi $t1,$zero,30
slt $s0,$t0,$t1
bne $s0,$zero,message
li $v0,10
syscall
key_wait:
lw $t0, CONS_RECEIVER_CONTROL
andi $t0, $t0, CONS_RECEIVER_READY_MASK # Isolate ready bit
beqz $t0, key_wait
# Read in new character from keyboard to low byte of $a0
# and clear other 3 bytes of $a0
lbu $a0, CONS_RECEIVER_DATA
display_wait:
lw $t0, DISPLAY_CONTROL
andi $t0, $t0, CONS_DISPLAY_MASK
beqz $t0,display_wait
sw $a0, DISPLAY_DATA #store it to the display data buffer
# Print character and newline to standard out
# to show it works
li $v0, SYS_PRINT_CHAR
syscall
li $a0, ISO_LF
li $v0, SYS_PRINT_CHAR
syscall
j key_wait
MakeList:
move $t7, $0 #initialize to 0, this will be the head
move $t8, $0 #tail pointer
li $v0, 5 #read an int and put it inot $t1
syscall
move $t1,$v0
MakeList_1:
beqz $t1, MakeList_5
li $a0,8 #malloc 8 bytes from the heap
li $v0,9
syscall
sw $t1, 4($v0) #put int in node
sw $0, ($v0) #initialize next to null
bnez $t7 MakeList_2
#empty list
move $t7, $v0 #head = malloc(2)
move $t8, $t7 #tail = head
b MakeList_3
MakeList_2: #else not empty, add to end
sw $v0, ($t8) #link to tail.next, first word in node
lw $t8, ($t8) #tail = tail.next
MakeList_3:
li $v0, 5
syscall
move $t1,$v0 #read an integer
b MakeList_1
MakeList_5:
sw $t7,head
sw $t8,tail
jr $ra
factorial:
add $sp,$sp,8
sw $ra, ($sp)
sw $s0,4($sp)
li $v0,1
beqz,$a0, done
move $s0, $a0
add $a0,$a0,-1
jal factorial
mul $v0,$v0,$s0
done:
lw $ra,($sp)
lw $s0 ,4($sp)
add $sp,$sp,8
jr $ra
User contributions licensed under CC BY-SA 3.0