0
votes

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
1

1 Answers

0
votes

The number you are converting is less than 65536, so it will fit in 16 bits. That is 4 hex digits. Each hex digit can be represented as an ASCII character which fits in a single byte (8 bits), so you can get the ASCII representation of all four digits in a single 32-bit register.

Steps 2 and 3 of your algorithm give you a single ASCII character in the low byte of whichever register you have used for the calculation. This low byte is the "lowest 8 bits" your teacher is talking about.

You need to store this low byte in a register somewhere (the register that the caller will look in when you return). But because you repeat steps 2 and 3 you need to make sure you store it in a way which won't result in it being overwritten by further iterations.

A possibility is:

  1. Set your storage register equal to zero before you start.
  2. Each time you complete step 2 of your algorithm
    • Shift the storage register left by 8 bits
    • Add your ASCII value (one byte) to the register

In concrete terms, something like this:

  1. Storage register Rx: 0x00000000
  2. First time through algorithm step 2
    • Shift Rx left gives 0x00000000
    • Add ASCII value (say 'A' = hex 29) to Rx gives 0x00000029
  3. Second time through algorithm step 2
    • Shift Rx left gives 0x00002900
    • Add ASCII value (say '1' = hex 1F) to Rx gives 0x0000291F
  4. Third time through algorithm step 2
    • Shift Rx left gives 0x00291F00
    • Add ASCII value (say '7' = hex 25) to Rx gives 0x00291F25
  5. Fourth time through algorithm step 2
    • Shift Rx left gives 0x291F2500
    • Add ASCII value (say '2' = hex 20) to Rx gives 0x291F2520

When you are done, the register (Rx) should contain your 4 ASCII characters 'A','1','7','2' (possibly in reverse order, depending on what you want to do with them). The calling function can look in Rx to, for example, print them out.