Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to translate the following code to MIPS. This code should be part of this one : # Program to read the header of

image text in transcribed

I need to translate the following code to MIPS. This code should be part of this one :

# Program to read the header of a valid pgm file # Written by Xiuwen Liu for CDA 3100 - Homework #4, problem #2 # Register usage # You need to figure that out .text .globl main main: addu $s7, $ra, $zero, # Save the ra la $a0, file_prompt #print out the prompt for file name li $v0, 4 #system call print_str syscall la $a0, file_name #read in the file name addi $a1, $zero, 256 #using systecall read_str li $v0, 8 #We read the file name syscall #Note that the string includes 10 (' ') at the end #We need to get rid of that first la $t1, file_name #Address of the file name string addi $t2, $zero, 10 length_loop: # of the string: lb $t0, 0($t1) # load the byte at addr B into $t0. beq $t0, $t2, end_length_loop # if $t3 == 10, branch out of loop. addu $t1, $t1, 1 b length_loop # and repeat the loop. end_length_loop: sb $zero, 0($t1) #We change ' ' to 0 la $a0, file_name #Show the file name li $v0, 4 syscall la $a0, newline #Display a new line li $v0, 4 syscall la $a0, row_prompt li $v0, 4 syscall addi $v0, $zero, 5 # We need to read an integer syscall add $s1, $v0, $zero # We save the number of rows to s1 la $a0, column_prompt li $v0, 4 syscall addi $v0, $zero, 5 # We need to read an integer syscall add $s2, $v0, $zero # We save the number of columns to s2 # We allocate enough space and save the addres to image #addi $v0, 9 #li $a0, 1000000 #syscall #la $a0, image #sw $v0, 0($a0) # image *read_image(char *filename, int nrow, int ncol) #prepare parameters for the arguments la $a0, file_name #The first argument add $a1, $s1, $zero #The second argument add $a2, $s2, $zero #The third argument jal read_image #We save the returned values add $s3, $v0, $zero # We save the address of the array beq $v0, $zero, End_main # If the address is zero, something is wrong #Call your function to compute histogram # int histogram(unsigned char *image, int nrow, int ncol, int *h) add $a0, $s3, $zero #The first argument add $a1, $s1, $zero #The second argument add $a2, $s2, $zero #The third argument la $a3, hist_array jal histogram #Call your function to print the histogram # void print_hist(int *h) la $a0, hist_array jal print_hist End_main: addu $ra, $zero, $s7 #restore $ra since the function calles #another function jr $ra add $zero, $zero, $zero add $zero, $zero, $zero ########## End of main function ######### .data #Data segment starts here file_prompt: .asciiz "Please enter a valid file name: " newline: .asciiz ". " file_name: .space 256 row_prompt: .asciiz "Number of rows: " column_prompt: .asciiz "Number of columns: " add_msg: .asciiz "Image address: " .align 2 hist_array: .word 0:256 # We define 256 words for the histogram ###### unsignched char *read_image(char *fname, int nrow, int ncol) ##### $a0 (fname): The name of the pgm file name ##### $a1 (nrow): The number of rows ##### $a2 (ncol): The number of columns in the image ##### $v0: memory address of the image .text .globl read_image read_image: addu $sp, $sp, -28 #We allocate three words sw $a0, 4($sp) sw $a1, 8($sp) sw $a2, 12($sp) sw $ra, 16($sp) add $a1, $zero, $zero #O_RDONLY is 0 add $a2, $zero, $zero #This parameter does not really matter li $v0, 13 #System call open syscall add $t1, $v0, $zero slt $t9, $zero, $t1 #if $t1  

Here image is the starting address of image which has nrow rows and ncol columns. h is the histogram of the image. int histogram (unsigned char Kimage. int ow. int ncol. int Hh) int i, j, k unsigned char Hp: for (i-0: i 256: i++) hlil 0: p image k 0: for (i 0: i K nrow: i++) for (j j ncol j++) k return k; Here image is the starting address of image which has nrow rows and ncol columns. h is the histogram of the image. int histogram (unsigned char Kimage. int ow. int ncol. int Hh) int i, j, k unsigned char Hp: for (i-0: i 256: i++) hlil 0: p image k 0: for (i 0: i K nrow: i++) for (j j ncol j++) k return k

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions