Last active
October 19, 2016 18:43
-
-
Save RGamberini/d8ed795caf73cfee0fe542a2cfe79ca7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
.data | |
arg1: .word 0 | |
arg2: .word 0 | |
error: .asciiz "Incorrect argument provided\n" | |
sm: .asciiz "Signed Magnitude: " | |
one: .asciiz "One's Complement: " | |
gray: .asciiz "Gray Code: " | |
dbl: .asciiz "Double Dabble: " | |
msg1: .asciiz "You entered " | |
msg2: .asciiz " which parsed to " | |
msg3: .asciiz "In hex it looks like " | |
line: .asciiz "\n" | |
.macro load_args() | |
lw $t0, 0($a1) # $t0 = argument 1 | |
sw $t0, arg1 # arg1 = t0 = arguement 1 | |
lw $t0, 4($a1) # Go 4 bytes or 1 "Word" over | |
sw $t0, arg2 | |
.end_macro | |
.macro print_string($funcArg) | |
la $a0, $funcArg | |
li $v0, 4 | |
syscall | |
.end_macro | |
.macro atoi($string) | |
lw $t0, $string # Let $t0 = the string | |
move $s1, $0 # Let $s1 = negative bit | |
move $t1, $0 # Let $t1 = the current index | |
move $t2, $0 # Let $t2 = the current sum | |
loop: | |
addu $t3, $t1, $t0 # Let $t3 = the memory address of the current character | |
lbu $t3, ($t3) # Let $t3 = the first byte loaded from $t3 as an ASCII char | |
beqz $t3, out # If we null terminate get out | |
beq $t3, 45, negative # 45 is the minus sign so flip $s1 and continue | |
blt $t3, 48, out # 48 is the lowest possible ASCII digit | |
bgt $t3, 57, out # 57 is the highest possible ASCII digit | |
# Left shifting allows me to multiply by 2^N | |
sll $t4, $t2, 3 # 2^3 = 8 | |
sll $t5, $t2, 1 # 2^1 = 2 | |
add $t2, $t4, $t5 # 8 + 2 = 10 | |
# By combining two left shifts I can multiply the sum by 10 | |
add $t2, $t2, $t3 # Add $t3 to the current sum | |
subi $t2, $t2, 48 # But remember that ASCII digits are padded 48 | |
j continue # Skip over the next two lines | |
negative: # The only time this is executed is if there is a '-' in the string | |
li $s1, 1 # Flip the negative bit | |
continue: | |
addi $t1, $t1, 1 # i++ | |
j loop # Do it all again | |
out: | |
beq $s1, 0, end # If $s0 isn't flipped quit | |
sub $t2, $zero, $t2 # Otherwise negate the sum | |
end: | |
move $s0, $t2 # Hang onto that value | |
.end_macro | |
# Default macro for printing out a message and a number's hexadecimal notation | |
.macro output($string, $hex) | |
print_string(line) | |
print_string($string) # Print hex intro | |
la $a0, ($hex) | |
li $v0, 34 # Print hexadecimal | |
syscall | |
.end_macro | |
.text | |
.globl main | |
main: | |
load_args() # arg1 and arg2 now contain what was passed from MARS | |
atoi(arg1) # s0 now contains the string as an integer | |
print_string(msg1) # Print intro | |
lw $a0, arg1 | |
li $v0, 4 # Print original number | |
syscall | |
print_string(msg2) # Print interlude | |
move $a0, $s0 | |
li $v0, 1 # Print integer | |
syscall | |
output(msg3, $s0) | |
# Now we have to jump to do the next part of the homework | |
lw $t0, arg2 # Let $t0 = arg2 | |
lbu $t0, ($t0) # Load the flag from $t0 | |
beq $t0, 49, ones_complement # 49 = '1' | |
beq $t0, 115, signed_magnitude # 115 = 's' | |
beq $t0, 103, gray_code # 103 = 'g' | |
beq $t0, 100, double_dabble # 100 = 'd' | |
# If we reach this then something has gone wrong | |
print_string(line) | |
print_string(error) | |
j done | |
ones_complement: | |
# You can flip all the bits in the integer by masking it with 0xFFFFFFFF | |
xori $s0, $s0, 0xFFFFFFFF # Let $s0 = one's complement form of the integer | |
output(one, $s0) | |
j done | |
signed_magnitude: | |
abs $s0, $s0 # Let $s0 = the absolute magnitude of $s0 | |
bnez $s1, flipbit # If the number is negative then goto flipbit else continue | |
j continue | |
flipbit: | |
# Masking with 0x80000000 flips the left most bit | |
xori $s0, $s0, 0x80000000 # Let $s0 be the signed mangitude of $s0 | |
continue: | |
output(sm, $s0) | |
j done | |
gray_code: | |
# For gray's number we ignore the fact that $s0 may or may not be negative | |
move $t0, $s0 # Let $t0 = $s0 | |
srl $t0, $t0, 1 # Shift $t0 right 1 | |
xor $s0, $s0, $t0 # XOR $s0 and $t0 | |
output(gray, $s0) | |
double_dabble: | |
move $t0, $s0 # Let $t0 = $s0 = v | |
move $t1, $zero # Let $t1 = 0 = r | |
move $t2, $zero # Let $t2 = 0 = k | |
outerloop: | |
beq $t2, 32, done # Iterate through the whole register (32 bits) | |
sll $t0, $t0, 1 # Shift the value left 1 | |
sll $t1, $t1, 1 # Shift the result left 1 | |
done: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment