Address out of range MIPS Assembly - mips32

I was wondering if anyone could please help me with debugging an address out of range error (denoted by comment in asm file) - I'm trying to write a simple assembly program targeting MIPS for the following code:
struct point {
int x;
int y;
};
void main() {
struct point p2;
p2.x = 1;
print_i(p2.x);
}
The assembly file produced was:
.data
.text
j main
main:
move $fp $sp #initialize fp to sp
addi $sp, $sp, -8 #allocate space for struct with 2 int fields
lw $t0, -8($fp) #load data at address for first field of p2 into $t0
sw $t0, 0($t9) #Out of Range error occurs here
li $t8, 1
sw $t8, ($t9)
li $v0, 1
lw $t0, -8($fp)
sw $t0, 0($t9)
add $a0, $t9, $zero
syscall
li $v0, 10
syscall
I think I'm loading data from the wrong address, but I've tried running the assembly with -4($fp) as well and I'm not sure what other address I should be using to get the location of field x for p2. I know this may be super obvious, but I'm just starting to learn how assembly works and I've been stuck on this for a while. Any help debugging or explanations of what my program is doing here would be super appreciated :) Thanks!

Related

How to debug a MIPS code? Affectation issues [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am trying to replicate this Java function in MIPS:
static int gmul(int a, int b) {
int p = 0;
for (int i = 0; i < 8; i++) {
if ( (b & 1) == 1) p = p ^ a;
a = a << 1;
if ( a > 255 ) a = a ^ 283;
b = b >> 1;
}
return p ;
}
I have this MIPS code:
.data
demanda:
.asciiz "Enter a first number"
demandb:
.asciiz "Enter a second number"
.text
main:
la $a0, demanda
li $v0, 4
syscall
li $v0, 5
syscall
move $t2, $v0
la $a1, demandb
li $v0, 4
syscall
li $v0, 5
syscall
move $a1, $v0
move $a0, $t2
li $v0, 0 # this is p in the java version
li $t0, 0
jal for
li $v0, 1 # Print p
syscall
li $v0, 10
syscall
for:
bge $t0,8,finfor # if i>=8
addi $t0, $t0, 1 # Incrementation
andi $t2, $a1,1 # Preliminary condition calculation: (b & 1) == 1 in Java
beq $t2,0, bEven # if ( (b & 1) == 1) in java
xor $v0, $a0, $v0 # Execution p = p ^ a in Java
bEven:
sll $a0, $a0, 1 # Execution a = a << 1 in java
blt $a0,256, aSmall # if ( a > 255 )
xori $a0, $a0, 283 # Execution a = a ^ 283 in Java
aSmall:
srl $a1, $a1, 1 # Execution b = b >> 1 in java
j for
finfor:
jr $ra
I think the formulas are OK, as the program runs, but it does not returns p as expected. It seems that it rather return a (which is in $a0), which I don't understand as I have put p in $v0 and try to print this one.
But it is not the first time I am struggling with registers and affectations in MIPS.
Any help is welcome and thanks in advance.
Found it. The trick in MIPS is that the print out command li $v0, 1 can override the value that you may want to print in $v0, that was my mistake. The work around is to store the data in another register first:
move $a2, $v0
...
move $a0 $a2
li $v0, 1
syscall
Or preferably, not using $v0 for the variable you need to handle.
And the step by single-step functionality really helps to find out mistakes.

write russian peasant multiplication with mips

i want to write a program that use russian peasant multiplication with mips but i face some problems
/ A method to multiply two numbers using Russian Peasant method
unsigned int russianPeasant(unsigned int a, unsigned int b)
{
int res = 0; // initialize result
// While second number doesn't become 1
while (b > 0)
{
// If second number becomes odd, add the first number to result
if (b & 1)
res = res + a;
// Double the first number and halve the second number
a = a << 1;
b = b >> 1;
}
return res;
}
I translated it in mips with MARS environment
.data
msg1: .asciiz "give your first number"
msg2: .asciiz "give the second number"
.text
#a = $t1
#b = $t2
li $t3,0 #p
li $s1,1
li $s2,2
#display msg1
li $v0,4
la $a0,msg1
syscall
#read first number
li $v0,5
syscall
move $t1,$v0
#display msg2
li $v0,4
la $a0,msg2
syscall
#read second number
li $v0,5
syscall
move $t2,$v0
WHILE:
bgt $t2,$zero,DO
move $a0,$t3
li $v0,1
syscall
li $v0,10
syscall
DO:
div $t2,$s2
mfhi $s0 #r $s0 = $t2 / $s2
beq $s0,$s1,INC # IF b MOD 2 = 1 I jump to INC
#a = 2xa
mul $t1,$t1,$s2
div $t2,$s2
mflo $t2 # b = $t2/$s2
j WHILE
# i = i+ 1
INC:
add $t3,$t3,$t1
jr $ra
please , someone could help me ?
to fix "Error in : invalid program counter value: 0x00000000"
i searched how to fix it but i have a problem with $ra
how to save propertly the return address in $ra ?
the jr $ra will jump to the address stored in register $ra, which was not set by your code, so it is still zero (initial register value from MARS before running your code). The CPU will then jump to address zero, which is failure, and reports that.
To use jr $ra to "return" from some code, you have to first use jal instruction (or other instruction modifying $ra content) to set up $ra. Also when you want to nest several jal "subroutines calls", you have to store/restore the $ra of the outer calls around to not lose the value by the next nested jal.
Following code is example of both using paired jal+jr $ra for "subroutine"-like call, and also example how straightforward the implementation of such algorithm is in assembly (because actually that C source is more like assembly in C, so your inexperience made you take a very convoluted and complex approach of something what can be implemented in assembly almost 1:1 with the original C source):
.text
main: # just minimal "main" to verify the code works
# read two integers as input
li $v0,5
syscall
move $a0, $v0 # $a0 = a
li $v0,5
syscall
move $a1, $v0 # $a1 = b
# call the russianPeasant subroutine
jal russianPeasant # $v0 = a * b
nop
# output result
move $a0, $v0
li $v0, 1
syscall
# terminate
li $v0, 10
syscall
And the subroutine itself, which can be called with arguments in a0 and a1 and returns result in v0.
# input: $a0 = a, $a1 = b
# output: $v0 = a * b
russianPeasant:
li $v0, 0 # res = 0
beq $a1, $zero, russianPeasant_b_zero
nop # neutralize "Delayed branching" setting ("nop" works with both ON/OFF setting)
russianPeasant_while_b:
andi $at, $a1, 1 # test if b is odd, for even b skip the res = res + a
beq $at, $zero, russianPeasant_b_even
nop # but such neutralization comes with performance cost of course
add $v0, $v0, $a0 # res = res + a
russianPeasant_b_even:
sll $a0, $a0, 1 # a = a << 1
srl $a1, $a1, 1 # b = b >> 1
bne $a1, $zero, russianPeasant_while_b # repeat when (b != 0)
nop # on real MIPS production code the instructions are reordered to avoid useless nop
russianPeasant_b_zero:
jr $ra # return res
nop
The code is intentionally putting after each branch nop instruction to make it work with both delayed branching setting ON or OFF.
Try to use MARS help (F1) with instruction description to figure out, how it works, also use the single-step feature of the debugger to watch out the code in action, one instruction at time, observing all the register values and code flow.
On real MIPS CPU with the delayed branching the code can be optimized like this (you can switch "Delayed branching" ON in MARS settings to make it simulate real MIPS CPU with this somewhat confusing behaviour .. from your original source it looks like you are learning the MIPS assembly without delayed branching, which is certainly reasonable approach for somebody just starting with assembly, but that's not how real MIPS CPU works):
# input: $a0 = a, $a1 = b
# output: $v0 = a * b
russianPeasant_real_MIPS: # second variant supporting delayed branching like real MIPS CPU
beq $a1, $zero, russianPeasant2_b_zero
li $v0, 0 # res = 0
russianPeasant2_while_b:
andi $at, $a1, 1 # test if b is odd, for even b skip the res = res + a
beq $at, $zero, russianPeasant2_b_even
srl $a1, $a1, 1 # b = b >> 1
add $v0, $v0, $a0 # res = res + a
russianPeasant2_b_even:
bne $a1, $zero, russianPeasant2_while_b # repeat when (b != 0)
sll $a0, $a0, 1 # a = a << 1
russianPeasant2_b_zero:
jr $ra # return res
nop

How to design a program that finds the sum of the integers from 1 to N, where N is a value read on the keyboard?

This is my first attempt at MIPS and I designed a pseudocode description of a possible algorithm. The code outline is as follows:
main: Print: "Please input a value for N ="
read v0
If(v0 >0)
{
t0 = 0
while(v0 > 0) do
{
t0 = t0 + v0
v0 = v0 +v0 -1
}
print: The sum of integers from 1 to N is = ", t0
go to main
}
else
print: "Honest Abe"
so, with this outline, I made somewhat of an attempt to translate this into MIPS but the whole storing variables and reading them in is confusing me. My attempt of the real code is:
.data
Prompt: .asciiz "\n Please input a value for N="
Result: .asciiz "\n The sum of integers from 1 to N is "
Bye: .asciiz "\n ***Honest Abe***"
.text
main:
li $v0,4 #load $v0 with print_string code
la $a0, Prompt #load $a0 with the message to be displayed
syscall
bgz $v0, else
li $t0, 0
while:
bgz $v0
add $t0,$t0,$v0
addi $v0,$vo -1
From here, I do not understand how the for loops work in MIPS and I do not understand if this is even the correct approach. The whole idea of reading in numbers and then storing the integers isn't processing in my head. Also, I am unsure if I am using the appropriate commands for the loop. Help to any of these questions would be great!
Your algorithm seems a bit odd and too much of a hassle, if I got right what you wanted to do.
You can compute a sum until a certain number with a simple for loop. As in C:
scanf("%d", &num);
for(i=0;i<num;i++)
sum=sum+i;
like an array.
With a little help from the reference card of MARS, you can end up with something like this:
.text
main:
li $v0, 5 #read a number from the keyboard
syscall
addi $s0, $v0, 1 #s0= the number from the keyboard + 1 (the limit)
li $t0, 1 #counter set to 1
loop:
beq $t0, $s0, exit #if the counter becomes equal tothe limit, exit the loop
add $t1, $t1, $t0 #add the counter to the sum to this point
addiu $t0, $t0, 1 #add 1 to the counter for each loop
j loop #go to the "loop" label above
exit:
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall

MIPS Program with Integer and Argument Example

I'm stuck on an exercise, and am unsure how to proceed. This is the exercise:
Write a MIPS assembly language procedure, Test, that accepts 2 integers as arguments and
returns 0 if the integers are equal, 1 if the first is less than the second, and 2 if the first is greater
than the second.
Write a MIPS assembly language program that reads in 2 integers, calls the procedure Test, then
outputs one of the following messages:
The integers are equal
The first integer is less than the second
The first integer is greater than the second
What would be an example to carry this out? Mips is very confusing to me, as I'm used to Java. Thank you.
EDIT: Here is the program I am using as a foundation, since I am unsure where to start:
.data
str1: .asciiz "Please Enter Integer 1: " # a
str2: .asciiz "Please Enter Integer 2: " # a
str3: .asciiz "The sum is " # a
newline: .asciiz "\n" # g
.text
main: addi $v0, $zero, 4
la $a0, str1
syscall
addi $v0, $zero, 5
syscall
add $s0, $zero, $v0
addi $v0, $zero, 4
la $a0, str2
syscall
addi $v0, $zero, 5
syscall
add $s1, $zero, $v0
L1: beq $s1, $zero, cont
addi $v0, $zero, 1
add $a0, $s0, $zero
addi $s1, $s1, -1
syscall
j L1
cont: addi $v0, $zero, 4
la $a0, newline
syscall
addi $v0, $zero, 10
syscall
jr $ra
To check whether one register's value is smaller than another register's value, we can use the
set-on-less-than instruction, which also has a set-on-less-than-immediate counterpart.
slt $r0, $r3, $r4
()if r3 < r4, r0 is set to 1
else r0 is set to 0
slti $r0, $r3, 10
()if r3 < 10, r0 is set to 1
else r0 is set to 0
slt and slti are similar to beq or bne, however, there are two differences. First, they test
whether one value is smaller than another value and, second, they don't branch to some
address, but, instead, set a flag, stored in the first operand.

Mips programming jump problems

Supposed to count number of R, I, and J instructions in the code starting from the first line in main. I have updated the code to all suggestions but results do not seem correct still. :(
EDIT: added those syntax changes, the variables seem to be working better, but the loop doesn't seem to be properly running through itself at all. Any noticeable problems anyone sees?
EDIT2: How do i change the loop to make $t1 rotate through 1 instruction set at a time? THAT is my key problem right now other than the silly syntax from a new mips user.
EDIT3: I have updated the code to all suggestions but results do not seem correct still. :(
.text
Main:
la $t1, 0x400000
li $t5, 0
li $t3, 2
li $t4, 3
li $t2, 0
la $s0, 0x400000
loop:
lw $t1, 0($s0)
addi $s0, 4
addi $t5, 1
beq $t5, 20 exit
srl $s4, $t1, 26
beq $s4, $t2 R
beq $s4, $t3 J
beq $s4, $t4 J
addi $s3, 1
j loop
R:
addi $s1, 1
j loop
J:
addi $s2, 1
j loop
exit:
sw $s1, RType
sw $s2, JType
sw $s3, IType
jr $ra
I see a set of problems:
1) Initialize the registers (don't assume register initial value is zero). For example, addi $t5, 1 requieres a previous add $t5, $zero, $zero (or li to the value you want).
2) the beq jump instruction requires two registers. For example, you have to change beq $s4, 3 J by:
li $t6, 3
beq $s4, $t6, J
In addition to Miguel's points I'd like to add that you're using the instruction set in a strange manner. Perhaps your assembler is capable of figuring out what you meant, but it's still makes the code confusing to read.
lw $t3, 2
lw $t4, 3
These should be li. The lw instruction is used for loading words from memory.
la $t0, 4($t1)
This should be lw, and unless you only want to check the instructions starting from address 0x400004 you should drop the offset.
I also can't seem to find anywhere where you increment $t1, so you'll be using the same address for every iteration of the loop.

Resources