diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
| commit | 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch) | |
| tree | ed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5300/compiler/p5-compiler/data | |
| download | misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.tar.gz misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.zip | |
Diffstat (limited to 'Homework/cs5300/compiler/p5-compiler/data')
38 files changed, 3498 insertions, 0 deletions
diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/Mars4_5.jar b/Homework/cs5300/compiler/p5-compiler/data/examples/Mars4_5.jar Binary files differnew file mode 100644 index 0000000..0021281 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/Mars4_5.jar diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test1.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test1.asm new file mode 100644 index 0000000..b849530 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test1.asm @@ -0,0 +1,35 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "Hello world" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test10.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test10.asm new file mode 100644 index 0000000..4d0bb1f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test10.asm @@ -0,0 +1,434 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for fib +fib: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 0 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel0 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel1 +datalabel0: +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 1 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel2 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel3 +datalabel2: +datalabel3: +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +sub $t1 $t1 $t2 +sw $t1 -16($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -20($sp) +# Calling function fib +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +sw $t1 -16($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +li $t3 2 +sub $t2 $t2 $t3 +sw $t2 -20($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -12($sp) +lw $t1 -16($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -24($sp) +add $t0 $t0 $t1 +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel4 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 0 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 1 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 2 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 4 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 5 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 6 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 7 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 8 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 9 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 10 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel4: .asciiz "This program prints the first 11 numbers of the Fibonacci sequence" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test11.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test11.asm new file mode 100644 index 0000000..f72fb29 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test11.asm @@ -0,0 +1,92 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel2 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 0 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 4 +j datalabel1 +datalabel2: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 0 through 9." diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test12.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test12.asm new file mode 100644 index 0000000..dbb716b --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test12.asm @@ -0,0 +1,213 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for fib +fib: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 0 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel0 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel1 +datalabel0: +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 1 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel2 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel3 +datalabel2: +datalabel3: +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +sub $t1 $t1 $t2 +sw $t1 -16($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -20($sp) +# Calling function fib +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +sw $t1 -16($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +li $t3 2 +sub $t2 $t2 $t3 +sw $t2 -20($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -12($sp) +lw $t1 -16($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -24($sp) +add $t0 $t0 $t1 +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel4 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +datalabel5: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 12 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel6 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 4 +j datalabel5 +datalabel6: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel4: .asciiz "This program prints the first 12 numbers of the Fibonacci sequence." diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test13.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test13.asm new file mode 100644 index 0000000..a459701 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test13.asm @@ -0,0 +1,296 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +li $t1 0 +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 0 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +li $t1 2 +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 2 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -44 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -44 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -44 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -44 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel2 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -44 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 44 +j datalabel1 +datalabel2: +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 3 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 6 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 6 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +li $t1 6 +mult $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This should print 0, 2, 2, 3, 6 and 36" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test14.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test14.asm new file mode 100644 index 0000000..4c16021 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test14.asm @@ -0,0 +1,289 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for sum +sum: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# i +# sum +# n +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -16 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t0 -20 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel0: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -16 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +# Get n's offset from $sp from the symbol table and initialize n's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of n. +lw $t1 0($t2) +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel1 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -20 +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of sum. +lw $t1 0($t2) +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t3 16 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Get x's base address from the stack. +lw $t3 0($t3) +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t5 4 +# Add the stack pointer address to the offset. +add $t5 $t5 $sp +# Load the value of i. +lw $t4 0($t5) +# Multiply the index by -4. +li $t5 4 +mul $t4 $t4 $t5 +# Add the index offset to the offset. +add $t3 $t3 $t4 +# Load the value of x. +lw $t2 0($t3) +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 20 +j datalabel0 +datalabel1: +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t1 -20 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of sum. +lw $t0 0($t1) +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel2 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel3: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -44 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel4 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -44 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 44 +j datalabel3 +datalabel4: +# println +# Calling function sum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -48($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -40 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Put the address of the array into the register. +# Use the address of a. +move $t1 $t2 +sw $t1 -52($sp) +li $t1 4 +sw $t1 -56($sp) +# Update the stack pointer +add $sp $sp -48 +# Call the function +jal sum +# Restore the stack pointer +add $sp $sp 48 +# Restore $t0-9 registers +lw $t0 -48($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -60($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function sum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -48($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -40 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Put the address of the array into the register. +# Use the address of a. +move $t1 $t2 +sw $t1 -52($sp) +li $t1 8 +sw $t1 -56($sp) +# Update the stack pointer +add $sp $sp -48 +# Call the function +jal sum +# Restore the stack pointer +add $sp $sp 48 +# Restore $t0-9 registers +lw $t0 -48($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -60($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel2: .asciiz "This should print 6 and 28" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test2.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test2.asm new file mode 100644 index 0000000..7f3312a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test2.asm @@ -0,0 +1,89 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 3 +li $t1 4 +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 14 +li $t1 2 +div $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +li $t1 1 +mult $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +li $t1 2 +mult $t0 $t1 +mflo $t0 +li $t1 2 +div $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7 7 7 (separated by newlines)" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test3.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test3.asm new file mode 100644 index 0000000..69f73f3 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test3.asm @@ -0,0 +1,73 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 4 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints the number 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test4.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test4.asm new file mode 100644 index 0000000..db276ba --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test4.asm @@ -0,0 +1,162 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# Update the stack pointer. +addi $sp $sp -8 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 5 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Entering a new scope. +# Symbols in symbol table: +# println +# b +# Update the stack pointer. +addi $sp $sp -4 +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 9 +# complete assignment statement with store +sw $t1 0($t0) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +sub $t1 $zero $t1 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 0 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 4 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 8 +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test5.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test5.asm new file mode 100644 index 0000000..76837bc --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test5.asm @@ -0,0 +1,155 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for foo +foo: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +li $t0 7 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for fum +fum: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 9 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 12 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t1 -8 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of b. +lw $t0 0($t1) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of a. +lw $t1 0($t2) +sub $t0 $t0 $t1 +li $t1 4 +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function foo +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal foo +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function foo +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal foo +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Calling function fum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fum +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test6.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test6.asm new file mode 100644 index 0000000..1668afa --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test6.asm @@ -0,0 +1,138 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# i +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -16($sp) +li $t1 4 +sw $t1 -20($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 5 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of a. +lw $t1 0($t2) +sw $t1 -16($sp) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +sw $t1 -20($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test7.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test7.asm new file mode 100644 index 0000000..8414044 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test7.asm @@ -0,0 +1,141 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for identity +identity: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function identity +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 7 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal identity +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -16($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test8.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test8.asm new file mode 100644 index 0000000..b9a3263 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test8.asm @@ -0,0 +1,164 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for add2 +add2: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -16($sp) +# Evaluate parameters and save to stack +# Calling function add +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -16($sp) +sw $t1 -20($sp) +# Evaluate parameters and save to stack +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of x. +lw $t2 0($t3) +sw $t2 -24($sp) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t3 -8 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of y. +lw $t2 0($t3) +sw $t2 -28($sp) +# Update the stack pointer +add $sp $sp -20 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 20 +# Restore $t0-9 registers +lw $t0 -16($sp) +lw $t1 -20($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -32($sp) +sw $t1 -20($sp) +li $t1 1 +sw $t1 -24($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -16($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -28($sp) +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function add2 +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 2 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal add2 +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -16($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/examples/test9.asm b/Homework/cs5300/compiler/p5-compiler/data/examples/test9.asm new file mode 100644 index 0000000..db843e0 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/examples/test9.asm @@ -0,0 +1,239 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel1 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel3 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel2 +datalabel1: +datalabel2: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t1 $t0 +subi $t0 $t0 1 +bne $t0 $zero datalabel4 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel6 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel5 +datalabel4: +datalabel5: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t1 $t0 +subi $t0 $t0 1 +bne $t0 $zero datalabel7 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel9 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel8 +datalabel7: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel10 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +datalabel8: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 3 +slt $t0 $t1 $t0 +bne $t0 $zero datalabel11 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel13 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel12 +datalabel11: +datalabel12: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 3 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel14 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel16 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel15 +datalabel14: +datalabel15: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t0 $t1 +bne $t0 $zero datalabel17 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel19 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel18 +datalabel17: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel20 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +datalabel18: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints [1..5] correct." +datalabel3: .asciiz "1 correct" +datalabel6: .asciiz "2 not correct" +datalabel9: .asciiz "2 not correct" +datalabel10: .asciiz "2 correct" +datalabel13: .asciiz "3 correct" +datalabel16: .asciiz "4 correct" +datalabel19: .asciiz "5 not correct" +datalabel20: .asciiz "5 correct" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test b/Homework/cs5300/compiler/p5-compiler/data/test new file mode 100644 index 0000000..89d5b0c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test @@ -0,0 +1,23 @@ +#! /bin/bash + +java -jar ./Mars4_5.jar test1.asm +java -jar ./Mars4_5.jar test2.asm +java -jar ./Mars4_5.jar test3.asm +java -jar ./Mars4_5.jar test4.asm +java -jar ./Mars4_5.jar test5.asm +java -jar ./Mars4_5.jar test6.asm +java -jar ./Mars4_5.jar test7.asm +java -jar ./Mars4_5.jar test8.asm +java -jar ./Mars4_5.jar test9.asm +java -jar ./Mars4_5.jar test10.asm +java -jar ./Mars4_5.jar test11.asm +java -jar ./Mars4_5.jar test12.asm +java -jar ./Mars4_5.jar test13.asm +java -jar ./Mars4_5.jar test14.asm + +# This loop runs the Java code to create the .asm then runs the .asm. +#for i in {1..14} +#do +# java -cp ../../out/production/p5-compiler:../../antlr-4.9.1-complete.jar main.Main ../test$i.c +# java -jar ./Mars4_5.jar ../test$i.asm +#done diff --git a/Homework/cs5300/compiler/p5-compiler/data/test1.asm b/Homework/cs5300/compiler/p5-compiler/data/test1.asm new file mode 100644 index 0000000..336d375 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test1.asm @@ -0,0 +1,44 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +# println +move $a0 $t0 +li $v0 1 +syscall +la $a0 string_1 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "Hello world" +string_1: .asciiz "hi" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test1.c b/Homework/cs5300/compiler/p5-compiler/data/test1.c new file mode 100644 index 0000000..fccaead --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test1.c @@ -0,0 +1,5 @@ +void main() +{ + println("Hello world"); + println(7, "hi"); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test10.c b/Homework/cs5300/compiler/p5-compiler/data/test10.c new file mode 100644 index 0000000..a90a21f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test10.c @@ -0,0 +1,20 @@ +int fib(int i) { + if (i == 0) return 1; + if (i == 1) return 1; + return fib(i-1) + fib(i-2); +} + +void main() { + println("This program prints the first 11 numbers of the Fibonacci sequence"); + println(fib(0)); // 1 + println(fib(1)); // 1 + println(fib(2)); // 2 + println(fib(3)); // 3 + println(fib(4)); // 5 + println(fib(5)); // 8 + println(fib(6)); // 13 + println(fib(7)); // 21 + println(fib(8)); // 34 + println(fib(9)); // 55 + println(fib(10)); // 89 +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test11.c b/Homework/cs5300/compiler/p5-compiler/data/test11.c new file mode 100644 index 0000000..865bcf8 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test11.c @@ -0,0 +1,9 @@ +void main() { + int i; + i = 0; + println("This program prints 0 through 9."); + while (i < 10) { + println(i); + i = i + 1; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test12.c b/Homework/cs5300/compiler/p5-compiler/data/test12.c new file mode 100644 index 0000000..4bb7f58 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test12.c @@ -0,0 +1,15 @@ +int fib(int i) { + if (i == 0) return 1; + if (i == 1) return 1; + return fib(i-1) + fib(i-2); +} + +void main() { + int i; + i = 0; + println("This program prints the first 12 numbers of the Fibonacci sequence."); + while (i < 12) { + println(fib(i)); + i = i + 1; + } +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test13.c b/Homework/cs5300/compiler/p5-compiler/data/test13.c new file mode 100644 index 0000000..c1a5c72 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test13.c @@ -0,0 +1,23 @@ +void main() { + int a[10]; + int i; + + println("This should print 0, 2, 2, 3, 6 and 36"); + a[0] = 0; + println(a[0]); + a[2] = 2; + println(a[2]); + + i = 2; + a[i] = i; + println(a[i]); + + i = 0; + while (i < 10) { + a[i] = i; + i = i + 1; + } + println(a[3]); + println(a[6]); + println(a[6]*6); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test14.c b/Homework/cs5300/compiler/p5-compiler/data/test14.c new file mode 100644 index 0000000..45565fa --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test14.c @@ -0,0 +1,27 @@ +// Sum the first n elements of the array +int sum(int x[], int n) { + int i; + int sum; + i = 0; + sum = 0; + while (i < n) { + sum = sum + x[i]; + i = i + 1; + } + return sum; +} + +void main() { + int a[10]; + int i; + + println("This should print 6 and 28"); + i = 0; + while (i < 10) { + a[i] = i; + i = i + 1; + } + + println(sum(a, 4)); + println(sum(a, 8)); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test2.asm b/Homework/cs5300/compiler/p5-compiler/data/test2.asm new file mode 100644 index 0000000..e7792ab --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test2.asm @@ -0,0 +1,86 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +# println +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 3 +li $t1 4 +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 14 +li $t1 2 +div $t0 $t1 +mflo $t2 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +li $t1 1 +mult $t0 $t1 +mflo $t2 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 7 +li $t1 2 +mult $t0 $t1 +mflo $t2 +li $t0 2 +div $t2 $t0 +mflo $t1 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7 7 7 7 (separated by newlines)" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test2.c b/Homework/cs5300/compiler/p5-compiler/data/test2.c new file mode 100644 index 0000000..ec9b22a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test2.c @@ -0,0 +1,9 @@ +void main() +{ + println("This program prints 7 7 7 7 7 (separated by newlines)"); + println(7); + println(3 + 4); + println(14 / 2); + println(7 * 1); + println((7 * 2) / 2); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test3.asm b/Homework/cs5300/compiler/p5-compiler/data/test3.asm new file mode 100644 index 0000000..6afcca1 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test3.asm @@ -0,0 +1,56 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 -4 +add $t0 $t0 $sp +li $t1 3 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 4 +sw $t1 0($t0) +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints the number 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test3.c b/Homework/cs5300/compiler/p5-compiler/data/test3.c new file mode 100644 index 0000000..1083434 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test3.c @@ -0,0 +1,9 @@ +void main() { + int a; + int b; + + println("This program prints the number 7"); + a = 3; + b = 4; + println(a+b); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test4.asm b/Homework/cs5300/compiler/p5-compiler/data/test4.asm new file mode 100644 index 0000000..978e71e --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test4.asm @@ -0,0 +1,118 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +li $t0 -4 +add $t0 $t0 $sp +li $t1 3 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 2 +sw $t1 0($t0) +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# Update the stack pointer. +addi $sp $sp -8 +li $t0 -4 +add $t0 $t0 $sp +li $t1 5 +sw $t1 0($t0) +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 0 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Entering a new scope. +# Symbols in symbol table: +# println +# b +# Update the stack pointer. +addi $sp $sp -4 +li $t0 -4 +add $t0 $t0 $sp +li $t1 9 +sw $t1 0($t0) +li $t0 0 +add $t0 $t0 $sp +li $t1 2 +# Negation +sub $t1, $zero, $t1 +sw $t1 0($t0) +li $t0 0 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -4 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 4 +li $t0 0 +add $t0 $t0 $sp +li $t1 4 +sw $t1 0($t0) +# Exit scope. +addi $sp $sp 8 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test4.c b/Homework/cs5300/compiler/p5-compiler/data/test4.c new file mode 100644 index 0000000..04c877a --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test4.c @@ -0,0 +1,21 @@ +void main() { + int a; + int b; + + println("This program prints 7 7 7"); + a = 3; + b = 2; + { + int a; + a = 5; + println(a + b); + { + int b; + b = 9; + a = -2; + println(a + b); + } + b = 4; + } + println(a + b); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test5.asm b/Homework/cs5300/compiler/p5-compiler/data/test5.asm new file mode 100644 index 0000000..511f09f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test5.asm @@ -0,0 +1,105 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +foo: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 7 +# println +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +jr $ra +fum: +# Entering a new scope. +# Symbols in symbol table: +# a : -4 +# println : 0 +# b : -8 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +li $t1 9 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 12 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -4 +add $t1 $t1 $sp +lw $t1 0($t1) +sub $t2 $t0 $t1 +li $t0 4 +add $t1 $t2 $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -12($sp) +add $sp $sp -12 +jal foo +add $sp $sp 12 +lw $t0 -12($sp) +move $ra $t0 +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +add $sp $sp -4 +jal foo +add $sp $sp 4 +lw $t0 -4($sp) +move $ra $t0 +move $t0 $ra +sw $t0 -4($sp) +add $sp $sp -4 +jal fum +add $sp $sp 4 +lw $t0 -4($sp) +move $ra $t0 +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test5.c b/Homework/cs5300/compiler/p5-compiler/data/test5.c new file mode 100644 index 0000000..835db83 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test5.c @@ -0,0 +1,19 @@ +void foo() { + println(7); +} + +void fum() { + int a; + int b; + a = 9; + b = 12; + println(b-a+4); + + foo(); +} + +void main() { + println("This program prints 7 7 7"); + foo(); + fum(); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test6.asm b/Homework/cs5300/compiler/p5-compiler/data/test6.asm new file mode 100644 index 0000000..83a0a59 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test6.asm @@ -0,0 +1,94 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +add: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# i : -12 +# y : -8 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# println +move $a0 $t2 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# a : -4 +# println : 0 +# b : -8 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -12($sp) +li $t1 3 +sw $t1 -16($sp) +li $t1 4 +sw $t1 -20($sp) +add $sp $sp -12 +jal add +add $sp $sp 12 +lw $t0 -12($sp) +move $ra $t0 +li $t0 -4 +add $t0 $t0 $sp +li $t1 5 +sw $t1 0($t0) +li $t0 -8 +add $t0 $t0 $sp +li $t1 2 +sw $t1 0($t0) +move $t0 $ra +sw $t0 -12($sp) +li $t1 -4 +add $t1 $t1 $sp +lw $t1 0($t1) +sw $t1 -16($sp) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +sw $t1 -20($sp) +add $sp $sp -12 +jal add +add $sp $sp 12 +lw $t0 -12($sp) +move $ra $t0 +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test6.c b/Homework/cs5300/compiler/p5-compiler/data/test6.c new file mode 100644 index 0000000..03de49c --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test6.c @@ -0,0 +1,15 @@ +void add(int x, int y) { + int i; + println(x+y); +} + +void main() { + int a; + int b; + println("This program prints 7 7"); + add(3, 4); + + a = 5; + b = 2; + add(a, b); +} diff --git a/Homework/cs5300/compiler/p5-compiler/data/test7.asm b/Homework/cs5300/compiler/p5-compiler/data/test7.asm new file mode 100644 index 0000000..a84c1c9 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test7.asm @@ -0,0 +1,107 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +identity: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# return : -8 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +# symbol table size - -8 +sw $t0 -8($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +add: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# y : -8 +# return : -12 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# symbol table size - -12 +sw $t2 -12($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +li $t1 7 +sw $t1 -8($sp) +add $sp $sp -4 +jal identity +add $sp $sp 4 +lw $t0 -4($sp) +# symbol table size - -8 +lw $t1 -12($sp) +move $ra $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +li $t1 3 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +add $sp $sp -4 +jal add +add $sp $sp 4 +lw $t0 -4($sp) +# symbol table size - -12 +lw $t1 -16($sp) +move $ra $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test7.c b/Homework/cs5300/compiler/p5-compiler/data/test7.c new file mode 100644 index 0000000..e7f45a5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test7.c @@ -0,0 +1,14 @@ +int identity(int x) { + return x; +} + +int add(int x, int y) { + return x+y; +} + +void main() { + println("This program prints 7 7"); + println(identity(7)); + println(add(3, 4)); +} + diff --git a/Homework/cs5300/compiler/p5-compiler/data/test8.asm b/Homework/cs5300/compiler/p5-compiler/data/test8.asm new file mode 100644 index 0000000..09281f5 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test8.asm @@ -0,0 +1,118 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main +add: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# y : -8 +# return : -12 +# Update the stack pointer. +addi $sp $sp 0 +li $t0 -4 +add $t0 $t0 $sp +lw $t0 0($t0) +li $t1 -8 +add $t1 $t1 $sp +lw $t1 0($t1) +add $t2 $t0 $t1 +# symbol table size - -12 +sw $t2 -12($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +add2: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# x : -4 +# y : -8 +# return : -12 +# Update the stack pointer. +addi $sp $sp 0 +move $t0 $ra +sw $t0 -16($sp) +move $t1 $ra +sw $t0 -16($sp) +sw $t1 -20($sp) +li $t2 -4 +add $t2 $t2 $sp +lw $t2 0($t2) +sw $t2 -24($sp) +li $t2 -8 +add $t2 $t2 $sp +lw $t2 0($t2) +sw $t2 -28($sp) +add $sp $sp -20 +jal add +add $sp $sp 20 +lw $t0 -16($sp) +lw $t1 -20($sp) +# symbol table size - -28 +lw $t2 -32($sp) +move $ra $t1 +sw $t2 -20($sp) +li $t1 1 +sw $t1 -24($sp) +add $sp $sp -16 +jal add +add $sp $sp 16 +lw $t0 -16($sp) +# symbol table size - -24 +lw $t1 -28($sp) +move $ra $t0 +# symbol table size - -12 +sw $t1 -12($sp) +# Exit scope. +addi $sp $sp 0 +jr $ra +main: +# Entering a new scope. +# Symbols in symbol table: +# println : 0 +# Update the stack pointer. +addi $sp $sp 0 +# println +la $a0 string_0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +move $t0 $ra +sw $t0 -4($sp) +li $t1 2 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +add $sp $sp -4 +jal add2 +add $sp $sp 4 +lw $t0 -4($sp) +# symbol table size - -12 +lw $t1 -16($sp) +move $ra $t0 +# println +move $a0 $t1 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exit scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +string_0: .asciiz "This program prints 7" diff --git a/Homework/cs5300/compiler/p5-compiler/data/test8.c b/Homework/cs5300/compiler/p5-compiler/data/test8.c new file mode 100644 index 0000000..df0f2ec --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test8.c @@ -0,0 +1,14 @@ + +int add(int x, int y) { + return x+y; +} + +int add2(int x, int y) { + return add(add(x,y),1); +} + +void main() { + println("This program prints 7"); + println(add2(2, 4)); +} + diff --git a/Homework/cs5300/compiler/p5-compiler/data/test9.c b/Homework/cs5300/compiler/p5-compiler/data/test9.c new file mode 100644 index 0000000..1aacb59 --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/data/test9.c @@ -0,0 +1,27 @@ +void main() { + int a; + a = 3; + println("This program prints [1..5] correct."); + if (a < 4) { + println("1 correct"); + } + if (a > 4) { + println("2 not correct"); + } + if (a > 4) { + println("2 not correct"); + } else { + println("2 correct"); + } + if (a <= 3) { + println("3 correct"); + } + if (a == 3) { + println("4 correct"); + } + if (a >= 4) { + println("5 not correct"); + } else { + println("5 correct"); + } +} |
