code transformation for mips program - mips

I was wondering if anyone can help in a simple solution. I have a program in assembly that counts decimals from 0-31 and counts in binary beginning from the 5th left digit. I want to transform the program to count 0-31 in binary and show it in console in normal count starting from the right digit. Here's the first code:
main:
addi $9,$0,27 #example number
addi $10,$0,5
addi $2,$0,1
L1:
and $4,$9,$2
syscall
srl $9,$9,1
addi $10,$10,-1
nbe $10,$0,L1
end
Thank you in advance!

Just found it:
.data
.text
main:
addi $9,$0,23
addi $10,$0,5
addi $2,$0,1
sll $8,$2,4
Loop1:
and $4,$8,$9
srl $4,$4,4
syscall
sll $9,$9,1
addi $10,$0,-1
bne $10,$0, Loop1
.end main

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 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).

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.