MIPS Runtime Error "Cannot read diectly from text segment" - mips

The block of code starting from jal next is given in the question, so it can't be changed (have to give intermediate memory and register states after each instruction). The program terminates at the error line with the mesage Runtime exception at 0x00400054: Cannot read directly from text segment!0x00400074' I think the line is supposed to load the value of memory location x200040054 to $t0. I tried manually entering the memory and register values and removing the first block of code to get a different program count when the line with the error executes, but get the same error.
.text
li $t2, 0x0012b628
li $t3, 0x01234567
li $t4, 0xfedcba98
li $t5, 0xf0f0f0f0
sw $t2, 0x10000000
sw $t3, 0x10000004
sw $t5, 0x1000000C
li $a0, 0x000007de
li $t0, 0x52f123d6
li $t1, 0xffffffff
li $s0, 0x10000000
jal next
next : lw $t0 , 0x20($31) #line with error
andi $t1 , $t0 , 0x07C0
xor $t0 , $t0 , $t1
lw $t1 , 0($s0)
andi $t1 , $t1 , 0x07C0
or $t0 , $t0 , $t1
srl $t1 , $t1 , 6
sw $t0 , 0x20($ra)
sll $a0 , $a0 , 31
li $v0, 10
syscall

You are getting a runtime exception because the code you are running is trying to read and the write the text segment (the memory which holds the program code) and usually you cannot read/write those portions of memory at runtime.
The line of code that is giving you the exception is trying to put into register $t0 the contents of the instruction located 8 instructions ahead of $ra which at that moment should point to the location of instruction lw $t0 , 0x20($31) #line with error, that is the binary representation of instruction sll $a0 , $a0 , 31
Then, after some bit manipulation it tries to change the instruction sll $a0 , $a0 , 31 with some other instruction, apparently with sll $a0 , $a0 , 24

Related

Little Endian Mips operation

I am struggling to understand what this block of mips instruction does. I want to find out what the register $t0 has after these instructions.
ori $t0 $zero 0xA5C11000
addi $t1 $zero 0x10010000
sw $t0 ($t1)
lb $t0 1($t1)
sh $t0 2($t1)
lw $t0 ($t1)
I know that the registers $t0 and $t1 have A5C11000 and 10010000 in them. Then the sw command stores $t1 at the location of $t0. Lb then offsets $t1 by 1 and stores that at the location 10010001? I don't know what happens after this.
Not quite: sw stores the contents of $t0 at the address in $t1, i.e. 10010000. lb loads a byte from address $t1 + 1 into $t0. sh stores a halfword (the lower 2 bytes of $t0) at address $t1 +2.
Try stepping through the code in the debugger to see what it does.

Binary value of a line of code in mips

Hello everyone I am having trouble figuring this out, the question wants me to give the value of the line of code in a binary value of the offset field not too sure what to do please help.
add $t0, $zero, $zero
addi $a0, $zero, 21
loop:
beq $a0, $zero, end
add $t0, $t0, $a0
addi $a0, $a0, -3
j loop
end
For beq $a0, $zero, end give the binary value of the offset field. Briefly explain.

MIPS Programming in Past Paper

I'm very confused about a question in a university past paper. It is as follows:
What is the value in register $s1 after executing the following piece of MIPS
assembly code?
li $t0, 0x1
li $s0, 0x0
li $s1, 0xa5a5a5a5
loop: and $t1, $t0, $s1
beq $t1, $zero, skip
addi $s0, $s0, 1
skip: sll $t0, $t0, 1 # Shift left logical
bne $t0, $zero, loop
(a) 0x10
(b) 0xa5a5a5a5
(c) 0x0
(d) 0x5a5a5a5a
(e) 0x1
The given answer is A - now, as far as I'm aware, the value of $s1 is not changed after its initial declaration - so how is this the case? I'd have thought it would be B?
This shows the QTSpim:
This shows the PCSpim:

MIPS multiplying an int by a float, getting wrong answer

when I run my program and input 20 degrees, the output is 8.8E-44 in MARS and 0.000000 in QtSpim. it should be 62.8
am I using the wrong instructions here?
.data
pi: .float 3.14
c: .word 180
initMSG: .asciiz "\nEnter an angle in degrees: "
.text
.globl main
main:
mul $t5, $t2, $t6
add $t5, $t5, $sp
addi $t5, $t5, 4
lw $t9, 0($t5)
mul $t4, $t1, $t6
add $t4, $t4, $a0
sw $t9, 0($t4)
addi $t2, $t2, 1
addi $t1, $t1, 1
j forLoop3
Exit:
li $v0, 10 #load the system exit command
syscall #stop the program
The problem is that pi is a float, but the value you grab from the user is a plain integer. You'd need to convert the user's value into a float before you multiply it with pi.
Fortunately you don't have to convert it manually because there's a syscall for reading floats instead of integers. Change the reading syscall from 5 to 6 (6 means read float), and delete the subsequent two lines (syscall 6 will read the float into $f0 directly). After doing that and running it again using 20 degrees, I get 62.800003 as the output (there will be a small amount of error because 3.14 can't be represented exactly using binary floats).

Using lw on address of $ra

Assuming we have something of this sort in MIPS
jal F
F: lw $t1, 0($ra)
lw $t2, 8($ra)
What would we have in $t1 and $t2?
Will $t1 have the instruction code of F??
Say X is the address of label F
when jal is called $ra will hold X
you load what is on $ra to $t1 which is the binary machine laguage of the instruction at X
then you load what is 2 words lower (8) which is here not specified in your code, you need to show a 4th line of code to tell you =)