Last active
December 11, 2015 03:19
-
-
Save blueintegral/4536556 to your computer and use it in GitHub Desktop.
Compute parity of a number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Hunter Scott | |
#Version for MARS | |
#declare and reserve space for data and variables after .data | |
.data | |
#string constant | |
title: .asciiz "Program computes the pariety of number" | |
prompt: .asciiz "Input a number please: " | |
o_title: .asciiz "The pariety is " | |
# newline junk | |
newline: .byte 10,13,00,00 | |
#force address back to word boundry | |
.align 2 | |
#integer variables | |
n: .word 0 | |
pariety: .word 0 | |
#instructions come after .text directive | |
.text | |
main: | |
#print everything out | |
#puts title | |
li $v0, 4 | |
la $a0, title | |
syscall | |
#print out carriage return and line feed | |
#putc '\n' | |
li $v0, 4 | |
la $a0, newline | |
syscall | |
#print out carriage return and line feed | |
#putc '\n' | |
li $v0, 4 | |
la $a0, newline | |
syscall | |
#prompt for user input | |
#puts prompt | |
li $v0, 4 | |
la $a0, prompt | |
syscall | |
#read an integer, N | |
#geti $a0 | |
li $v0, 5 | |
syscall | |
add $a0, $v0, $zero #now the number read in is in $a0 | |
# | |
###### Real program logic starts here ####### | |
# | |
# | |
#prepare for subroutine | |
addi $sp, $sp, -16 #allocate space on stack | |
sw $ra, 4($sp) #push ra on stack | |
sw $a0, 0($sp) #push number to test on stack | |
jal PARIETY | |
#print carriage return and line feed | |
#putc '\n' | |
li $v0, 4 | |
la $a0, newline | |
syscall | |
#print out output title screen | |
#puts o_title | |
li $v0, 4 | |
la $a0, o_title | |
syscall | |
#load result | |
lw $a0, 12($sp) | |
#print out result | |
#puti $a0 | |
li $v0, 1 | |
syscall | |
#print out carriage return and line feed | |
#putc '\n' | |
li $v0, 4 | |
la $a0, newline | |
syscall | |
#end program and return to simulator | |
#done | |
li $v0, 10 | |
syscall | |
###END OF PROGRAM### | |
PARIETY: | |
#sw $ra, 8($sp) #push new $ra on stack | |
lw $v0, 0($sp) #load n | |
addi $t0, $0, 32 #reference | |
addi $t1, $0, 1 #reference for AND | |
add $t2, $0, $0 #initialize number of 1s count to 0 | |
LOOP: beq $t0, $0, DONE #see if we have checked all 32 bits | |
and $t3, $v0, $t1 #AND together 1 and input | |
srl $v0, $v0, 1 #shift right | |
addi $t0, $t0, -1 #decrement iteration counter | |
bne $t3, $t1, LOOP #if the result is not a 1, loop | |
addi $t2, $t2, 1 #increase numbers of ones found counter | |
j LOOP #then loop back | |
DONE: and $t3, $t2, $t1 #once we've iterated through all 32 bits, bitmask the number of 1s counter with an AND to isolate last bit | |
add $a1, $0, $0 #assume result is even, if it's not, it'll get changed below. | |
sgt $a1, $t3, $0 #if the last bit was greater than 0, input was odd, so flag is 1 | |
sw $a1, 12($sp) #store flag result | |
#lw $ra, 8($sp) #pop $ra from stack | |
jr $ra #return back to caller | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment