Initial commit - building

This commit is contained in:
Elizabeth Hunt 2023-04-10 09:17:11 -06:00
commit 5f28f80c4e
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
82 changed files with 8759 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

28
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Main",
"request": "launch",
"mainClass": "main.Main",
"projectName": "p5-compiler_25e45799"
},
{
"type": "java",
"name": "Main",
"request": "launch",
"mainClass": "Main",
"projectName": "p5-compiler_25e45799"
},
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
}
]
}

8
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,8 @@
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
"antlr-4.9.1-complete.jar"
],
"rpc.enabled": false,
"java.debug.settings.onBuildFailureProceed": true
}

BIN
antlr-4.9.1-complete.jar Normal file

Binary file not shown.

BIN
data/.DS_Store vendored Normal file

Binary file not shown.

BIN
data/examples/.DS_Store vendored Normal file

Binary file not shown.

BIN
data/examples/Mars4_5.jar Normal file

Binary file not shown.

35
data/examples/test1.asm Normal file
View File

@ -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"

434
data/examples/test10.asm Normal file
View File

@ -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"

92
data/examples/test11.asm Normal file
View File

@ -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."

213
data/examples/test12.asm Normal file
View File

@ -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."

296
data/examples/test13.asm Normal file
View File

@ -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"

289
data/examples/test14.asm Normal file
View File

@ -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"

89
data/examples/test2.asm Normal file
View File

@ -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
<