.data
buffer_for_input_string: .space 100
buffer_for_processed_string: .space 100
prompt_for_input: .asciiz “Please enter your string:\n”
prompt_for_output: .asciiz “Your processed string is as follows:\n”
.text
main:
# prompting the user with a message for a string input:
li $v0, 4
la $a0, prompt_for_input
syscall
# reading the input string and putting it in the memory:
# the starting address of the string is accessible as buffer_for_input_string
# 100 is the hard-coded maximum length of the null-terminated string that is
# going to be read from the input. So effectively, up to 99 ascii characters.
li $v0, 8
la $a0, buffer_for_input_string
li $a1, 100
syscall
# >>>> MAKE YOUR CHANGES BELOW HERE:
# Looping over characters of the string:
la $t0, buffer_for_input_string
la $t1, buffer_for_processed_string
Loop:
lb $t2, 0($t0)
# potentially do some processing on the character loaded in t2
sb $t2, 0($t1)
addi $t0, $t0, 1
addi $t1, $t1, 1
bne $t2, $zero, Loop # keep going until you reach the end of the string,
# which is demarcated by the null character.
# <<<< MAKE YOUR CHANGES ABOVE HERE # prompting the user with a message for the processed output: li $v0, 4 la $a0, prompt_for_output syscall # printing the processed output # note that v0 already holds 4, the syscall code for printing a string. la $a0, buffer_for_processed_string syscall # Finish the programme: li $v0, 10 # syscall code for exit syscall # exit 1, Remove any punctuation characters. So for example, for the input text of "Hello, World!" we should get "Hello World" 2, Delete any space before any punctuation character. For instance, for the input text of "Hello , 2022 World !", we should get "Hello, 2022 World!". .data prompt_for_input: .asciiz "Please enter your numbers, pressing enter after each, (0 to terminate):\n" prompt_for_output: .asciiz "Your quantity of interest is equal to: " .text main: # prompting the user with a message for a string input: li $v0, 4 la $a0, prompt_for_input syscall li $s0, 0 LOOP: li $v0, 5 syscall addu $s0, $s0, $v0 bne $v0, $zero, LOOP # prompting the user with a message for the processed output: li $v0, 4 la $a0, prompt_for_output syscall # printing the output addiu $v0, $zero, 1 addu $a0, $zero, $s0 syscall # Finish the programme: li $v0, 10 # syscall code for exit syscall # exit 1, The total sum of the absolute values. For instance, if the numbers entered are [1, -7, -1, -3, 6, -2, 0], then the printed result must be 20. 2, The number of entries (their count) that are negative AND divisible by 4, excluding the zero at the end. For example if the input numbers are [-1, -8, -32, 16, -5, -4, 12, 0], the printed result should be 3. .data input_file_name: .asciiz "in.ppm" output_file_name: .asciiz "out.ppm" .text main: # Open the input file for reading the image from: li $v0, 13 # system call for open file la $a0, input_file_name # input file name li $a1, 0 # flag for reading li $a2, 0 # mode is ignored syscall # open a file addu $s0, $zero, $v0 # save the file descriptor addi $t0, $zero, 1 sll $t0, $t0, 7 sll $t0, $t0, 7 addu $t1, $t0, $t0 addu $t0, $t1, $t0 addi $s1, $t0, 61 # In short: $s1 holds 128*128*3+61 sub $s2, $sp, $s1 # $sp (stack pointer register) holds the address of the top of the stack. # so $s2 will point to an address in the memory from which, we can safely # load our image to. # In short: # s1: holds the size of the image in bytes (which for our example, is 49,213) # s2: holds address of the starting byte of the buffer for the image # reading from file just opened li $v0, 14 # system call for reading from file addu $a0, $zero, $s0 # file descriptor addu $a1, $zero, $s2 # address of buffer to read to addu $a2, $zero, $s1 # how many bytes to read syscall # read from file # Close the input file: li $v0, 16 # system call code for close file addu $a0, $zero, $s0 # file descriptor to close syscall # close file # # At this point, we still have: # s1: holds the size of the image in bytes # s2: holds the address of the starting byte of the buffer for the image. # note that now, the image data is loaded in the memory, byte by byte, # starting from this address onwards. # >>>> MAKE YOUR CHANGES BELOW THIS LINE
addiu $t0, $s2, 61 # t0: will hold the address of each byte of the image
# we have added 61, since this is the size of the header
# we will not touch the bytes of the header.
# this is fine, as none of the tasks require changing the size
# or format of the image.
li $s3, 3 # colours
li $s4, 128 # columns
li $s5, 128 # rows
li $t4, 0 # row counter
Loop_over_rows:
li $t3, 0 # column counter
Loop_over_pixels_in_each_row: # i.e., columns
li $t2, 0 # colour counter (each colour represents Red-Green-Blue)
Loop_over_colours_in_each_pixel:
lbu $t1, 0($t0) # loading the value of the byte, representing
# the intensity of the corresponding RGB colour of that pixel
# note that an earlier version was using lb, which does sign extension
# but the 8-bits of the colour should be interpreted as unsigned
# representing values from 0 to 255.
# do something with the value of the byte
sb $t1, 0($t0) #
addi $t0, $t0, 1 # address of the next byte
addi $t2, $t2, 1 # counter of the colour bytes for each pixel
bne $t2, $s3, Loop_over_colours_in_each_pixel
addi $t3, $t3, 1 # counter over the pixels in a row (column)
bne $t3, $s4, Loop_over_pixels_in_each_row
addi $t4, $t4, 1
bne $t4, $s5, Loop_over_rows
# >>>> MAKE YOUR CHANGES ABOVE THIS LINE
# at this point, still we must have:
# s1: holds the size of the image in bytes
# s2: holds the address of the starting byte of the buffer for the image
# so do not touch the values in s1 and s2 in the part that you edit!
# Open a file for saving the processed image to:
li $v0, 13 # system call for open file
la $a0, output_file_name # input file name
li $a1, 0x41 # flag for (creating if not exists and) writing
li $a2, 0x1B4 # access mode of the file is set to “-rw-r–r–”
syscall # (create if not exists, and) open the file in writing mode
addu $s0, $zero, $v0 # save the file descriptor to $s0
# saving the processed image to it:
li $v0, 15 # syscall for writing to a file
addu $a0, $zero, $s0 # passing the file descriptor, in $a0
addu $a1, $zero, $s2 # passing the memory address to get the data to save to the file, in $a1
addu $a2, $zero, $s1 # passing how many bytes to save to the file, in $a2
syscall
# Close the output file:
li $v0, 16 # system call code for close file
syscall # close file
# Finish the programme:
li $v0, 10 # syscall code for exit
syscall # exit
1,Mirror the image (flip horizontally). >Hint: note that you already know the dimension of the image in terms of the number of pixels: it is a 128 by 128 image.