MIPS - My loop doesn't perform quite how it should - mips

I am trying to construct a while loop in Mars MIPS assembly however I have run into a bit of a problem, where the loop itself works, however the action of halving the number with each pass of the loop doesn't seem to initiate after the first round.
Below is the loop I have:
div:
sra $s0, $t1, 1 #halves the number in $s0
li $v0, 1
add $a0, $zero, $s0 #Prints out the number in $s0
syscall
li $a0, 32
li $v0, 11 #Prints out a space
syscall
loop:
blt $s0, $s4, exit #Loops to the start of div until it reaches 1
j div
exit:
li $v0, 10 #This is the exit.
syscall
All help is welcome, as I have a terrible feeling a rookie mistake is being made here.

Related

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, how to invert a series of numbers in a while loop (a rapid method)

I coding an exercise but I have find a little problem... My exercise ask me that convert a character (keyboard input) in:
ASCII code
Binary code
Hex code
without the MIPS syscall. 2 points on 3 are solved for now but in the point 2 I have find my problem... When I insert a letter for example, the binary code is invertited (see below)
Give me a character: a
ASCII: 97
Binary 10000110 (The real is 01100001)
Exist an easy method to invert this number? I leave the code below.
print_binary:
#move the function parameter in t0
move $t0, $a0
#Show a message
li $v0, 4
la $a0, m3
syscall
while:
#check if my counter is 8(conversion on 8 bit)
beq $t2, 8, end_loop
#Divide the number
div $t1, $t0, 2
#move hi in t4 to show in video
mfhi $t4
#Counter
add $t2, $t2, 1
#move for another division
move $t0, $t1
#Show hi for every division
li $v0, 1
move $a0, $t4
syscall
j while
end_loop:
jr $ra
I don't use the array very well, so I would avoid this method if possible...
Thank you

MIPS Beq Add/Subtract Switch

I am new to MIPS trying to figure out Branches and Switches. All I want to do is set switch 0 to add and switch 1 to subtract. Can someone help and explain what I am doing wrong because every video I watch does not help. Every time I run the program I previously had, all it did was run the add: switch over and over again.
li $a0, 2
li $a1, 1
main:
li $s0, 0xf0100000
li $s1, 0b00000001
li $s2, 0b00000010
start:
lw $t0, 0($s0)
beq <------ This is what I don't understand
nop
add:
addu $v0, $a0, $a1
j start
nop
subtract:
subu $v0, $a0, $a1
j start
nop

MIPS: is it possible to overwrite certain words of a file?

Is it possible to overwrite a file using MIPS?
I have a file and, under certain conditions (i.e. a user decide to update his personal data or delete completely) I need to delete/overwrite some words or row of the text file I have.
I tried this thing:
I already know how to find which word I would like to replace and, with a store byte i write on a buffer the "new" word.
Then I should save it on file. And that's where my problems begin since, using flag 1 (on syscall 13) overwrites the whole file and flag 9 doesn't apply any change. Here's my code. What am I doing wrong?
loop:
la $t6, empty_space
sb $t6, buffer($s7)
beq $s7, $t5, save_on_file
subi $s7, $s7, 1
j loop
save_on_file:
#open file
li $v0, 13
la $a0, file_out
li $a1, 1
li $a2, 0
syscall
move $s6, $v0
#write on file
li $v0, 15
move $a0, $s6
la $a1, buffer
move $a2, $s7
syscall
#close
li $v0, 16
move $a0, $s6
syscall
j menu

MIPS, using a while loop to calculatethe sum of odd integers 1-9

The following is my code in MIPS to calculate the sum of odd integers using a while loop.
.data
num: .space 4
.text
.globl main
main:
li $t1, 1
li $t2, 9 # make $t2 9 to break the loop
li $t3, 1
loop:
beq $t3, 11, Exit # check to see if $t3 = 11 if so exit
addi $t3, $t3, 2 # change $t3 to next odd number by adding 2
add $t1, $t1, $t3 # add $t3 to $t1 (1+3,...3+5...etc...)
j loop #jump back to the start of the loop
Exit:
li $v0, 1 # system call code to print an int
lw $a0, num # address of int to print
syscall # print the int
jr $ra #exit
This is my first real experience with MIPS and I'm not sure what is going wrong in this code. I put the print inside the while loop to see if it was ever calculating, but the result is always 1.
So, my result at the end is just 111111.
Edit: Removed the prints inside of the loop with the same result.
And OS is Windows 7 64x
Update: Having num as a variable was over complicating things. Code has been revised as follow and works. Thank you for the help!
enter code here
.data
.text
.globl main
main:
addi $t1, $0, 1
addi $t2, $0, 3
loop: bge $t2, 11, Exit # check to see if $t3 >= 11 if so exit
add $t1, $t1, $t2 # add $t2 to $t1 (1+3,...3+5...etc...)
addi $t2, $t2, 2 # change $t2 to next odd number by adding 2
j loop #jump back to the start of the loop
Exit:
li $v0, 1 # system call code to print an int
move $a0,$t1 # address of int to print
syscall # print the int
jr $ra #exit
la $t1, num
You're clearly getting into trouble here, since you're overwriting your accumulator with the address of num every time you're making the syscall. You're losing the current state of your calculation each time.
You'll need to either save your registers, or simply use different ones. Since I don't know what OS it is that you're using, I don't know if you more generally need to save registers over a syscall, but that could also be a source of errors.
I did similar problems for an architecture class and this seemed to be a recurrent problem among all students. When facing problems similar to this our professor's recommendation was to use a different register to temporarily store the register's address to avoid overwriting other desired values from our most commonly used regs.