Skip to content

Instantly share code, notes, and snippets.

@RGamberini
Last active October 20, 2016 23:24
Show Gist options
  • Save RGamberini/3bfa67f453dc10123adccc1830620ba0 to your computer and use it in GitHub Desktop.
Save RGamberini/3bfa67f453dc10123adccc1830620ba0 to your computer and use it in GitHub Desktop.
.data
line: .asciiz "\n"
adding: .asciiz "Adding three\n"
.macro print_string($funcArg)
la $a0, $funcArg
li $v0, 4
syscall
.end_macro
.macro print_bin($toPrint)
move $a0, $toPrint
li $v0, 35
syscall
print_string(line)
.end_macro
.macro invert($toInvert)
xori $toInvert, $toInvert, 0xFFFFFFFF
.end_macro
.macro left_justify($toJustify, $leftMask, $zeros)
shiftLeft:
and $t3, $toJustify, $leftMask # Let $t3 = the left most bit
bnez $t3, end
sll $toJustify, $toJustify, 1
addi $zeros, $zeros, 1
j shiftLeft
end:
.end_macro
.text
li $t1, -1 # Let $t1 = k
li $s0, 243 # Let $s0 = two's complement int
print_bin($s0)
li $t2, 0x00000001
sll $t2, $t2, 31 # Let $t2 = left most bit
move $t4, $zero # Let $t4 = result = 0
move $t5, $zero # Let $t5 = number of preceding zeros in $s0
left_justify($s0, $t2, $t5)
subi $t5, $t5, 16
abs $t5, $t5 # Let $t5 = the number of bits to go through
outerloop:
addi $t1, $t1, 1 # Iterate k
sll $t4, $t4, 1 # Shift the result over to make room for the incoming bit
beq $t1, $t5, done # Once we're through the whole register (32 bits) finish
and $t3, $s0, $t2 # Let $t3 = the left most bit of $s0
srl $t3, $t3, 31 # Right justify $t3
or $t4, $t4, $t3 # Set the right most bit of $t4 (result) to $t3
print_bin($s0)
print_bin($t4)
sll $s0, $s0, 1 # Move the original number over
li $t6, -1 # Let $t6 = i
li $t7, 0x0000000F # Let $t7 = mask
innerloop: # In the inner loop we have to check if any of the the words are over 4
addi $t6, $t6, 1 # i++
beq $t6, 4, outerloop
and $t8, $t4, $t7 # Let $t8 = our current word
bgt $t8, 4, greaterThanFour
j continue
greaterThanFour:
print_string(adding)
addi $t8, $t8, 3
invert($t7)
and $t4, $t4, $t7
or $t4, $t4, $t8
invert($t7)
j outerloop
continue:
sll $t7, $t7 4
j innerloop
done:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment