Skip to content

Instantly share code, notes, and snippets.

@aflaag
Last active March 21, 2024 09:18
Show Gist options
  • Save aflaag/b58aff6aa4d755fc2ea0f9025cabdd35 to your computer and use it in GitHub Desktop.
Save aflaag/b58aff6aa4d755fc2ea0f9025cabdd35 to your computer and use it in GitHub Desktop.
.globl main
.data
L: .word 3
A: .half 0x002A, 0xA5FF, 0x5BE1
.text
main:
# prepare arguments for the parity function
lw $a1, L
la $a0, A
jal parity
move $a0, $v0 # a0 = v0
li $v0, 1 # print the parity word
syscall
move $a2, $a0 # a2 = a0
jal parity_bit
move $a0, $v1 # a0 = v1
li $v0, 1 # print the parity bit of the parity word
syscall
li $v0, 10 # exit
syscall
parity:
# push stack
subi $sp, $sp, 8
sw $ra, 4($sp)
sw $fp, 0($sp)
# result
move $v0, $zero
# index
move $v0, $zero
# exit condition
sll $t1, $a1, 1 # t1 = a1 << 1 => a1 * 2 (L * half_size)
loopA:
beq $t0, $t1, loopAExit # exit if index == L * half_size
add $t4, $a0, $t0 # t4 = a0 + t0, array_address + index_address => the current half address
lh $a2, 0($t4) # a2 = L[t4]
jal parity_bit # v1 = parity_bit(a2: half_word)
sll $v0, $v0, 1 # v0 <<= 1 making space for the next parity bit
add $v0, $v0, $v1 # v0 += v1, adding the parity bit to the result
addi $t0, $t0, 2 # t0 += 2
j loopA
loopAExit:
# pop stack
lw $fp, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
parity_bit:
# push stack
subi $sp, $sp, 8
sw $ra, 4($sp)
sw $fp, 0($sp)
# result
move $v1, $zero
# bitmask
move $t5, $zero
addi $t5, $t5, 1 # t5 = 1
# bit counter
move $t7, $zero # t7 = 0
# exit condition
move $t6, $zero
addi $t6, $t6, 16
loopHalf:
beq $t7, $t6, loopHalfExit # exit if bit counter == 16 (half_size = 16)
and $s0, $t5, $a2 # s0 = t5 & a2, s0 stores the current bit of the word
srlv $s0, $s0, $t7 # s0 >>= t7, transfer the MSB to the LSB
xor $v1, $v1, $s0 # v1 ^= s0
sll $t5, $t5, 1 # t5 <<= 1, update the bitmask
addi $t7, $t7, 1 # t7 += 1
j loopHalf
loopHalfExit:
# pop stack
lw $fp, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment