Untitled
unknown
plain_text
3 years ago
6.7 kB
3
Indexable
Never
# Ali Tavassoly # 98108395 .macro input_integer # macro for input integer addi $v0, $zero, 5 syscall .end_macro .macro output_enter # macro for enter output addi $v0, $zero, 11 addi $a0, $zero, 10 syscall .end_macro .macro output_space # macro for space output addi $a0, $zero, 32 addi $v0, $zero, 11 syscall .end_macro .macro input_string # macro for string input la $a0, file_name add $a1, $zero, 1024 addi $v0, $zero, 8 syscall .end_macro .macro open_file (%flag) # macro for openning file la $a0, file_name addi $a1, $zero, %flag addi $v0, $zero, 13 syscall .end_macro .macro close_file addi $v0, $zero, 16 add $a0, $zero, $t2 # file descriptor syscall # close file .end_macro .text main: la $a0, stack_size_message addi $v0, $zero, 4 syscall # Input size of stack input_integer add $s7, $zero, $v0 # size of stack add $a0, $zero, $v0 # size of stack mul $a0, $a0, 4 # number of bytes that is needed add $t0, $zero, $a0 addi $t0, $t0, 4 # number of bytes for alternative stack addi $v0, $zero, 9 syscall # allocating memory add $s6, $zero, $v0 # address of alternative memory addi $v0, $zero, 9 add $a0, $zero, $t0 syscall # allocating memory for alternative stack add $s0, $zero, $v0 # address of allocated memory add $s1, $zero, $v0 # pointer to the top of the stack for: la $a0, input_message addi $v0, $zero, 4 syscall # print input message input_integer add $t0, $zero, $v0 # input a number beq $t0, 0, zero beq $t0, 1, one beq $t0, 2, two beq $t0, 3, three beq $t0, 4, four beq $t0, 5, five beq $t0, 6, six beq $t0, 7, seven la $a0, outrange_error addi $v0, $zero, 4 syscall # if the number is outranged j for zero: j end_for one: sub $t3, $s1, $s0 div $t3, $t3, 4 beq $t3, $s7, StackOverFlow_error input_integer # input a number to push add $a0, $zero, $v0 jal push # push the number j for two: beq $s0, $s1, StackOverFlow_error jal pop # pop add $a0, $zero, $v0 addi $v0, $zero, 1 syscall # print the popped number output_enter # print enter j for three: add $t0, $zero, $s0 while: beq $t0, $s1, end_while # end while condition lw $a0, ($t0) addi $v0, $zero, 1 syscall # print the i-th number output_space addi $t0, $t0, 4 # next iterate j while end_while: output_enter j for four: sub $t3, $s1, $s0 div $t3, $t3, 4 ble $t3, 1, NotEnoughElements_error jal pop add $t0, $zero, $v0 # first element jal pop add $t1, $zero, $v0 # second element add $a0, $t0, $t1 jal push j for five: sub $t3, $s1, $s0 div $t3, $t3, 4 ble $t3, 1, NotEnoughElements_error jal pop add $t0, $zero, $v0 # first element jal pop add $t1, $zero, $v0 # second element mul $a0, $t0, $t1 jal push j for six: input_string open_file(1) add $t2, $zero, $v0 # file descriptor add $a1, $zero, $s0 sub $a2, $s1, $s0 # number of bytes to be written add $a0, $zero, $t2 # file descriptor addi $v0, $zero, 15 syscall # write file close_file j for seven: input_string open_file(0) add $t2, $zero, $v0 # file descriptor blt $v0, 0, FileNotFound_error # if the file does not exit! mul $t5, $s7, 4 addi $t5, $t5, 4 # size of current stack + 1 add $a1, $zero, $s6 add $a2, $zero, $t5 # read size + 1 of stack add $a0, $zero, $t2 # file descriptor addi $v0, $zero, 14 syscall # read file add $t5, $zero, $v0 close_file bne $t5, $s7, SizeNotMatch_error open_file(0) add $t2, $zero, $v0 # file descriptor add $a1, $zero, $s0 addi $a2, $zero, 2048 add $a0, $zero, $t2 # file descriptor addi $v0, $zero, 14 syscall # read file add $s1, $s0, $v0 # update top of stack close_file j for end_for: j main_return StackOverFlow_error: la $a0, StackOverFlow_error_message addi $v0, $zero, 4 syscall # if the stack is full j for StackEmpty_error: la $a0, StackEmpty_error_message addi $v0, $zero, 4 syscall # if the stack is empty j for NotEnoughElements_error: la $a0, NotEnoughElements_error_message addi $v0, $zero, 4 syscall # if there isn't enough elements j for FileNotFound_error: la $a0, FileNotFound_error_message addi $v0, $zero, 4 syscall # if the file doesn't exit j for SizeNotMatch_error: la $a0, SizeNotMatch_error_message addi $v0, $zero, 4 syscall # if size of stack in the file does not match to the current size j for main_return: addi $v0, $zero, 10 syscall push: sw $a0, ($s1) addi $s1, $s1, 4 jr $ra pop: addi $s1, $s1, -4 lw $v0, ($s1) jr $ra .data jump_table: .space 36 input_message: .asciiz "Enter a number between 0 to 7:\n" stack_size_message: .asciiz "Enter size of the stack:\n" outrange_error: .asciiz "Please input a number between 0 to 7 \n" StackOverFlow_error_message: .asciiz "Stack is already full!\n" StackEmpty_error_message: .asciiz "Stack is empty! \n" NotEnoughElements_error_message: .asciiz "For summation and multipication, there should be at least 2 elements! \n" FileNotFound_error_message : .asciiz "The file you have entered, doesn't exit! \n" SizeNotMatch_error_message : .asciiz "Size of stacks does not match! \n" file_name: .asciiz