diff options
author | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-10 09:17:11 -0600 |
---|---|---|
committer | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-10 09:17:11 -0600 |
commit | 5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 (patch) | |
tree | 600ad8b1ec5aad5155baf8c0352281054a8e6366 /data/examples | |
download | cminus-5f28f80c4e25a56cd444914c2f0b3da5e7fdb088.tar.gz cminus-5f28f80c4e25a56cd444914c2f0b3da5e7fdb088.zip |
Initial commit - building
Diffstat (limited to 'data/examples')
-rw-r--r-- | data/examples/.DS_Store | bin | 0 -> 10244 bytes | |||
-rw-r--r-- | data/examples/Mars4_5.jar | bin | 0 -> 4169142 bytes | |||
-rw-r--r-- | data/examples/test1.asm | 35 | ||||
-rw-r--r-- | data/examples/test10.asm | 434 | ||||
-rw-r--r-- | data/examples/test11.asm | 92 | ||||
-rw-r--r-- | data/examples/test12.asm | 213 | ||||
-rw-r--r-- | data/examples/test13.asm | 296 | ||||
-rw-r--r-- | data/examples/test14.asm | 289 | ||||
-rw-r--r-- | data/examples/test2.asm | 89 | ||||
-rw-r--r-- | data/examples/test3.asm | 73 | ||||
-rw-r--r-- | data/examples/test4.asm | 162 | ||||
-rw-r--r-- | data/examples/test5.asm | 155 | ||||
-rw-r--r-- | data/examples/test6.asm | 138 | ||||
-rw-r--r-- | data/examples/test7.asm | 141 | ||||
-rw-r--r-- | data/examples/test8.asm | 164 | ||||
-rw-r--r-- | data/examples/test9.asm | 239 |
16 files changed, 2520 insertions, 0 deletions
diff --git a/data/examples/.DS_Store b/data/examples/.DS_Store Binary files differnew file mode 100644 index 0000000..bd8fb93 --- /dev/null +++ b/data/examples/.DS_Store diff --git a/data/examples/Mars4_5.jar b/data/examples/Mars4_5.jar Binary files differnew file mode 100644 index 0000000..0021281 --- /dev/null +++ b/data/examples/Mars4_5.jar diff --git a/data/examples/test1.asm b/data/examples/test1.asm new file mode 100644 index 0000000..b849530 --- /dev/null +++ b/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/data/examples/test10.asm b/data/examples/test10.asm new file mode 100644 index 0000000..4d0bb1f --- /dev/null +++ b/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/data/examples/test11.asm b/data/examples/test11.asm new file mode 100644 index 0000000..f72fb29 --- /dev/null +++ b/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/data/examples/test12.asm b/data/examples/test12.asm new file mode 100644 index 0000000..dbb716b --- /dev/null +++ b/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/data/examples/test13.asm b/data/examples/test13.asm new file mode 100644 index 0000000..a459701 --- /dev/null +++ b/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/data/examples/test14.asm b/data/examples/test14.asm new file mode 100644 index 0000000..4c16021 --- /dev/null +++ b/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/data/examples/test2.asm b/data/examples/test2.asm new file mode 100644 index 0000000..7f3312a --- /dev/null +++ b/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/data/examples/test3.asm b/data/examples/test3.asm new file mode 100644 index 0000000..69f73f3 --- /dev/null +++ b/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/data/examples/test4.asm b/data/examples/test4.asm new file mode 100644 index 0000000..db276ba --- /dev/null +++ b/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/data/examples/test5.asm b/data/examples/test5.asm new file mode 100644 index 0000000..76837bc --- /dev/null +++ b/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/data/examples/test6.asm b/data/examples/test6.asm new file mode 100644 index 0000000..1668afa --- /dev/null +++ b/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/data/examples/test7.asm b/data/examples/test7.asm new file mode 100644 index 0000000..8414044 --- /dev/null +++ b/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/data/examples/test8.asm b/data/examples/test8.asm new file mode 100644 index 0000000..b9a3263 --- /dev/null +++ b/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/data/examples/test9.asm b/data/examples/test9.asm new file mode 100644 index 0000000..db843e0 --- /dev/null +++ b/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" |