.data prompt: .asciiz "Enter your account login: " inbuf: .space 256 out1: .asciiz "Your assignment 1 output is " out2: .asciiz "x" hex: .ascii "0123456789abcdef" .text main: la $a0, prompt # prompt user for li $v0,4 # their login syscall la $a0, inbuf # get input, max length li $a1, 256 # is 256 characters li $v0, 8 syscall la $a0, out1 # output constant prefix li $v0,4 syscall la $s0, inbuf loop: # Hexify input string # lbu $s1, 0($s0) -- this may be used alternatively to save # an and instruction later. lb $s1, 0($s0) # Work on this one byte at a time: add $s0, $s0, 1 # *cp++ beq $s1, 0, done # The input syscall guarantees buffer beq $s1, 0xa, done # is null terminated. If user input # had to be truncated, NL would be missing. srl $s2, $s1, 4 # High nibble first and $s2, $s2, 0xf # This is not nec'y if lbu used above. lb $s2, hex($s2) # Look up char representation of nibble # in hex table. sb $s2, out2 # Output the character. la $a0, out2 li $v0, 4 syscall and $s2, $s1, 0xf # Same thing for lower nibble. lb $s2, hex($s2) sb $s2, out2 la $a0, out2 li $v0, 4 syscall j loop done: li $t0, 0xa # Output a trailing newline. sb $t0, out2 la $a0, out2 li $v0, 4 syscall li $v0, 0 jr $ra