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

Question 3 Name your source code file hw04-03.s A C for or do-while loop can be

ID: 3564879 • Letter: Q

Question

Question 3

Name your source code file hw04-03.s A C for or do-while loop can be converted directly into assembly
language, but it may be easier to first convert the loop into an equivalent while loop,
for (init-expr; cond-expr; update-expr) {
body
}
do {
body
} while (cond-expr)
init-expr
while (cond-expr) {
body
update-expr
}
goto body_label;
while (cond-expr) {
body_label: body
}
For this exercise write a complete MIPS assembly language program which prompts the user to enter a positive integer
n and computes and displays the sum of the squares of the first n positive integers. For example, if n is 6 the program
shall compute and display 12
+ 22
+ 32
+ 42
+ 52
+ 62
. Here is the pseudocode for the program,
Program hw04-03
Function main()
Integer i, n, sum
PrintString("Enter an integer (> 0): ")
n = ReadInt()
For (i = 1, sum = 0; i <= n; ++i)
sum += i * i
End For
PrintString("The sum of the squares of the first ")
PrintInt(n)
PrintString(" positive integers is ")
PrintInt(sum)
Exit()
End Function main
End Program hw04-3

Question 6

In a MIPS32 program, the instruction at memory address 0x0040_8400 is j end_if which jumps to the label end_if
which is at memory address 0x0040_928C. What would be the encoding of this instruction? Express your answer as
an 8-hexdigit integer. For full credit explain how your answer was obtained.


Question 7

In a MIPS32 program, the instruction at memory address 0x0040_8400 is beq $t0, $t1, loop which jumps to the

label loop which is at memory address 0x0040_81E8. What would be the encoding of this instruction? Express your
answer as an 8-hexdigit integer. For full credit explain how your answer was obtained.

Explanation / Answer

The instruction format of j is
000010b - instruction index(address to which the jump should happen right shifted by 2)
the upper 4 bits of the effective address is same as the current PC
current PC is at 0x00408400 and the effective address should be 0x0040928C,so the jump is possible.
so the value to be written to the instruction will be (0x040928C>>2) (remove upper 4 bits)= (0000-0100-0000-1001-0010-1000-1100 >> 2)
= 0000-0100-0000-1001-0010-1000-11
effective instruction is
000010 - 0000-0100-0000-1001-0010-1000-11 = 0000-1000-0001-0000-0010-0100-1010-0011 = 0x081024A3

beq instruction format is
000100 - rs(5bits) - rt(5bits) - offset(the offset of resltant address with respect to current PC)
beq rs,rt,offset
beq $t0,$t1,((0x004081e8 - 0x00408400)>>2)
rs = $t0 = 01000
rt = $t1 = 01001
offset = (0xFFDE8 >>2 ) = 1111-1111-1101-1110-1000 >>2 = 1111-1111-0111-1010
as we need only 16 bits in offset we truncate the remaining upper bits
effective instruction is
000100 - 01000 - 01001 - 1111-1111-0111-1010 = 0001-0010-0000-1001-1111-1111-0111-1010 = 0x1209FF7A

# hw04-03.s

.data
enter: .asciiz "Enter an interger: "
sum: .asciiz "The sum of the squares of the first "
positive: .asciiz " positive intergers is "
.text

#Display a prompt to enter the integer
la $a0,enter
li $v0,4
syscall

#read the integer,read integer is in $v0
li $v0,5
syscall
move $t1,$v0 # n in $t1
li $t0,1 #i = 1
li $a1,0 #sum = 0
begin_loop:
bgt $t0,$t1,end_loop #i<=1 perform else end
mul $a2,$t0,$t0 #i*i
add $a1,$a1,$a2 #sum + i*i
addi $t0,$t0,1
b begin_loop
end_loop:
#print the first string
la $a0,sum
li $v0,4
syscall

move $a0,$t1
li $v0,1
syscall

#print second string
la $a0,positive
li $v0,4
syscall

move $a0,$a1
li $v0,1
syscall

#terminate the program
li $v0,10
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