Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Program that converts a user's integer input between 0-65535 and converts it to Hex.
- #Jacob Barrett
- .data # Declares the Variables in the program
- # Array of the numbers in Hexidecimal form
- n0: .asciiz "0"
- n1: .asciiz "1"
- n2: .asciiz "2"
- n3: .asciiz "3"
- n4: .asciiz "4"
- n5: .asciiz "5"
- n6: .asciiz "6"
- n7: .asciiz "7"
- n8: .asciiz "8"
- n9: .asciiz "9"
- n10: .asciiz "A"
- n11: .asciiz "B"
- n12: .asciiz "C"
- n13: .asciiz "D"
- n14: .asciiz "E"
- n15: .asciiz "F"
- array1: .word n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15
- size: .space 64 # Size of the Array 4*16 = 64
- # Strings
- notInBetween: .asciiz "That wasn't a number between 0 and 65535."
- String: .asciiz "Enter a number between 0 and 65535: "
- Result: .asciiz "Your number in hex is: "
- .text # Program's code after this line
- .globl main
- main:
- addi $t0, $zero, 0 # Counter that keeps track of how many times divideLoop is used
- li $v0, 4 # Asks the user to enter a number
- la $a0, String
- syscall
- li $v0, 5 # Reads the input by the user
- syscall
- addi $s0, $v0, 0 # Saves the input to $s0
- # Checks if the user's input is within 0-65535 for X
- blt $s0, 0, lessOrMoreThan # If $s0 is less than 0, then go to lessOrMoreThan
- bgt $s0, 65536, lessOrMoreThan # If $s0, is greater than 65536, then go to lessOrMoreThan
- j divideLoop # Jump to divideLoop
- divideLoop:
- blt $s0, 16, exit # Branches if $s0 is less than 16; it will branch to exit when it is done
- sub $s0, $s0, 16 # Subtracts X from 16, then stores the new value where X was. This is done
- # so that we can repetively subtract 16 from X in the loop; otherwise
- # it would keep subtracting 16 from X without updating X, creating an infinite loop
- add $t0, $t0, 1 # Counter for the number of times divideLoop is used (the quotient)
- j divideLoop
- divideLoop2:
- blt $t0, 16, exit # If $t0 is less than 16, then we no longer need to divide it. Go to exit
- sub $t0, $t0, 16 # Subtract 16 from $t0; this will be repeated until it is less than 16
- add $t1, $t1, 1
- j divideLoop2 # Counter for the number of times divideLoop2 is used (the quotient)
- divideLoop3:
- blt $t1, 16, exit # If $t1 is less than 16, then we no longer need to divide it. Go to exit
- sub $t1, $t1, 16 # Subtract 16 from $t1; this will be repeated until it is less than 16
- add $t2, $t2, 1 # Counter for the number of times divideLoop3 is used (the quotient)
- j divideLoop3
- exit:
- # These are used to determine whether or not we need to divide more
- # If $t0 or $t1 is greater than 15, then it will throw an OutOfBound error
- bgt $t1, 15, divideLoop3 # Checks if $t1 is greater than 15; if it is, then go to divideLoop3
- bgt $t0, 15, divideLoop2 # Checks if $t0 is greater than 15; if it is, then go to divideLoop2
- move $s1, $t0 # Moves $t0 to $s1
- move $s2, $t1 # Moves $t1 to $s2
- move $s3, $t2 # Moves $t2 to $s3
- sll $s0, $s0, 2 # Sets $s0 * 4 ($s0 * 2^2)
- sll $s1, $s1, 2 # Sets $s1 * 4
- sll $s2, $s2, 2 # Sets $s2 * 4
- sll $s3, $s3, 2 # Sets $s3 * 4
- ### Print Result
- li $v0, 4 # Prints the result string
- la $a0, Result
- syscall
- li $v0, 4 # Prints out the first character for the Hex conversion (YXXX)
- lw $a0, array1($s3)
- syscall
- li $v0, 4 # Prints out the second character for the Hex conversion (XYXX)
- lw $a0, array1($s2)
- syscall
- li $v0, 4 # Prints out the third character for the Hex conversion (XXYX)
- lw $a0, array1($s1)
- syscall
- li $v0, 4 # Prints out the fourth character for the Hex converson (XXXY)
- lw $a0, array1($s0)
- syscall
- li $v0, 10 # Ends the program
- syscall
- lessOrMoreThan:
- li $v0, 4 # Prints out the notInBetween string
- la $a0, notInBetween
- syscall
- li $v0, 10 # Ends the program
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement