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

Write a MIPS program(Mars) that reads from the keyboard a string of no more than

ID: 3602811 • Letter: W

Question

Write a MIPS program(Mars) that reads from the keyboard a string of no more than 20 characters and then outputs the string in a single line even if the input is written on multiple lines. The input string will be terminated by an character '0', which doesn't count as part of the string. Hint: use a loop to read the input character-by-character and save all characters in a memory buffer.

Sample runs (green text is the output abc def abcdef ghie abcdefghi Example of terminating input with 0 ghijklm nopq abcdefghijklmnopq Example of reaching maximum number of acharacters (20)

Explanation / Answer

.data
    str: .space 20
    msg: .asciiz " You wrote: "
    prompt: .ascii "Enter string (max 20 chars): "

    .text

    main:
    la $a0,prompt #Load and print string asking for string
    li $v0,4
    syscall

    la $t2,str #address of string
    li $t3,20

input_loop:
    be $t3,$zero,loop_end

    li $v0, 12 #take in character input
    syscall

    move $t0,$v0    #input is recorded
  

    be $t0,$zero,loop_end   #checking for 0 character i.e end of input

    li $t1,10                           #ascii code for new line
    be $t0,$t1,loop_count_increment     #checking for new line,if new line then storing of line is skipped

    sw $t0,0($t2)       #storing the character in the string buffer
    addi $t2,$t2,1      #incrementing to the next location

loop_count_increment:      
    subi $t3,$t3,1      #condition for maximum 20 characters of input
    b input_loop
  
loop_end:
   la $a0, msg #load and print “You wrote" string
   li $v0,4
   syscall

   la $a0, str #load and print “You wrote" string
   li $v0,4
   syscall

   li $v0,10 #end program
   syscall

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote