```## LSU EE 4720 Computer Architecture Spring 2015
#
## LIVE Classroom example for review of MIPS:  Convert int to hex string.
#
#   This file contains the state of the code after class on Friday 16
#   January 2015.  At that point we had just gotten started.

.data
msg:
.asciiz "The value of %/s4/5d = 0x%/s4/x is %/a1/s\n"
str:
.align 4
.space 12               # Storage for string.
values:                         # Table of test inputs.
.word 1234              # First entry in table of test inputs.
.word 0x1234
.word 0xa9
.word 0x0
.word 0xf
.word 0xf00d
.word -1

###############################################################################
#
##  Test Code
#
#  This code calls the hex to string routine multiple times using
#  data appearing above.
#

.text
.globl __start
__start:
la \$s2, values          # Address of table of test inputs.
addi \$s3, \$0, -1;       # Value that marks end of table.
MLOOP:
lw \$a0, 0(\$s2)          # Load a test input from the table.
la \$a1, str             # Set \$a1 to address of storage for string.
lui \$t0, 0x2020         # Prepare an initial value for string.
ori \$t0, \$t0, 0x2020    # t0 will contain four blanks.
sw \$t0, 0(\$a1)          # Write string with initial value (blanks).
sw \$t0, 4(\$a1)
sw \$t0, 8(\$a1)
sb \$0, 11(\$a1)
jal itos
addi \$a2, \$0, 10        # Set length of string.
lw \$s4, 0(\$s2)          # Load test input again.
la \$a1, str             # Address of string.
la \$a0, msg             # Address of message (printf-like format str)
addi \$v0, \$0, 11        # Code indicating we want to run printf.
syscall                 # Make a system call to do printf.
bne \$s4, \$s3 MLOOP      # Continue if we are not at end of table.
li \$v0, 10              # Exit system call. (Not used if branch taken.)
syscall
nop

###############################################################################
#
# Unsigned Integer to Hexadecimal String
#

itos:
## Register Usage
#
# CALL VALUES:
#  \$a0: Unsigned integer, to convert.
#       Initialized to null-terminated string of blanks.
#  \$a2: Length of storage.
#
# RETURN:
#       There is no return register value ..
#       .. instead write storage at \$a1 with hex representation of \$a0.
#
# NOTE: Registers \$a0-\$a4 and \$t0-\$t7 can be modified.
# NOTE: 'a' = 97, 'z' = 122,  'a' - 'A' = 32
# NOTE: '0' = 48 = 0x30

## Just to refresh our memories, just write 'a' and 'b' into string.
#