COS Assignment 5
COS Assignment 5
Problem 1:
(20 points) Let s be an integer not divisible by 9; s is to be read as
input. You are asked to
compute z = oor(s/9), i.e., the largest integer smaller than or equal
to x.
This is a precision chosen such that will help obtain the oor of the
result easily. The result will be stored as a high and low in two
separate registers, and the high will automatically give us the
result, preventing us from having to right shift by 32 to correct for
the previous shift.
*/
fi
fl
fl
fl
fl
fl
fl
ALGORITHM:
Exit program
.text
main:
li $v0, 4 # syscall instruction to print string
la $a0, prompt # store prompt String in a0 for printing
syscall # print prompt to console
fl
# getting user input -- assume not divisible by 9
li $v0, 5 # syscall instruction to read int input
syscall
# now properly store the input in s0
move $s0, $v0
j divider
divider:
# n * 1/9 instead of n/9
# 1/9 in binary = 0.000111 (repeating)
# multiply by 2^32 for precision (make it an int)
# ^ equivalent to a left shift of 32
# = 477218588 (magic num), store this
li $s1, 477218589 # +1 to account for loss of precision
# print newline
li $v0, 4
la $a0, newline
syscall
# exit program
li $v0, 10 # syscall to exit
syscall
DOCUMENTATION
1. https://chortle.ccsu.edu/assemblytutorial/Chapter-14/
ass14_05.html
2. https://stackover ow.com/questions/19063210/what-is-lower-
and-higher-bits
3. Division in ARM https://stackover ow.com/questions/
19844575/how-to-do-division-in-arm
4. Wikipedia division https://en.wikipedia.org/wiki/
Division_algorithm
5. Wikipedia multiplication https://en.wikipedia.org/wiki/
Multiplication_algorithm
6. computers suck at division (a painful discovery) https://
www.youtube.com/watch?v=ssDBqQ5f5_0&list=LL
for i = 1 to length(ARRAY) - 1:
key = ARRAY[i] // place key value correctly
j = i-1 // prev index
MIPS CODE
.data
array: .word 8, 3, 5, 9, 1, 6 # sample array of elements (change to
test!)
n: .word 6 # no. of elements in the array
newline: .asciiz "\n" # newline for printing and formatting
.text
.globl main
main:
ff
ff
# load array base address
la $t0, array # t0 = base address of array
lw $t1, n # t1 = size of array (n)
outerloop:
bge $t2, $t1, print_array # if i >= n, print (end of array)
innerloop:
blt $t6, 0, insert_key # if j < 0, stop shifting
mul $t7, $t6, 4 # o set= j * 4
add $t8, $t0, $t7 # array[j] address
lw $t9, 0($t8)
insert_key:
mul $t7, $t6, 4 # o set = j * 4
add $t8, $t0, $t7 # array[j+1]'s address
sw $t5, 4($t8) # array[j+1] = key
printloop:
ff
ff
ff
bge $t2, $t1, exit # if i >= n, exit
mul $t3, $t2, 4 # o set = i * 4
add $t4, $t0, $t3
lw $a0, 0($t4)
exit:
li $v0, 10 # exit program -- syscall instruction
syscall
DOCUMENTATION
1. Insertion sort algorithm: https://www.geeksforgeeks.org/
insertion-sort-algorithm/
2. Rest is from MIPS documents on classroom.
OUTPUT CHECKING
ff
Input ^ = 1,9,4,6,2,7,4,8,2,39,1,0
Input:
2,323487,4,655387124,7,7267444,34823615274,813465134,2,312
349,1782643,11
Code in HLL:
Code in MIPS:
.data
prompt_value: .asciiz "Enter a number: "
.text
.globl main
main:
li $t0, 16
li $t1, 0 # Counter i = 0
la $t2, array
input_loop:
beq $t1, $t0, get_target # Once numbers entered, get target sum
li $v0, 4
la $a0, prompt_value
syscall
li $v0, 5
syscall
sw $v0, 0($t2)
get_target:
li $v0, 4
la $a0, prompt_target
syscall
li $v0, 5
syscall
move $t3, $v0
# Find pairs:
li $t4, 0
outer_loop:
inner_loop:
la $t2, array
li $v0, 4
la $a0, pair_message
syscall
# Print A[i]
li $v0, 1
li $v0, 4
la $a0, comma_space
syscall
# Print A[ j]
li $v0, 1
syscall
# Print ")\n"
li $v0, 4
la $a0, close_bracket
syscall
skip_pair:
j inner_loop
increment_outer:
j outer_loop
check_no_pairs:
beq $t6, 1, exit_program # If pairs found, exit normally
li $v0, 4
la $a0, no_pairs_msg
syscall
exit_program:
li $v0, 10
syscall
Outputs:
Problem-4:
i. Code in HLL:
Code in MIPS:
.data
.text
.globl main
main:
# Prompt to get first binary string
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8
la $a0, buffer
li $a1, 34
syscall
la $a0, buffer
jal parse_binary
li $v0, 4
la $a0, prompt2
syscall
li $v0, 8
la $a0, buffer
li $a1, 34
syscall
la $a0, buffer
jal parse_binary
move $s1, $v0
li $v0, 4
la $a0, result1
syscall
jal float_to_decimal
li $v0, 4
la $a0, result2
syscall
jal float_to_decimal
li $v0, 10
syscall
parse_binary:
li $v0, 0
parse_loop:
j parse_loop
parse_done:
jr $ra
float_to_decimal:
li $t2, 0x007FFFFF
# find 2^exponent
pos_exp_loop:
j pos_exp_loop
negative_exponent:
neg_exp_loop:
beq $t6, $t1, exp_done
j neg_exp_loop
exp_done:
li $v0, 1
syscall
li $v0, 4
la $a0, dot
syscall
li $v0, 1
syscall
li $v0, 4
la $a0, newline
syscall
jr $ra
Outputs:
ii and iii:
Code in HLL:
Code in MIPS:
.data
prompt1: .asciiz "Enter first float (as 32-bit binary): "
.text
.globl main
main:
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8
la $a0, buffer
li $a1, 33
syscall
la $a0, buffer
jal binary_to_int
li $v0, 4
la $a0, prompt2
syscall
li $v0, 8
la $a0, buffer
li $a1, 33
syscall
la $a0, buffer
jal binary_to_int
jal extract_fields
check_b_implicit:
li $t2, 0x00800000
mantissas_ready:
j exponents_equal
a_larger: # if
j exponents_equal
b_larger: # if
# Shift mantissa A right by $t9 bits (absolute value)
j exponents_equal
zero_a:
li $s2, 0
j exponents_equal
zero_b:
li $s5, 0
exponents_equal:
a_positive:
b_positive:
# Add mantissas
add $t2, $s2, $s5
li $t3, 0
li $t3, 1
li $s6, 0 # Sign = 0
li $s1, 0 # Exponent = 0
li $t2, 0 # Mantissa = 0
j create_result
normalize_start:
find_highest_bit:
and $t6, $t2, $t4
bnez $t6, found_highest_bit
j find_highest_bit
found_highest_bit:
j normalization_done
sub $s1, $s1, $t8 # Adjust exponent: subtract shift amount (FIXED
LINE)
li $t7, 0x007FFFFF
create_result:
# combine to get the final result
# Print result
li $v0, 4
la $a0, result_msg
syscall
overflow_exit:
li $v0, 4
la $a0, overflow
syscall
j exit
underflow_exit:
li $v0, 4
la $a0, underflow
syscall
j exit
exit:
li $v0, 10
syscall
binary_to_int:
binary_loop:
lb $t1, 0($t0)
beqz $t1, binary_done
j binary_loop
binary_done:
jr $ra
extract_fields:
jr $ra
print_binary:
move $t0, $a0
li $t1, 32
print_binary_loop:
li $v0, 11
li $a0, 49
syscall
j print_binary_next
print_zero:
li $v0, 11
li $a0, 48
syscall
print_binary_next:
# Decrement counter
# Print newline
li $v0, 4
la $a0, newline
syscall
jr $ra
Outputs:
For 2 positives:
For 2 negatives: