Starting from:

$30

CS231-Lab 4 Solved

1. Implement a procedure ​reverse in MIPS assembly language that, given a string ​S and its length​, reverses ​S​.  

 

 For example, if ​S =“Hello” and ​length = 5, then after calling your procedure S​ becomes “olleH”, and this reversed ​S should be printed out. (NOTE: ​S = “H ello” and length = 6, S​ becomes “olle H”, assuming each space will be calculated as an each length; also special characters will not be considered).  

 

In the program, we assume the variables (e.g., ​S ​and​ length​) should be declared and initialized manually in the ​.data​ section. (Need to be tested by changing the​ S​ and ​length​ manually.)  

 

The signature of this procedure in a high level language would look like this:  

void reverse(char String[], int length);  

 

Output​: for ​S​ =“​Hello​”

 

With the printed ​olleH 

The string ​S​ MUST have ​olleH ​(,with ASCII representation;  the address might be different)

  

 

 2. [​OP2​] Optionally, your program can use ​a prompt user​ to input a string ​S,​ and print reversed ​S out. And, the program should continue prompting until the user inputs “-”, i.e., until S​ ​=“-”.  

 

NOTE​: The option2 will not be supported by your TA, but optionally you can submit this version after working by yourself, who wants to try a more challenging problem. Please add the following message in ​the first line of your submitted program​: ​#[OP2] was implemented  

 

For the optional problem, you need to refer more SYSCALL system services, in addition to the below examples: ​https://courses.missouristate.edu/KenVollmar/mars/Help/SyscallHelp.html 

 

NOTES​:  How to print Integers and Strings/space/newline using ‘syscall’  

 

.data 

x:
.word
 5
msg1:
.asciiz
"x="
nl:
.asciiz
"\n"
space:
.asciiz
" "
 

.text main:

# Register assignments

# $s0 = x

 

# Initialize registers
 
                lw        $s0, x

 

# Print msg1
# Reg $s0 = x
li $v0, 4 la $a0, msg1

syscall

 

# Print result (x)
# print_string syscall code = 4
                li          $v0,1
# print_int syscall code = 1
                move   $a0, $s0

syscall

 

# Print newline
# Load integer to print in $a0
                li          $v0,4

                la         $a0, nl

syscall

 

# Exit
# print_string syscall code = 4
                li          $v0,10

syscall
# exit
 

 

More products