# # ###################################################################### # testStack.spim --- test the stack functions for postfix.spim. # This does not test that the $sx registers are being saved # properly. # # Note: two tests requires uncommenting a few lines. Read main. # # Assumptions: # isFull and isEmpty are named just that and return values through # $v0. # # push is named just that and takes the value to be pushed from # $a0. # # pop is named just that and returns a value through $v0. # # The stack is 128 words. ###################################################################### .data nl: .asciiz "\n" full: .asciiz "Trying to push onto a full stack." empty: .asciiz "Trying to pop from an empty stack." .align 2 stack: .space 512 line: .space 80 # Error messages for stack function tests. e1: .asciiz "e1: isEmpty failed on an empty stack." e2: .asciiz "e2: isEmpty failed on a non-empty stack." e3: .asciiz "e3: push failed." e4: .asciiz "e4: pop failed." e5: .asciiz "e5: isEmpty failed on an empty stack." e6: .asciiz "e6: push/pop problems." e7: .asciiz "e7: isFull failed on a full stack." e8: .asciiz "e8: isFull failed on a non-full stack." e9: .asciiz "e9: push/pop problems." e10: .asciiz "e10: push/pop problems." e11: .asciiz "e11: isEmpty failed on an empty stack." e12: .asciiz "e12: can pop from an empty stack." e13: .asciiz "e13: can push to a full stack." e14: .asciiz "e14: Sx registers not all saved." passed: .asciiz "\nWhoopee, all tests passed.\n" s1: .asciiz "Testing isEmpty saves.\n" s2: .asciiz "Testing isFull saves.\n" s3: .asciiz "Testing push saves.\n" s4: .asciiz "Testing pop saves.\n" ###################################################################### # main ###################################################################### .text .globl main main: sub $sp, $sp, 8 sw $fp, 8($sp) sw $ra, 4($sp) add $fp, $sp, 8 # Initialize globals. la $t8, stack addi $t8, $t8, 512 # $t8 is top of stack. Points # to tos element. Stack grows # down in memory. Its size is # 128 words. la $t9, line # $t9 is the line pointer. # Points to next character to # be examined. li $v0, 4 la $a0, nl syscall # Uncomment next three lines to test popping from an empty stack. # jal pop # la $a0, e12 # jal die li $v0, 0 # Test isEmpty on empty stack. jal isEmpty beq $v0, 1, p1 la $a0, e1 jal die .globl p1 p1: li $a0, 0xdeadbeef # Test isEmpty on non-empty stack. jal push li $v0, 1 jal isEmpty beq $v0, 0, p2 la $a0, e2 jal die .globl p2 p2: lw $t0, 0($t8) # Test push the dirty way. beq $t0, 0xdeadbeef, p3 la $a0, e3 jal die .globl p3 p3: jal pop # Test push/pop the clean way. beq $v0, 0xdeadbeef, p4 la $a0, e4 jal die .globl p4 p4: jal isEmpty # Re-test isEmpty. beq $v0, 1, p5 la $a0, e5 jal die .globl p5 p5: li $a0, 0xbeefdead # Test 2 pushed followed by # 2 pops. jal push li $a0, 0xdeadbeef jal push li $v0, 0 jal pop beq $v0, 0xdeadbeef, p6 la $a0, e6 jal die .globl p6 p6: jal pop beq $v0, 0xbeefdead, p7 la $a0, e6 jal die .globl p7 p7: li $t0, 127 # Fill the stack. .globl main_w1 main_w1: bltz $t0, main_ew1 move $a0, $t0 jal push sub $t0, $t0, 1 b main_w1 .globl main_ew1 main_ew1: li $v0, 0 # Test isFull on full stack. jal isFull beq $v0, 1, p8 la $a0, e7 jal die .globl p8 p8: # Uncomment next three lines to test pushing to a full stack. # jal push # la $a0, e13 # jal die jal pop # Test isFull on non-full stack. move $t0, $v0 li $v0, 1 jal isFull beq $v0, 0, p9 la $a0, e8 jal die .globl p9 p9: beqz $t0, p10 # Check the popped value. la $a0, e9 jal die .globl p10 p10: li $t0, 1 # Pop everything from the stack # and check. .globl main_w2 main_w2: beq $t0, 128, main_ew2 jal pop beq $t0, $v0, p11 la $a0, e10 jal die .globl p11 p11: add $t0, $t0, 1 b main_w2 .globl main_ew2 main_ew2: li $v0, 0 # Check isEmpty one more time. jal isEmpty beq $v0, 1, p12 la $a0, e11 jal die # The next four sections check the saving of the Sx # registers. .globl p12 p12: li $v0, 4 # Checking isEmpty. la $a0, s1 syscall jal setSRegs jal isEmpty jal testSRegs li $v0, 4 # Checking isFull. la $a0, s2 syscall jal setSRegs jal isFull jal testSRegs li $v0, 4 # Checking push. la $a0, s3 syscall jal setSRegs jal push jal testSRegs li $v0, 4 # Checking pop. la $a0, s4 syscall jal setSRegs jal pop jal testSRegs li $v0, 4 la $a0, passed syscall lw $ra, 4($sp) lw $fp, 8($sp) add $sp, $sp, 8 jr $ra ###################################################################### # exit - Terminate program execution. ###################################################################### .text .globl exit exit: li $v0, 10 syscall ###################################################################### # die - Print the error message pointed to by the char pointer in $a0 # and terminate. ###################################################################### .text .globl die die: sub $sp, $sp, 8 sw $fp, 8($sp) sw $ra, 4($sp) add $fp, $sp, 8 li $v0, 4 syscall li $v0, 4 la $a0, nl syscall jal exit lw $ra, 4($sp) lw $fp, 8($sp) add $sp, $sp, 8 jr $ra ###################################################################### # setSRegs - Set all Sx registers to specific values for later # checking. Also, scribbles on 8 words below stack frame as an # improvement to the test. ###################################################################### .text .globl setSRegs setSRegs: sub $sp, $sp, 8 sw $fp, 8($sp) sw $ra, 4($sp) add $fp, $sp, 8 li $s0, 0x01234567 # Set all Sx registers. li $s1, 0x12345678 li $s2, 0x23456789 li $s3, 0x3456789a li $s4, 0x456789ab li $s5, 0x56789abc li $s6, 0x6789abcd li $s7, 0x789abcde sub $t0, $sp, 32 # Scribble on 8 words below # stack frame. setSRegs_w1: bgt $t0, $sp, setSRegs_ew1 sw $0, 0($t0) add $t0, $t0, 4 b setSRegs_w1 setSRegs_ew1: lw $ra, 4($sp) lw $fp, 8($sp) add $sp, $sp, 8 jr $ra ###################################################################### # testSRegs - Test that the values previously stored to the Sx # registers are still there. ###################################################################### .text .globl testSRegs testSRegs: sub $sp, $sp, 8 sw $fp, 8($sp) sw $ra, 4($sp) add $fp, $sp, 8 bne $s0, 0x01234567, testSRegs_failed bne $s1, 0x12345678, testSRegs_failed bne $s2, 0x23456789, testSRegs_failed bne $s3, 0x3456789a, testSRegs_failed bne $s4, 0x456789ab, testSRegs_failed bne $s5, 0x56789abc, testSRegs_failed bne $s6, 0x6789abcd, testSRegs_failed bne $s7, 0x789abcde, testSRegs_failed b testSRegs_eif1 testSRegs_failed: la $a0, e14 jal die testSRegs_eif1: lw $ra, 4($sp) lw $fp, 8($sp) add $sp, $sp, 8 jr $ra # Your stack routines are below this line. ######################################################################