MIPS practice in writing lists of number - mips

As the question required below,
Write a program in MIPS32 assembly language which reads a positive integer N and prints out the following:
1
12
123
1234
12345
...
1 2 3 4 5 6 ... N
Here is my code shown below:
.data
word: .asciiz "Please enter a random row: \n"
.text
.globl main
main:
la $a0, word # load word
li $v0, 4
syscall
li $v0, 1 # service 1 is print integer
move $a0, $t0 # move register to be printed into argument
register $a0
syscall
lw $s0, row # $s0 = row
addi $s0, $s0, -1 # for accommodating loop condition
li $s1, 1 # set i = 1
li $s2, 1 # set j = 1
loop1:
blt $s0, $s1, Exit # for(i = 0; i < row; i++)
addi $s1, $s1, 1 # i++
li $s2, 1 # resets j to 1 after every iteration of for
loop
move $a0, $t0 # move register to be printed into argument
register $a0
j loop2 # executing the nested for loop
loop2:
blt $s1, $s2, loop1 # for(i = 0; i < j; i++)
li $v0, 1 # read_double => scanf("%d")
move $a0, $t0 # move register to be printed into argument $a0
syscall
j loop2
li $v0, 10 # loading exit code
syscall # execute exit
As I tend to run it through, then I still could not achieve the expected outcome.

Here's how you'll write the code to do this:
Ask user for N value, & save.
Create a loop that will continue until N iterations (see conditional branching)
For N iterations, save the value 1, 1+=1, ... N in the register.
Remember that each word is 4 bytes. Make sure you are changing where you are accessing your register to save your values correctly, or else they will overwrite each other.
Use another loop to print all values (again, conditional branching).
If you want help with code, try writing some first. :-)

Related

MIPS program for calculating n choose k using recursion

I started a course of assembly and I am using MIPS as the programming language. I try to solve the problem of n choose k. Here is the code that I got so far but it always shows 1 as the result.
# Calculate n choose k
#
# n: integer value for n
# k: integer value for k
#
# Return: integer value for n choose k
.text
.globl nChooseK
nChooseK:
# Check if k is equal to 0 or k is equal to n
beq $a1, $zero, return_1
beq $a1, $a0, return_1
# Calculate n-1 choose k-1
sub $t0, $a0, 1
sub $t1, $a1, 1
jal nChooseK
move $t2, $v0
# Calculate n-1 choose k
move $a0, $a0
move $a1, $t1
jal nChooseK
move $t3, $v0
# Return the sum of the two calculations
add $v0, $t2, $t3
jr $ra
return_1:
# Return 1 if k is equal to 0 or k is equal to n
li $v0, 1
# Print the result of n choose k
.globl main
main:
li $a0, 5 # Load the value of n into $a0
li $a1, 3 # Load the value of k into $a1
#jal nChooseK # Call the nChooseK function
move $a0, $v0 # Load the result into $a0
li $v0, 1 # Set $v0 to 1 to print an integer
syscall # Print the result
li $v0, 10 # Set $v0 to 10 to exit the program
syscall # Exit the program
Your problem is most likely here:
return_1:
# Return 1 if k is equal to 0 or k is equal to n
li $v0, 1
# fallthrough into main (comment added by me). Put jr $ra here
# Print the result of n choose k
.globl main
Regardless of your input, eventually one of your recursions will have to return 1 as a base case. So, what's happening is, you're setting $v0 to 1 and going back into main. Now, that being said, I'm not sure why this doesn't result in an infinite loop, since you'd end up calling nChooseK over and over again and overflow the stack. Could be that you've commented out the jal nChooseK down at the bottom.

MIPS: Infinite loop with branches

So I have a program that takes an input from the user (integer above 0) and adds up all even numbers below it to achieve a return answer (Ex: input: 7; ans: 2 + 4 + 6 = 12).
The issue with this program is that it's meant to break out of the loop based on if my 'active even variable' ($t1) > the input. Although my program never seems to properly interpret the branch and loops indefinitely until $t1 overflows (I have checked the debugger and know that the program does run the branch line every time). Below is my code:
.data
N: .word 0
Result: .word 0
.text
.globl main
initialize:
li $v0, 5 #getting arg1 from user
syscall
la $t0, N
sw $v0, 0($t0)
li $t1, 2
li $t2, 0
main:
blt $t0, $t1, fin2
fori:
add $t2, $t2, $t1 #t2 += t1
add $t1, $t1, 2 #t1 += 2
slt $t5, $t1, $t0
bne $t5, $zero, fori
fin:
li $v0,1 #prints return value
move $a0, $t2
syscall
li $v0, 10
syscall
fin2:
li $v0,1 #prints return value
move $a0, $zero
syscall
li $v0, 10
syscall
So I don't know if you NEED to be using word storage and such, but really you were just over complicating it in doing so. All you needed was a simple loop which has a counter that increments by 2, checks if it is larger than the initial value, and then add that overall value to the result
.text
.globl main
initialize:
li $v0, 5 # Getting arg1 from user
syscall # System call
move $t0, $v0 # Store the input value in $t0
li $t1, 0 # Initializing the result register
li $t2, 0 # Initializing the addition/counter register
main:
loop:
add $t2, $t2, 2 # Increase the value to be added by 2 (next even value)
bge $t2, $t0, fin # Check if the increment is larger than or equal to the initial input, if so break to finish
add $t1, $t1, $t2 # Increment the result by adding the even value
j loop # jump bak to the top of the loop
fin:
li $v0,1 # let the system know an integer is going to be printed
move $a0, $t1 # Load the result into the $a0 register (the register that prints values)
syscall # System Call
li $v0, 10 # Let the system know the program is going to exit
syscall # System Call
So as you can see $t2 increments by 2 each time. After each increment it is compared to the input value. If the input ($t0) than $t2 then add the value of $t2 to the result ($t1). This way there is an increment counter which is also used to add the necessary even value to the result.
Edit:
Not sure if this is entirely what you meant but I just tossed in some loads and saves, using the s registers as those are the register that are supposed to be used when saving values.
.data
N: .word 0
Result: .word 0
.text
.globl main
initialize:
li $v0, 5 # Getting arg1 from user
syscall # System Call
la $s0, N # Load the address of N into $s0
sw $v0, 0($s0) # Store the input value in 0 index of N
li $t2, 0 # Initializing the addition/counter register
la $s1, Result # Load the address of Result into $s1
main:
sw $t2, 0($s1) # Setting the 0 index of Result to 0
loop:
add $t2, $t2, 2 # Increase the value to be added by 2 (next even value)
lw $t4, 0($s0) # Loading the input value into the $t4 register
bge $t2, $t4, fin # Check if the increment is larger than or equal to the initial input, if so break to finish
lw $t4, 0($s1) # Loading the current result into the $t4 register
add $t4, $t4, $t2 # Increment the result by adding the even value
sw $t4, 0($s1) # Saving the new current result into the $t4 register
j loop # jump bak to the top of the loop
fin:
li $v0,1 # let the system know an integer is going to be printed
lw $a0, 0($s1) # Load the result into the $a0 register (the register that prints values)
syscall # System Call
li $v0, 10 # Let the system know the program is going to exit
syscall # System Call

How to count the number of words and print the result in mips?

So I am fairly new to mips and am trying to do a hmwk.
The exercise is to create a block of words with 0 as the last word , to count them knowing we don't consider the last element 0 and print out the result.
Here is my code for the moment
.data
blockOfWords: .word 12,43,549,7,60,0
.text
la $a0, blockOfWords #putting address of blockOfWords in $a0
loopStart: sll $t1, $s3,2 #iterates thru blockOfWords by jumping by 4 bytes, $t1=4*i
add $t1,$t1,$a0 #we increment the address by four so as to advance in the array after every iteration
lw $a1, 0($t1) #$a1=blockOfWOrds[i]
beq $a1,$zero, Exit #if the value in $a1[i]==0, exit the loop
addi $s0,$s0,1 #else, increment by one the cnt=$s0
j loopStart #and continue looping thru blockOfWords
Exit:
#how do i print the result?
So instead of doing a shift left logical it is a lot easier to up the memory address in the array
In this way the lw $a1, 0($a0) is just grabbing the beginning of the word
it is then incremented by 4 after a check for the 0. This makes the function much simpler and easier to read.
.data
blockOfWords: .word 12,43,549,7,60,0
.text
main:
la $a0, blockOfWords # Load beginning address into $a0
loopStart:
lw $a1, 0($a0) # load the value at the beginning of $a0 into $a1
beq $a1,$zero, exit # Check if $a1 is 0, if so jump to exit
addi $s0,$s0,1 # Add one to the count
addi $a0, $a0, 4 # Up the intial index of the array by 4, up one word
j loopStart # Re-loop
exit:
li $v0, 1 # Load the value of 1 into the $v0 register
la $a0, ($s0) # Load the counter into the $a0 address
syscall
li $v0, 10 # A value 10 syscall to indicate end of program
syscall

Printing an hourglass in mips32

I'm having problems solving this question. I want to print an hourglass using mips32. n is an integer given by user as input and the hourglass must be printed in n lines. For example, for n = 5 the output is:
*****
***
*
***
*****
Here is my code for the first part (the triangle top of the hourglass). The problem is that it prints only the first line of stars and then exits. By running my code line by line in Mars, I understood that the first line of the backToLoop1 label is run every time loop3 is run! So it causes the program to end after the first line. I really can't realize why this happens.
.data
newLine: .asciiz "\n"
.text
main:
li $v0, 5 # read n
syscall # call sysetem
addi $t2, $v0, 0 # moves n to $t2
li $t0, 1 # i= 1
loop1:
blt $t2, $t0, Exit # if n<i exit
la $a0, newLine # go to next line
addi $v0, $0, 4 # 4 represents printing string
syscall # call system
# loop2 bounds
li $t1, 1 # k= 1
subi $t3, $t0, 1 # $t3= i-1 upper bound for loop2
# loop3 bounds
li $t5, 1 # j= 1
addi $t6, $t2, 1 # t6= n+1
sub $t6, $t6, $t0 # $t6= n+1-i upper bound for loop3
loop2:
blt $t3, $t1, loop3
li $a0, ' ' # load space to $a0
la $v0, 11 # 11 represents printing character
syscall # call system
addi $t1, $t1, 1 # k++
ble $t1, $t3, loop2 # if <= i-1 loop2 again
loop3:
blt $t6, $t5, backToLoop1 # back to loop1
li $a0, '*' # load star to $a0
la $v0, 11 # 4 represents printing character
syscall # call system
addi $t5, $t5, 1 # j++
ble $t5, $t6, loop3 # if j <= n-i+1 loop3 again
backToLoop1:
addi $t0, $t0, 1 # i++
ble $t0, $t2, loop1 # if i<=n loop1 again
blt $t2, $t0, Exit
Exit: # Terminate the program
li $v0, 10 # 10 represents exit
syscall # call system
You're off to a good start. However, there doesn't appear to be a clear strategy for slanting the right side of the hourglass. Ideally we can write logic to handle drawing the bottom half without duplicating most of the logic.
My default approach for this sort of pattern is to use two pointers, a left starting at 0 and right starting at n - 1. These represent the index bounds for the asterisk characters for each row. Per row iteration, decrement the right pointer and increment the left pointer, essentially drawing an "X" pattern on the n by n grid.
This strategy gets us 95% of the way there. The last step is to temporarily swap the left and right pointers if left > right, which handles drawing the bottom half without too much spaghetti.
.data
prompt: .asciiz "enter a number: "
.text
main:
la $a0 prompt # collect n
li $v0 4
syscall
li $v0 5
syscall
move $s3 $v0 # n
li $s0 0 # left index
move $s1 $s3 # right index = n - 1
addi $s1 $s1 -1
row_loop:
bltz $s1 exit # while right-- >= 0
li $s2 0 # column index
col_loop:
beq $s2 $s3 row_loop_done # for 0..n
# if left > right, swap temporarily
move $t0 $s0
move $t1 $s1
blt $t0 $t1 pick_char
move $t2 $t0
move $t0 $t1
move $t1 $t2
pick_char:
# '*' if left <= i <= right else ' '
blt $s2 $t0 pick_space
bgt $s2 $t1 pick_space
li $a0 42 # print '*'
j print_char
pick_space:
li $a0 32 # print ' '
print_char:
li $v0 11
syscall
addi $s2 $s2 1 # column index++
j col_loop
row_loop_done:
li $a0 10 # print newline
li $v0 11
syscall
addi $s1 $s1 -1 # right--
addi $s0 $s0 1 # left++
j row_loop
exit:
li $v0 10
syscall

MIPS Mult two numbers and result it negative number

I am new for MIPS, there is my code:
.text
main:
add $s0, $zero, 1 # set $s0 to 0 for the initial value for the following loop
Loop: jal srand # Call function srand
addi $s0, $s0, 1 # $s0 = i++
slti $s1, $s0, 6 # $s1 < 6
bne $s1, $zero, Loop # go to loop in i < 6
beq $s1, 6, end_loop # go out the loop when i == 6
end_loop:
li $v0, 10
syscall
srand: # This function will set the numbers for future calculation
lw $t0, a # Load a value to $t0 1103515245
lw $t1, c # Load c value to $t1 12345
lw $t2, m # Load m value to $t2 2147483648
multu $t0,$s0 # result for multiplication (Xn*a) and store the result to $t3
add $t4, $t3, $t1 # result for add multiplication (Xn*a+c) and store the result to $t4
move $a0, $t4 # Debug function
li $v0, 1 # Debug function
syscall # Debug function
la $a0, msg
li $v0, 4
syscall
jr $ra
There is a problem, when the code goes to this "multu $t0,$s0" commend and result will be wrong.
1103515245 * 2 returned a negative number -2087936806
anyone know how to fix it ??
thanks
As chrylis said, it looks like integer overflow. If that's unclear to you, you should read up on two's complement integer representations.
Basically, the value of the highest-order bit is defined to be negative. Suppose you have 32-bit integers. Then 0x800000 will have the value of -2**32, and the other bits have their normal value, so you end up with an expression that looks like -2**32 + [the sum of the other bits], in particular 0xFFFFFFFF has the value of -1, 0xFFFFFE is -2, and so on.