Write the program in MIPS that reads a string representing an attendance record
ID: 3589959 • Letter: W
Question
Write the program in MIPS that reads a string representing an attendance record for a student The record only contains the following three characters: 'A': Absent. 2. 'L': Late. . 'P Present. A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two consecutive 'L' (late) You need to return (print) whether the student could be rewarded according to his attendance record Example: Input: "PPALLP" Output: True Example 2: Input: "PPALLL Output: FalseExplanation / Answer
Declaring in the .data segment the string
.data
str: .asciiz "PPALLP"
t: .asciiz "True"
f: .asciiz "False"
Then in the .text segment declare the label main like in every other programs.
In the main label we are going to initialize 2 registers. One of them will contain the current address of the string, the other one is just our counter.
.text
main:
la $t0, str # la means load address (so we load the address of str into $t0)
li $c, 0 # $c is the counter. set it to 0 (for A)
li $cl, 0 # $cl is the counter. set it to 0 (for L)
li $checkc, 2 # $checkc is to verify that counter is 2 or there are more than one 'A'
li $checkcl, 3 # $checkcl is to verify that counter is 3 or there are 3 consecutive 'L'
Now logic for checking attendence record for a Student.
attendence:
lb $t2, 0($t0) # Load the first byte from address in $t0
li $t1, 'A' # We need the constant 'A'
li $t3, 'L' # We need the constant 'L'
beq $t2, $t1, countA # Checking if bit equals 'A'
beq $t2, $t2, countL # Checking if bit equals 'L'
beq $c, $checkc, printf # if there are 2 A's, output is False
beq $cl, $checkcl, printf # if there are 2 A's, output is False
li $cl, 0
beqz $t2, printt # if $t2 == 0 then go to label end
add $t0, $t0, 1 # else increment the address
j attendence # finally loop
end:
countA:
add $c, $c, 1 # and increment the counter of course
j attendence:
countL:
add $cl, $cl, 1 # and increment the counter of course
j attendence:
printf:
li $v0, 4
la $a0, f # Printing False when there are more than two A's or 3 consecutive L's
syscall
j end:
printt:
li $v0, 4
la $a0, t # Printing True
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.