Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #Program that converts a user's integer input between 0-65535 and converts it to Hex.
  2. #Jacob Barrett
  3.  
  4. .data # Declares the Variables in the program
  5.  
  6. # Array of the numbers in Hexidecimal form
  7. n0: .asciiz "0"
  8. n1: .asciiz "1"
  9. n2: .asciiz "2"
  10. n3: .asciiz "3"
  11. n4: .asciiz "4"
  12. n5: .asciiz "5"
  13. n6: .asciiz "6"
  14. n7: .asciiz "7"
  15. n8: .asciiz "8"
  16. n9: .asciiz "9"
  17. n10: .asciiz "A"
  18. n11: .asciiz "B"
  19. n12: .asciiz "C"
  20. n13: .asciiz "D"
  21. n14: .asciiz "E"
  22. n15: .asciiz "F"
  23. array1: .word n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15
  24. size: .space 64 # Size of the Array 4*16 = 64
  25.  
  26. # Strings
  27. notInBetween: .asciiz "That wasn't a number between 0 and 65535."
  28. String: .asciiz "Enter a number between 0 and 65535: "
  29. Result: .asciiz "Your number in hex is: "
  30.  
  31.  
  32. .text # Program's code after this line
  33.  
  34. .globl main
  35.  
  36. main:
  37.  
  38. addi $t0, $zero, 0 # Counter that keeps track of how many times divideLoop is used
  39.  
  40. li $v0, 4 # Asks the user to enter a number
  41. la $a0, String
  42. syscall
  43.  
  44. li $v0, 5 # Reads the input by the user
  45. syscall
  46. addi $s0, $v0, 0 # Saves the input to $s0
  47.  
  48. # Checks if the user's input is within 0-65535 for X
  49. blt $s0, 0, lessOrMoreThan # If $s0 is less than 0, then go to lessOrMoreThan
  50. bgt $s0, 65536, lessOrMoreThan # If $s0, is greater than 65536, then go to lessOrMoreThan
  51.  
  52. j divideLoop # Jump to divideLoop
  53.  
  54. divideLoop:
  55. blt $s0, 16, exit # Branches if $s0 is less than 16; it will branch to exit when it is done
  56. sub $s0, $s0, 16 # Subtracts X from 16, then stores the new value where X was. This is done
  57. # so that we can repetively subtract 16 from X in the loop; otherwise
  58. # it would keep subtracting 16 from X without updating X, creating an infinite loop
  59.  
  60. add $t0, $t0, 1 # Counter for the number of times divideLoop is used (the quotient)
  61. j divideLoop
  62.  
  63. divideLoop2:
  64. blt $t0, 16, exit # If $t0 is less than 16, then we no longer need to divide it. Go to exit
  65. sub $t0, $t0, 16 # Subtract 16 from $t0; this will be repeated until it is less than 16
  66.  
  67. add $t1, $t1, 1
  68. j divideLoop2 # Counter for the number of times divideLoop2 is used (the quotient)
  69.  
  70. divideLoop3:
  71. blt $t1, 16, exit # If $t1 is less than 16, then we no longer need to divide it. Go to exit
  72. sub $t1, $t1, 16 # Subtract 16 from $t1; this will be repeated until it is less than 16
  73.  
  74. add $t2, $t2, 1 # Counter for the number of times divideLoop3 is used (the quotient)
  75. j divideLoop3
  76.  
  77.  
  78. exit:
  79.  
  80. # These are used to determine whether or not we need to divide more
  81. # If $t0 or $t1 is greater than 15, then it will throw an OutOfBound error
  82. bgt $t1, 15, divideLoop3 # Checks if $t1 is greater than 15; if it is, then go to divideLoop3
  83. bgt $t0, 15, divideLoop2 # Checks if $t0 is greater than 15; if it is, then go to divideLoop2
  84.  
  85.  
  86. move $s1, $t0 # Moves $t0 to $s1
  87. move $s2, $t1 # Moves $t1 to $s2
  88. move $s3, $t2 # Moves $t2 to $s3
  89. sll $s0, $s0, 2 # Sets $s0 * 4 ($s0 * 2^2)
  90. sll $s1, $s1, 2 # Sets $s1 * 4
  91. sll $s2, $s2, 2 # Sets $s2 * 4
  92. sll $s3, $s3, 2 # Sets $s3 * 4
  93.  
  94.  
  95. ### Print Result
  96. li $v0, 4 # Prints the result string
  97. la $a0, Result
  98. syscall
  99. li $v0, 4 # Prints out the first character for the Hex conversion (YXXX)
  100. lw $a0, array1($s3)
  101. syscall
  102. li $v0, 4 # Prints out the second character for the Hex conversion (XYXX)
  103. lw $a0, array1($s2)
  104. syscall
  105. li $v0, 4 # Prints out the third character for the Hex conversion (XXYX)
  106. lw $a0, array1($s1)
  107. syscall
  108. li $v0, 4 # Prints out the fourth character for the Hex converson (XXXY)
  109. lw $a0, array1($s0)
  110. syscall
  111.  
  112. li $v0, 10 # Ends the program
  113. syscall
  114.  
  115. lessOrMoreThan:
  116.  
  117. li $v0, 4 # Prints out the notInBetween string
  118. la $a0, notInBetween
  119. syscall
  120.  
  121.  
  122. li $v0, 10 # Ends the program
  123. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement