So I'm doing an assembly programming assignment where our teacher asks us to input 5 numbers into an array (they must be greater than 0 and less than 65536) and then grab them back out, convert them to hexadecimal, and then print it out. He gives the method for converting to hexadecimal as follows:
(1) Perform a logical AND on the value of 15 and the parameter.
(2) If the result is less than 10, add it to the ASCII code of character '0'. Otherwise, subtract it by 10 and then add it to the ASCII code of character 'A'.
(3) Shift the parameter to the right by four bits.
(4) Repeat the above process for four times.
(5) Take the lowest 8 bits of the four results obtained above, merge them into a single register and return.
I think I've gotten through steps 1-4 properly, but I don't entirely understand step 5, could someone help me figure out what to do? What does he mean by "lowest 8 bits of the four results"? I've included my code below (it does everything up to step 4, but I haven't coded the section for step 5, as I don't understand it.
# MIPS Programming Assignment 3
.data
msg: .asciiz "\nPlease enter an integer: " #input prompt
error: .asciiz "\nYour number is out of range, try again!"
array: .space 20 #reserve space for 5-integer array
.text
main:
la $t3, array #load pointer for traversing array
la $s0, array #load initial position of array
#initialize counter for loop
add $t0, $t0, $zero #set counter to 0
li $t1, 5 #set maximum number of passes to 5
#initialize counter for hexadecimal loop
add $t4, $t4, $zero #set counter to 0
la $t5, array #load pointer for traversing array
#loop to insert numbers into array
insert_loop:
beq $t0, $t1, hexadecimal_loop #if $t0 = $t1 then exit loop
li $v0, 4 #system call code for printing string
la $a0, msg
syscall
li $v0, 5 #system call for reading integer
syscall
move $t2, $v0 #move read integer to $t2
bltz $t2, error_msg #bltz = branch if ($t2) < zero
bgt $t2, 32768, error_msg #branch if t2 > 32768
sw $t2, ($t3) #stores t2 in t3 array
addi $t3, $t3, 4 #increment array by 4 (move to next index)
addi $t0, $t0, 1 #increment counter by 1
j insert_loop
#loop for hexadecimal subprogram
hexadecimal_loop:
add $s2, $s2, $zero #set counter for loop
li $s3, 5 #set max number of passes to 5
beq $t4, $t1, exit #when $t4 = $t1, then exit loop
lw $s1, ($t5) #store array value into $s1
add $a0, $zero, $s1 #"move" value to $a0 to use as parameter
jal hexadecimal
addi $t5, $t5, 4 #increment array by 4 (move to next index)
#hexadecimal subprogram
hexadecimal:
add $t5, $a0, $zero #copy parameter to $t5
beq $s2, $s3, return_result #when $s2 = $s3 then exit loop
andi $t6, $t5, 15 #logical AND on $t5 and 15
addi $t7, $t7, 10 #variable to check again
bgt $t6, $t7, more_than_ten #if $t6 > 10
blt $t6, $t7, less_than_ten #if $t6 < 10
addi $s2, $s2, 1 #increment counter by 1
#error message display
error_msg:
li $v0, 4
la $a0, error
syscall
j insert_loop
less_than_ten:
addi $t7, $t6, 10
srl $t5, $t5, 4
j hexadecimal
more_than_ten:
addi $t7, $t6, -10
addi $t7, $t7, 65
srl $t5, $t5, 4
j hexadecimal
exit:
li $v0, 10 # System call code for exiting program
syscall