remove leading and trailing whitespace mips? - mips

so I figured out this program that basically codes a number pattern and the numbers have to be tabbed in between each other, so for example:
1 1 1
but the last "1" also has a tab and i need to remove that. this is what my code looks like for tabbing: i use this before the end of my for loop, so it can incrementing how many ever times. i don't really know where to start for creating a conditional that doesn't print the last number with a tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall

You didn't provide enough code to give a full answer, but there are several ways to omit printing the last tab:
If you know you are processing the last item you can jump over the print tab code, e.g. suppose you are in a while-loop where you loop while $t0 is different than $t1, then you can write:
while_loop:
# .... do something
beq $t0, $t1, skip
# your code to print tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall
skip:
# ... something else
bne $t0, $t1 while_loop % this is the condition to keep in the loop
if the printing of the tab is the last thing you were doing in the loop, then you can simplify a bit:
while_loop:
# .... do something
beq $t0, $t1, while_loop
# your code to print tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall
b while_loop
Another approach is to print the tab at the beginning of the loop, save for the first iteration. Useful if you are iterating over some values on a register and know some initial value wont be repeated. In this example I will just use a supposedly spare register:
li $t7, 0 # $t7 will only have 0 on the first iteration of the loop
while_loop:
beq $t7, $zero, skip
# your code to print tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall
skip:
li $t7, 1
% your remaining code here, which at some point goes to the while_loop

Related

MIPS- How to Subtract (already used sub- does not work!)

My code comes up with Unsupported R-Type, how do I make it subtract correctly. Tried changing other details within.... Already tried using subu...............................................................................................
.data
str: .asciiz "\nHello World!\n"
# You can change what is between the quotes if you like
.text
.globl main
main:
# Do the addition
# For this, we first need to put the values
# to add into registers ($t0 and $t1)
li $t0, 30 # You can change the 10
li $t1, 20 # You can change the 20
# Now we can add the values in $t0
# and $t1, putting the result in special register $a0
sub $a0, $t0, $t1
# Set up for printing the value in $a0.
# A 1 in $v0 means we want to print an int
li $v0, 1
# The system call looks at what is in $v0
# and $a0, and knows to print what is in $a0
syscall
# Now we want to print Hello World
# So we load the (address of the) string into $a0
la $a0, str
# And put a 4 in $v0 to mean print a string
li $v0, 4
# And just like before syscall looks at
# $v0 and $a0 and knows to print the string
syscall
# Nicely end the program
li $v0, 0
jr $ra
Your program runs fine in the mars and spim simulators except for program termination. These simulators don't set up $ra, so it's zero. So, at the end, you're returning to something that may have semi-random instructions, including an illegal one. Thus, it's not your sub at all that is the problem. It's what happens later.
Change:
# Nicely end the program
li $v0, 0
jr $ra
Into:
# Nicely end the program
li $v0, 10
syscall

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

Program defining if an input integer is divided by 2

I am trying to make a program on QtSpim that constantly takes new integers as input (through the console) and then prints them on the console only when they are even numbers. I set the input 5 as the exit case. The program compiles as it should and when I press the run button there doesn't seem to have a problem. The problem is when I try to type the input number, as the console doesn't respond to that (the number I just typed doesn't even appear on the screen).
Here is my code, I imagine the mistake must be in the first lines where the input number is read, but I can't find it:
.text
.globl __start
__start:
li $v0,5
syscall
move $t0,$v0
add $t1,$t0,$zero
addi $t2,$zero,5
LOOP: div $t0,$t0,2
bne $t0,$zero,LOOP
mfhi $t3
bne $t3,$zero,REPEAT
li $v0,1
move $a0,$t3
syscall
REPEAT:bne $t1,$t2,__start
li $v0,10
syscall
.data
The thing can be done in a much simpler way, using the bitwise and.
Every odd number will have the last bit set, which will make number & 1 equal to 1.
.text
.globl __start
__start:
li $t0, 5
loop:
move $v0, $t0 # set $v0 to 5: read integer
syscall # read in the number
andi $t1, $v0, 1 # check if it's divisible by 2
bnez $t1, check # if no, jump to a check for 5
move $a0, $v0 # if yes, print it
li $v0, 1 # set $v0 to 1: print integer
syscall # do the printing
j loop # continue
check:
bne $t0, $v0, loop # if the integer read is not equal to 5, run again
li $v0, 10
syscall # exit
Your method of checking for even numbers is incorrect. Whenever you enter a number >=1 you'll end up dividing 1 by 2 on the last iteration of your loop. And of course 1 MOD 2 is 1, so your code always thinks the number is odd.
A single division by 2 is sufficient to determine if the value is odd or even. But an AND operation would be even more efficient:
andi $t0,$t0,1 # if the least significant bit is set, the number is odd
bne $t0,$zero,REPEAT
After making that change you'll probably also have to change the printing code, since the value to print no longer is in $t3.

Printing out value in loop

Hey guys Im new to ComArch. Im writing a homework assignment. My question is i have this loop in my program that divides a users int input by 10 and my loop will go through the and divide until the quotient is at 0 . Currently it prints out 0 for my quotient but doest print out each value of the remainder. So how would i modify my code to print out every single remainder that is left over after dividing.
loop:
li $s0, 10 #divisor
div $t0,$s0 #divide input by 10
mflo $t0 #quotiant
mfhi $t3 #remainder
sw $t3, ($t1) #stores emainder into address of int_a
addi $t1,$t1,4 #increases the pointer
bne $t0,0,loop
You will find in the MIPS Architecture and Assembly Language Overview the section "System Calls and I/O (SPIM Simulator)" which details how to print a value.
In your case, within the loop, you would have to print $t3.
li $v0, 1 # load appropriate system call code into register $v0;
# code for printing integer is 1
move $a0, $t3 # move integer to be printed into $a0: $a0 = $t3
syscall # call operating system to perform operation

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.