Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MIPS Assembly language Your task is to write the body of the threshold function

ID: 3742996 • Letter: M

Question

MIPS Assembly language

Your task is to write the body of the threshold function that takes in an array of pixels of a grayscale image (one byte per pixel) along with its dimensions and applies binary thresholding. The input image for this part is a square sized grayscale image. For grayscale images, one byte is used to represent each pixel’s intensity where the possible values range from 0x00 to 0xFF. In other words, the image is stored in a 2D array where each element is a byte.

###############################################################

# Text Section

.text

# Utility function to print byte arrays

#a0: array

#a1: length

print_array:

li $t1, 0

move $t2, $a0

print:

lb $a0, ($t2)

andi $a0, $a0, 0xff

li $v0, 1

syscall

li $v0, 4

la $a0, space

syscall

addi $t2, $t2, 1

addi $t1, $t1, 1

blt $t1, $a1, print

jr $ra

###############################################################

###############################################################

# PART 1 (Image thresholding)

#a0: input buffer address

#a1: output buffer address

#a2: image dimension

#a3: threshold value

###############################################################

threshold:

############################## Part 1: your code begins here ###

(Answer here)

############################## Part 1: your code ends here ###

jr $ra

Explanation / Answer

###############################################################

# Text Section

.text

# Utility function to print byte arrays

#a0: array

#a1: length

print_array:

li $t1, 0

move $t2, $a0

print:

lb $a0, ($t2)

andi $a0, $a0, 0xff

li $v0, 1

syscall

li $v0, 4

la $a0, space

syscall

addi $t2, $t2, 1

addi $t1, $t1, 1

blt $t1, $a1, print

jr $ra

###############################################################

###############################################################

# PART 1 (Image thresholding)

#a0: input buffer address

#a1: output buffer address

#a2: image dimension

#a3: threshold value

###############################################################

threshold:

############################## Part 1: your code begins here ###

add $s0,$0,$a0

add $s1,$0,$a0

add $t0,$0,$a0

add $s2,$0,$a0

li $t1,0

li $t2,0

threshold_loop:

beq $t0,$0,EXIT_LOOP

addi $t0,$t0,-1

lb $t1,0($s0)

bgt $t1,$0,NEXT_COND

li $t1,0

j NEXT_ELE

NEXT_COND:

ble $t1,$s2,NEXT_ELE ; disallow anything greater than threshold

li $t1,0xFF

NEXT_ELE:

sb $t1,0($s1)

#as each element is represented by byte,next element is after one byte location

addi $s0,$s0,1

addi $s1,$s1,1

j threshold_loop

EXIT_LOOP:

############################## Part 1: your code ends here ###

jr $ra

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote