Skip to content

Instantly share code, notes, and snippets.

@benwrk
Last active December 8, 2016 13:14
Show Gist options
  • Save benwrk/cd1cda58f851e83b4436bf0ed729acf5 to your computer and use it in GitHub Desktop.
Save benwrk/cd1cda58f851e83b4436bf0ed729acf5 to your computer and use it in GitHub Desktop.
Assembly homework solutions for the Computer Organization and Computer Architecture course.
.data
array: .word 0 : 50
size: .word 50
space: .asciiz " "
.text
.globl main
main:
init_value_init:
lw $t3, size
la $t8, array
move $t0, $zero
init_value_loop:
mul $t1, $t0, 4
sw $t0, array($t1)
addi $t0, $t0, 1
blt $t0, $t3, init_value_loop
li $t0, 0
move $t1, $zero
logic:
# sll $t2, $t0, 2 # bit shift left << 2
mul $t2, $t0, 4 # i = i * 4
add $t2, $t2, $t8 # y = a[i]
lw $t1, 4($t2)
lw $t3, ($t2)
sw $t3, 4($t2)
sw $t1, ($t2)
print_loop_init:
move $t0, $zero
lw $t1, size
la $t8, array
print_loop:
mul $t2, $t0, 4
li $v0, 1
lw $a0, array($t2)
syscall
li $v0, 4
la $a0, space
syscall
addi $t0, $t0, 1
blt $t0, $t1, print_loop
.data
array: .word 0 : 50
size: .word 50
space: .asciiz " "
nl: .asciiz "\n"
.text
.globl main
main:
init_value_init:
lw $t3, size
la $t8, array
move $t0, $zero
init_value_loop:
mul $t1, $t0, 4
sw $t0, array($t1)
addi $t0, $t0, 1
blt $t0, $t3, init_value_loop
li $t0, 0
move $t1, $zero
logic:
lw $t1, ($t8)
lw $t2, 4($t8)
sw $t2, ($t8)
lw $t2, 8($t8)
sw $t2, 4($t8)
sw $t1, 8($t8)
slt $t1, $t2, $zero
li $v0, 1
move $a0, $t1
syscall
li $v0, 4
la $a0, nl
syscall
print_loop_init:
move $t0, $zero
lw $t1, size
la $t8, array
print_loop:
mul $t2, $t0, 4
li $v0, 1
lw $a0, array($t2)
syscall
li $v0, 4
la $a0, space
syscall
addi $t0, $t0, 1
blt $t0, $t1, print_loop
.data
array: .word 0 : 50
size: .word 50
space: .asciiz " "
nl: .asciiz "\n"
.text
.globl main
main:
init_value_init:
lw $t3, size
la $t8, array
move $t0, $zero
init_value_loop:
mul $t1, $t0, 4
sw $t0, array($t1)
addi $t0, $t0, 1
blt $t0, $t3, init_value_loop
li $t0, 2
move $t1, $zero
logic:
sll $t2, $t0, 2
add $t2, $t2, $t8
lw $t3, ($t2)
beq $t3, 1, l1
beq $t3, 2, l2
j l3
l1:
addi $t1, $t1, 1
j l4
l2:
addi $t1, $t1, 2
j l4
l3:
addi $t1, $t1, 3
l4:
li $v0, 1
move $a0, $t1
syscall
li $v0, 4
la $a0, nl
syscall
print_loop_init:
move $t0, $zero
lw $t1, size
la $t8, array
print_loop:
mul $t2, $t0, 4
li $v0, 1
lw $a0, array($t2)
syscall
li $v0, 4
la $a0, space
syscall
addi $t0, $t0, 1
blt $t0, $t1, print_loop
exit:
li $v0, 10
syscall
.data
array: .word 0 : 50
size: .word 50
space: .asciiz " "
nl: .asciiz "\n"
.text
.globl main
main:
init_value_init:
lw $t3, size
la $t8, array
move $t0, $zero
init_value_loop:
mul $t1, $t0, 4
sw $t0, array($t1)
addi $t0, $t0, 1
blt $t0, $t3, init_value_loop
li $t0, 0
move $t1, $zero
logic:
mul $t2, $t0, 4
add $t2, $t2, $t8
lw $t3, ($t2)
bne $t3, 1, skip_plus_1
addi $t1, $t1, 1
j skip_plus_3
skip_plus_1:
bne $t3, 2, skip_plus_2
addi $t1, $t1, 2
j skip_plus_3
skip_plus_2:
addi $t1, $t1, 3
skip_plus_3:
li $v0, 1
move $a0, $t1
syscall
li $v0, 4
la $a0, nl
syscall
print_loop_init:
move $t0, $zero
lw $t1, size
la $t8, array
print_loop:
mul $t2, $t0, 4
li $v0, 1
lw $a0, array($t2)
syscall
li $v0, 4
la $a0, space
syscall
addi $t0, $t0, 1
blt $t0, $t1, print_loop
exit:
li $v0, 10
syscall
.data
array: .word 0 : 1000
size: .word 1000
space: .asciiz " "
nl: .asciiz "\n"
.text
.globl main
main:
init_value_init:
lw $t3, size
la $t8, array
move $t0, $zero
init_value_loop:
mul $t1, $t0, 4
sw $t0, array($t1)
addi $t0, $t0, 1
blt $t0, $t3, init_value_loop
li $t0, 0
move $t1, $zero
lw $t4, size
logic_loop:
mul $t2, $t0, 4
add $t2, $t2, $t8
lw $t3, ($t2)
bgt $t3, $t1, increment
j decrement
increment:
addi $t1, $t1, 1
j skip
decrement:
subi $t1, $t1, 1
skip:
addi $t0, $t0, 1
blt $t0, $t4, logic_loop
print_loop_init:
move $t0, $zero
lw $t1, size
la $t8, array
print_loop:
mul $t2, $t0, 4
li $v0, 1
lw $a0, array($t2)
syscall
li $v0, 4
la $a0, space
syscall
addi $t0, $t0, 1
blt $t0, $t1, print_loop
exit:
li $v0, 10
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment