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

Objective: This homework gives practice in MIPS file I/O and insight into parity

ID: 3875366 • Letter: O

Question

Objective: This homework gives practice in MIPS file I/O and insight into parity. Parity is a means of checking if stored data has been corrupted. Keep in mind that parity checking would really be implemented in circuitry for speed, this is just a simulation and practice in reading files. Create a MIPS program that fulfills the following specifications: • create a plain text file with the sentence: The quick brown fox jumped over the lazy river. This file must be in the same folder from which you run MARS. • save the name of your file in the data section • use SYSCALLs to open the file, then read it into a buffer in data, then close the file; allocate space for another buffer to contain the text+parity • since the ASCII characters only use 7 bits (6-0), bit 7 is unused; we will use this bit to store parity • even parity bit: set bit 7 to 0 if the ascii code contains an even number of bits; otherwise set to 1 • loop through the buffer: o read a byte o set bit 7 for even parity o write the byte to the second buffer • put the code that determines 1 or 0 for the parity bit in a function • then write another function to check the parity • run the program once to check that you get the “ok” message if the data has not been corrupted • run the program again, set a breakpoint before you jump to the check function and manually corrupt the data as seen in the screen shot below; the first a “1010” was changed to 2 “0010” which changed the parity; now the program will print the “not ok” message

Explanation / Answer

.data

fout: .asciiz "testtext1.txt" # filename for output

parityok_msg: .asciiz "OK"

paritynotok_msg: .asciiz "NOT OK"

#buffer: .asciiz "The quick brown fox jumps over the lazy dog."

buffer: .space 50 # reserve space to copy file contents for upto 50 characters

paritybuffer: .space 50 # reserve space to copy file contents with parity bit

.text

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

# Open (for writing) a file that does not exist

li $v0, 13 # system call for open file

la $a0, fout # output file name

li $a1, 0 # Open for writing (flags are 0: read, 1: write)

li $a2, 0 # mode is ignored

syscall # open a file (file descriptor returned in $v0)

move $s6, $v0 # save the file descriptor

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

# Read from file just opened

li $v0, 14 # system call for write to file

move $a0, $s6 # file descriptor

la $a1, buffer # address of buffer where to write

li $a2, 50 # hardcoded buffer length

syscall # write to file

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

# Close the file

li $v0, 16 # system call for close file

move $a0, $s6 # file descriptor to close

syscall # close file

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

# parity generator

# xor all bits to check parity and generte parity bit

  

la $a1, buffer # load address of buffered data

la $a2, paritybuffer # load address of target data

li $t0,0

  

setparity_loop:

bgt $t0,50,exit_paritygenerator

lb $t1 , ($a1)

# bit wise XOR of character read from buffer to find parity

# parity bit is set if number of 1's is odd to make the parity even

srl $t4, $t1, 16

xor $t2, $t1, $t4

srl $t4, $t2, 8

xor $t2, $t2, $t4

srl $t4, $t2, 4

xor $t2, $t2, $t4

srl $t4, $t2, 2

xor $t2, $t2, $t4

srl $t4, $t2, 1

xor $t2, $t2, $t4

and $t2, $t2, 1

sll $t2,$t2,7 # shift the parity bit to 8th bit location

or $t1,$t1,$t2 # set parity bit in the character

sb $t1, ($a2) # store the byte in buffer

add $a1,$a1,1 # increment pointers to next character

add $a2,$a2,1

addi $t0,$t0,1 # loop counter

j setparity_loop

exit_paritygenerator:  

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

# parity checker

# simple xor of all bits should result in zero

la $a1, paritybuffer # load address of buffered data

li $t0,0

  

checkparity_loop:

bgt $t0,50,exit_paritychecker

lb $t1 , ($a1)

# bit wise XOR of character read from buffer to find parity

# parity bit is set if number of 1's is odd to make the parity even

srl $t4, $t1, 16

xor $t2, $t1, $t4

srl $t4, $t2, 8

xor $t2, $t2, $t4

srl $t4, $t2, 4

xor $t2, $t2, $t4

srl $t4, $t2, 2

xor $t2, $t2, $t4

srl $t4, $t2, 1

xor $t2, $t2, $t4

and $t2, $t2, 1

beq $t2,0,parityok # shift the parity bit to 8th bit location

li $v0, 4 #system call code for Print String

la $a0, paritynotok_msg #load address of message into $a0

syscall #print the string

b exit_paritycheckererror

parityok:

add $a1,$a1,1 # increment pointers to next character

addi $t0,$t0,1 # loop counter

j checkparity_loop

exit_paritychecker:  

li $v0, 4 #system call code for Print String

la $a0, parityok_msg #load address of message into $a0

syscall #print the string

exit_paritycheckererror:

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

# Exit Gracefully

li $v0, 10

syscall