.data prompt: .asciiz "Enter your account login: " inbuf: .space 256 out1: .asciiz "Your assignment 1 output is " out2: .asciiz "x" .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. ble $s2, 9, a1 add $s2, $s2, 0x7 # >= 0xa adds 0x37 (fall through), # gets us to 0x41-0x46, ascii codes for A-F. a1: add $s2, $s2, 0x30 # < 9 adds 0x30, gets us to 0x30-0x39, 0-9. sb $s2, out2 # output the character. la $a0, out2 li $v0, 4 syscall and $s2, $s1, 0xf # Same thing for lower nibble. ble $s2, 9, a2 add $s2, $s2, 0x7 a2: add $s2, $s2, 0x30 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